File Coverage

blib/lib/Perl/Critic/Policy/Community/LexicalForeachIterator.pm
Criterion Covered Total %
statement 13 14 92.8
branch n/a
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 20 22 90.9


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   350 use warnings;
  1         2  
  1         25  
4 1     1   4  
  1         2  
  1         23  
5             use Perl::Critic::Utils qw(:severities :classification :ppi);
6 1     1   4 use parent 'Perl::Critic::Policy::Variables::RequireLexicalLoopIterators';
  1         3  
  1         42  
7 1     1   302  
  1         1  
  1         6  
8             our $VERSION = 'v1.0.3';
9              
10              
11 2     2 1 27297 1;
12 0     0 1    
13             =head1 NAME
14              
15             Perl::Critic::Policy::Community::LexicalForeachIterator - Don't use undeclared
16             foreach loop iterators
17              
18             =head1 DESCRIPTION
19              
20             It's possible to use a variable that's already been declared as the iterator
21             for a L<foreach loop|perlsyn/"Foreach Loops">, but this will localize the
22             variable to the loop and its value will be reverted after the loop is done.
23             Always declare the loop iterator in the lexical scope of the loop with C<my>.
24              
25             foreach $foo (...) {...} # not ok
26             for $bar (...) {...} # not ok
27             foreach my $foo (...) {...} # ok
28             for my $bar (...) {...} # ok
29              
30             This policy is a subclass of the L<Perl::Critic> core policy
31             L<Perl::Critic::Policy::Variables::RequireLexicalLoopIterators>, and performs
32             the same function but in the C<community> theme.
33              
34             =head1 AFFILIATION
35              
36             This policy is part of L<Perl::Critic::Community>.
37              
38             =head1 CONFIGURATION
39              
40             This policy is not configurable except for the standard options.
41              
42             =head1 AUTHOR
43              
44             Dan Book, C<dbook@cpan.org>
45              
46             =head1 COPYRIGHT AND LICENSE
47              
48             Copyright 2015, Dan Book.
49              
50             This library is free software; you may redistribute it and/or modify it under
51             the terms of the Artistic License version 2.0.
52              
53             =head1 SEE ALSO
54              
55             L<Perl::Critic>