1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/CodeLayout/ProhibitTrailingWhitespace.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::CodeLayout::ProhibitTrailingWhitespace;
13 use English qw(-no_match_vars);
18 use PPI::Token::Whitespace;
19 use Perl::Critic::Utils qw{ :characters :severities };
21 use base 'Perl::Critic::Policy';
23 our $VERSION = '1.088';
25 #-----------------------------------------------------------------------------
27 Readonly::Scalar my $EXPL => q{Don't use whitespace at the end of lines};
29 ## no critic (RequireInterpolationOfMetachars)
30 Readonly::Hash my %C_STYLE_ESCAPES =>
42 #-----------------------------------------------------------------------------
44 sub supported_parameters { return qw{ } }
45 sub default_severity { return $SEVERITY_LOWEST }
46 sub default_themes { return qw( core maintenance ) }
47 sub applies_to { return 'PPI::Token::Whitespace' }
49 #-----------------------------------------------------------------------------
52 my ( $self, $token, undef ) = @_;
54 # There is at most one linefeed per Whitespace token, and it will always
55 # be the last character, if present. If the code has two consecutive
56 # blank lines, PPI will produce two Whitespace tokens, each consisting
57 # of a single linefeed. Thus, any Whitespace token consisting of a single
58 # character cannot contain trailing whitespace.
59 my $content = $token->content();
60 return if length($content) < 2;
61 return if qq{\n} ne chop $content;
63 my $description = q{Found "};
64 $description .= join $EMPTY, map { _escape($_) } split $EMPTY, $content;
65 $description .= q{" at the end of the line};
67 return $self->violation( $description, $EXPL, $token );
71 my $character = shift;
72 my $ordinal = ord $character;
74 if (my $c_escape = $C_STYLE_ESCAPES{$ordinal}) {
79 # Apparently, the charnames.pm that ships with older perls does not
80 # support the C<viacode> function, and newer versions of the module are
81 # not distributed separately from perl itself So if the C<viacode> method
82 # is not supported, then just substitute something.
85 ## no critic (RequireInterpolationOfMetachars)
86 if ( charnames->can( 'viacode' ) ) {
87 return q/\N{/ . charnames::viacode($ordinal) . q/}/;
90 return '\N{WHITESPACE CHAR}';
96 #-----------------------------------------------------------------------------
106 Perl::Critic::Policy::CodeLayout::ProhibitTrailingWhitespace - Don't use whitespace at the end of lines.
110 This Policy is part of the core L<Perl::Critic> distribution.
115 Anything that is not readily visually detectable is a bad thing in
116 general, and more specifically, as different people edit the same
117 code, their editors may automatically strip out trailing whitespace,
118 causing spurious differences between different versions of the same
119 file (i.e. code in a source control system).
124 This Policy is not configurable except for the standard options.
129 Elliot Shank C<< <perl@galumph.com> >>
133 Copyright (c) 2007-2008 Elliot Shank. All rights reserved.
135 This program is free software; you can redistribute it and/or modify
136 it under the same terms as Perl itself. The full text of this license
137 can be found in the LICENSE file included with this module.
143 # cperl-indent-level: 4
145 # indent-tabs-mode: nil
146 # c-indentation-style: bsd
148 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :