Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / Variables / RequireLexicalLoopIterators.pm
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) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::Variables::RequireLexicalLoopIterators;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{Loop iterator is not lexical};
23 Readonly::Scalar my $EXPL => [ 108 ];
24
25 #-----------------------------------------------------------------------------
26
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' }
31
32 #-----------------------------------------------------------------------------
33
34 sub violates {
35     my ( $self, $elem, undef ) = @_;
36
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';
41
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');
46
47     return if $second_child eq 'my';
48
49     return $self->violation( $DESC, $EXPL, $elem );
50 }
51
52 #-----------------------------------------------------------------------------
53
54 1;
55
56 __END__
57
58 #-----------------------------------------------------------------------------
59
60 =pod
61
62 =head1 NAME
63
64 Perl::Critic::Policy::Variables::RequireLexicalLoopIterators - Write C<for my $element (@list) {...}> instead of C<for $element (@list) {...}>.
65
66 =head1 AFFILIATION
67
68 This Policy is part of the core L<Perl::Critic> distribution.
69
70
71 =head1 DESCRIPTION
72
73 C<for>/C<foreach> loops I<always> create new lexical variables for named
74 iterators.  In other words
75
76   for $zed (...) {
77      ...
78   }
79
80 is equivalent to
81
82   for my $zed (...) {
83      ...
84   }
85
86 This may not seem like a big deal until you see code like
87
88   my $bicycle;
89   for $bicycle (@things_attached_to_the_bike_rack) {
90       if (
91               $bicycle->is_red()
92           and $bicycle->has_baseball_card_in_spokes()
93           and $bicycle->has_bent_kickstand()
94       ) {
95           $bicycle->remove_lock();
96
97           last;
98       }
99   }
100
101   if ( $bicycle and $bicycle->is_unlocked() ) {
102       ride_home($bicycle);
103   }
104
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
108 which one it was.
109
110
111 =head1 CONFIGURATION
112
113 This Policy is not configurable except for the standard options.
114
115
116 =head1 AUTHOR
117
118 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
119
120 =head1 COPYRIGHT
121
122 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
123
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.
127
128 =cut
129
130 # Local Variables:
131 #   mode: cperl
132 #   cperl-indent-level: 4
133 #   fill-column: 78
134 #   indent-tabs-mode: nil
135 #   c-indentation-style: bsd
136 # End:
137 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :