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