1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Variables/RequireLexicalLoopIterators.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Variables::RequireLexicalLoopIterators;
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Loop iterator is not lexical};
23 Readonly::Scalar my $EXPL => [ 108 ];
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_HIGHEST }
29 sub default_themes { return qw(core pbp bugs) }
30 sub applies_to { return 'PPI::Statement::Compound' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
37 # First child will be 'for' or 'foreach' keyword
38 my $first_child = $elem->schild(0);
39 return if !$first_child;
40 return if $first_child ne 'for' and $first_child ne 'foreach';
42 # The second child could be the iteration list
43 my $second_child = $elem->schild(1);
44 return if !$second_child;
45 return if $second_child->isa('PPI::Structure::ForLoop');
47 return if $second_child eq 'my';
49 return $self->violation( $DESC, $EXPL, $elem );
52 #-----------------------------------------------------------------------------
58 #-----------------------------------------------------------------------------
64 Perl::Critic::Policy::Variables::RequireLexicalLoopIterators - Write C<for my $element (@list) {...}> instead of C<for $element (@list) {...}>.
68 This Policy is part of the core L<Perl::Critic> distribution.
73 C<for>/C<foreach> loops I<always> create new lexical variables for named
74 iterators. In other words
86 This may not seem like a big deal until you see code like
89 for $bicycle (@things_attached_to_the_bike_rack) {
92 and $bicycle->has_baseball_card_in_spokes()
93 and $bicycle->has_bent_kickstand()
95 $bicycle->remove_lock();
101 if ( $bicycle and $bicycle->is_unlocked() ) {
105 which is not going to allow you to arrive in time for dinner with your family
106 because the C<$bicycle> outside the loop is different from the C<$bicycle>
107 inside the loop. You may have freed your bicycle, but you can't remember
113 This Policy is not configurable except for the standard options.
118 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
122 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
124 This program is free software; you can redistribute it and/or modify
125 it under the same terms as Perl itself. The full text of this license
126 can be found in the LICENSE file included with this module.
132 # cperl-indent-level: 4
134 # indent-tabs-mode: nil
135 # c-indentation-style: bsd
137 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :