1Perl::Critic::Policy::VUasPreeirralbC:lo:enCstr:ri:itPbirucot:he:idPboiPlteiRrcelyu:sD:eoVdcaNuramimeaenbstl(ae3ts)i:o:nProhibitReusedNames(3)
2
3
4

NAME

6       Perl::Critic::Policy::Variables::ProhibitReusedNames - Do not reuse a
7       variable name in a lexical scope
8

AFFILIATION

10       This Policy is part of the core Perl::Critic distribution.
11

DESCRIPTION

13       It's really hard on future maintenance programmers if you reuse a
14       variable name in a lexical scope. The programmer is at risk of
15       confusing which variable is which. And, worse, the programmer could
16       accidentally remove the inner declaration, thus silently changing the
17       meaning of the inner code to use the outer variable.
18
19           my $x = 1;
20           for my $i (0 .. 10) {
21               my $x = $i+1;  # not OK, "$x" reused
22           }
23
24       With "use warnings" in effect, Perl will warn you if you reuse a
25       variable name at the same scope level but not within nested scopes.
26       Like so:
27
28           % perl -we 'my $x; my $x'
29           "my" variable $x masks earlier declaration in same scope at -e line 1.
30
31       This policy takes that warning to a stricter level.
32

CAVEATS

34   Crossing subroutines
35       This policy looks across subroutine boundaries.  So, the following may
36       be a false positive for you:
37
38           sub make_accessor {
39               my ($self, $fieldname) = @_;
40               return sub {
41                   my ($self) = @_;  # false positive, $self declared as reused
42                   return $self->{$fieldname};
43               }
44           }
45
46       This is intentional, though, because it catches bugs like this:
47
48           my $debug_mode = 0;
49           sub set_debug {
50               my $debug_mode = 1;  # accidental redeclaration
51           }
52
53       I've done this myself several times -- it's a strong habit to put that
54       "my" in front of variables at the start of subroutines.
55
56   Performance
57       The current implementation walks the tree over and over.  For a big
58       file, this can be a huge time sink.  I'm considering rewriting to
59       search the document just once for variable declarations and cache the
60       tree walking on that single analysis.
61

CONFIGURATION

63       This policy has a single option, "allow", which is a list of names to
64       never count as duplicates.  It defaults to containing $self and $class.
65       You add to this by adding something like this to your .perlcriticrc:
66
67           [Variables::ProhibitReusedNames]
68           allow = $self $class @blah
69

AUTHOR

71       Chris Dolan <cdolan@cpan.org>
72
73       This policy is inspired by
74       <http://use.perl.org/~jdavidb/journal/37548>.  Java does not allow you
75       to reuse variable names declared in outer scopes, which I think is a
76       nice feature.
77
79       Copyright (c) 2008-2013 Chris Dolan
80
81       This program is free software; you can redistribute it and/or modify it
82       under the same terms as Perl itself.  The full text of this license can
83       be found in the LICENSE file included with this module.
84
85
86
87perl v5.30.0           Perl::Criti2c0:1:9P-o0l7i-c2y6::Variables::ProhibitReusedNames(3)
Impressum