File Coverage

blib/lib/Perl/Critic/Policy/Variables/RequireInitializationForLocalVars.pm
Criterion Covered Total %
statement 29 29 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 13 13 100.0
pod 4 5 80.0
total 55 56 98.2


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Variables::RequireInitializationForLocalVars;
2              
3 40     40   26782 use 5.010001;
  40         200  
4 40     40   265 use strict;
  40         141  
  40         903  
5 40     40   226 use warnings;
  40         120  
  40         955  
6 40     40   229 use Readonly;
  40         119  
  40         2002  
7              
8 40     40   313 use Perl::Critic::Utils qw{ :severities };
  40         117  
  40         2067  
9 40     40   5148 use parent 'Perl::Critic::Policy';
  40         119  
  40         256  
10              
11             our $VERSION = '1.148';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{"local" variable not initialized};
16             Readonly::Scalar my $EXPL => [ 78 ];
17              
18             #-----------------------------------------------------------------------------
19              
20 92     92 0 1748 sub supported_parameters { return () }
21 80     80 1 358 sub default_severity { return $SEVERITY_MEDIUM }
22 92     92 1 475 sub default_themes { return qw(core pbp bugs certrec ) }
23 33     33 1 119 sub applies_to { return 'PPI::Statement::Variable' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 97     97 1 264 my ( $self, $elem, undef ) = @_;
29 97 100 100     250 if ( $elem->type() eq 'local' && !_is_initialized($elem) ) {
30 6         107 return $self->violation( $DESC, $EXPL, $elem );
31             }
32 91         3480 return; #ok!
33             }
34              
35             #-----------------------------------------------------------------------------
36              
37             sub _is_initialized {
38 9     9   399 my $elem = shift;
39 9 100   83   39 my $wanted = sub { $_[1]->isa('PPI::Token::Operator') && $_[1] eq q{=} };
  83         1229  
40 9 100       36 return $elem->find( $wanted ) ? 1 : 0;
41             }
42              
43             1;
44              
45             __END__
46              
47             #-----------------------------------------------------------------------------
48              
49             =pod
50              
51             =head1 NAME
52              
53             Perl::Critic::Policy::Variables::RequireInitializationForLocalVars - Write C<local $foo = $bar;> instead of just C<local $foo;>.
54              
55              
56             =head1 AFFILIATION
57              
58             This Policy is part of the core L<Perl::Critic|Perl::Critic>
59             distribution.
60              
61              
62             =head1 DESCRIPTION
63              
64             Most people don't realize that a localized copy of a variable does not
65             retain its original value. Unless you initialize the variable when
66             you C<local>-ize it, it defaults to C<undef>. If you want the
67             variable to retain its original value, just initialize it to itself.
68             If you really do want the localized copy to be undef, then make it
69             explicit.
70              
71             package Foo;
72             $Bar = '42';
73              
74             package Baz;
75              
76             sub frobulate {
77              
78             local $Foo::Bar; #not ok, local $Foo::Bar is 'undef'
79             local $Foo::Bar = undef; #ok, local $Foo::Bar is obviously 'undef'
80             local $Foo::Bar = $Foo::Bar; #ok, local $Foo::Bar still equals '42'
81              
82             }
83              
84              
85             =head1 CONFIGURATION
86              
87             This Policy is not configurable except for the standard options.
88              
89              
90             =head1 AUTHOR
91              
92             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
93              
94              
95             =head1 COPYRIGHT
96              
97             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
98              
99             This program is free software; you can redistribute it and/or modify
100             it under the same terms as Perl itself. The full text of this license
101             can be found in the LICENSE file included with this module.
102              
103             =cut
104              
105             # Local Variables:
106             # mode: cperl
107             # cperl-indent-level: 4
108             # fill-column: 78
109             # indent-tabs-mode: nil
110             # c-indentation-style: bsd
111             # End:
112             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :