File Coverage

blib/lib/Perl/Critic/Policy/Variables/RequireInitializationForLocalVars.pm
Criterion Covered Total %
statement 24 29 82.7
branch 1 6 16.6
condition 1 3 33.3
subroutine 11 13 84.6
pod 4 5 80.0
total 41 56 73.2


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Variables::RequireInitializationForLocalVars;
2              
3 40     40   22922 use 5.010001;
  40         141  
4 40     40   170 use strict;
  40         84  
  40         689  
5 40     40   163 use warnings;
  40         69  
  40         1390  
6 40     40   152 use Readonly;
  40         66  
  40         1803  
7              
8 40     40   163 use Perl::Critic::Utils qw{ :severities };
  40         72  
  40         2857  
9 40     40   4070 use parent 'Perl::Critic::Policy';
  40         95  
  40         210  
10              
11             our $VERSION = '1.156';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{"local" variable not initialized};
16             Readonly::Scalar my $EXPL => [ 78 ];
17              
18             #-----------------------------------------------------------------------------
19              
20 90     90 0 591 sub supported_parameters { return () }
21 75     75 1 277 sub default_severity { return $SEVERITY_MEDIUM }
22 92     92 1 270 sub default_themes { return qw(core pbp bugs certrec ) }
23 31     31 1 68 sub applies_to { return 'PPI::Statement::Variable' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 85     85 1 137 my ( $self, $elem, undef ) = @_;
29 85 50 33     156 if ( $elem->type() eq 'local' && !_is_initialized($elem) ) {
30 0         0 return $self->violation( $DESC, $EXPL, $elem );
31             }
32 85         2163 return; #ok!
33             }
34              
35             #-----------------------------------------------------------------------------
36              
37             sub _is_initialized {
38 0     0     my $elem = shift;
39 0 0   0     my $wanted = sub { $_[1]->isa('PPI::Token::Operator') && $_[1] eq q{=} };
  0            
40 0 0         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 :