1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitCommaSeparatedStatements.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements;
16 use Perl::Critic::Utils qw{ :booleans :characters :severities :classification };
17 use Perl::Critic::Utils::PPI qw{ is_ppi_statement_subclass };
19 use base 'Perl::Critic::Policy';
21 our $VERSION = '1.088';
23 #-----------------------------------------------------------------------------
25 Readonly::Scalar my $DESC => q{Comma used to separate statements};
26 Readonly::Scalar my $EXPL => [ 68, 71 ];
28 #-----------------------------------------------------------------------------
30 sub supported_parameters {
33 name => 'allow_last_statement_to_be_comma_separated_in_map_and_grep',
34 description => 'Allow map and grep blocks to return lists.',
35 default_string => $FALSE,
36 behavior => 'boolean',
41 sub default_severity { return $SEVERITY_HIGH }
42 sub default_themes { return qw( core bugs pbp ) }
43 sub applies_to { return 'PPI::Statement' }
45 #-----------------------------------------------------------------------------
48 my ( $self, $elem, undef ) = @_;
50 # Grrr... PPI instantiates non-leaf nodes in its class hierarchy...
51 return if is_ppi_statement_subclass($elem);
53 # Now, if PPI hasn't introduced any new PPI::Statement subclasses, we've
54 # got an element who's class really is PPI::Statement.
56 return if _is_parent_a_constructor_or_list($elem);
57 return if _is_parent_a_for_loop($elem);
60 $self->{_allow_last_statement_to_be_comma_separated_in_map_and_grep}
62 return if not _is_direct_part_of_map_or_grep_block($elem);
65 foreach my $child ( $elem->schildren() ) {
67 not $self->{_allow_last_statement_to_be_comma_separated_in_map_and_grep}
68 and not _is_last_statement_in_a_block($child)
70 if ( $child->isa('PPI::Token::Word') ) {
71 return if _succeeding_commas_are_list_element_separators($child);
73 elsif ( $child->isa('PPI::Token::Operator') ) {
74 if ( $child->content() eq $COMMA ) {
75 return $self->violation($DESC, $EXPL, $elem);
84 sub _is_parent_a_constructor_or_list {
87 my $parent = $elem->parent();
89 return if not $parent;
92 $parent->isa('PPI::Structure::Constructor')
93 or $parent->isa('PPI::Structure::List')
97 sub _is_parent_a_for_loop {
100 my $parent = $elem->parent();
102 return if not $parent;
104 return if not $parent->isa('PPI::Structure::ForLoop');
106 return 1 == scalar $parent->schildren(); # Multiple means C-style loop.
109 sub _is_direct_part_of_map_or_grep_block {
112 my $parent = $elem->parent();
113 return if not $parent;
114 return if not $parent->isa('PPI::Structure::Block');
116 my $block_prior_sibling = $parent->sprevious_sibling();
117 return if not $block_prior_sibling;
118 return if not $block_prior_sibling->isa('PPI::Token::Word');
120 return $block_prior_sibling eq 'map' || $block_prior_sibling eq 'grep';
123 sub _is_last_statement_in_a_block {
126 my $parent = $elem->parent();
127 return if not $parent;
128 return if not $parent->isa('PPI::Structure::Block');
130 my $next_sibling = $elem->snext_sibling();
131 return if not $next_sibling;
136 sub _succeeding_commas_are_list_element_separators {
140 is_perl_builtin_with_zero_and_or_one_arguments($elem)
141 and not is_perl_builtin_with_multiple_arguments($elem)
146 my $sibling = $elem->snext_sibling();
148 return 1 if not $sibling; # There won't be any succeeding commas.
150 return not $sibling->isa('PPI::Structure::List');
157 #-----------------------------------------------------------------------------
163 Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements - Don't use the comma operator as a statement separator.
167 This Policy is part of the core L<Perl::Critic> distribution.
172 Perl's comma statement separator has really low precedence, which
173 leads to code that looks like it's using the comma list element
174 separator not actually doing so. Conway suggests that the statement
175 separator not be used in order to prevent this situation.
177 The confusion that the statement separator causes is primarily due to
178 the assignment operators having higher precedence.
180 For example, trying to combine two arrays into another like this won't
185 because it is equivalent to
190 Conversely, there are the built-in functions, like C<print>, that
191 normally force the rest of the statement into list context, but don't
192 when called like a subroutine.
194 This is not likely to produce what is intended:
196 print join q{, }, 2, 3, 5, 7, ": the single-digit primes.\n";
198 The obvious fix is to add parentheses. Placing them like
200 print join( q{, }, 2, 3, 5, 7 ), ": the single-digit primes.\n";
204 print ( join q{, }, 2, 3, 5, 7 ), ": the single-digit primes.\n";
206 will not, because it is equivalent to
208 print( join q{, }, 2, 3, 5, 7 );
209 ": the single-digit primes.\n";
214 This policy can be configured to allow the last statement in a C<map>
215 or C<grep> block to be comma separated. This is done via the
216 C<allow_last_statement_to_be_comma_separated_in_map_and_grep> option
219 [ValuesAndExpressions::ProhibitCommaSeparatedStatements]
220 allow_last_statement_to_be_comma_separated_in_map_and_grep = 1
222 With this option off (the default), the following code violates this
225 %hash = map {$_, 1} @list;
227 With this option on, this statement is allowed. Even if this option
228 is off, using a fat comma C<< => >> works, but that forces
229 stringification on the first value, which may not be what you want.
233 Elliot Shank C<< <perl@galumph.com> >>
237 Copyright (c) 2007-2008 Elliot Shank. All rights reserved.
239 This program is free software; you can redistribute it and/or modify
240 it under the same terms as Perl itself. The full text of this license
241 can be found in the LICENSE file included with this module.
247 # cperl-indent-level: 4
249 # indent-tabs-mode: nil
250 # c-indentation-style: bsd
252 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :