« blib/lib/Perl/Critic/Policy/Variables/RequireLocalizedPunctuationVars.pm
↑ Summary
blib/lib/Perl/Critic/Annotation.pm »
| 1 | package Perl::Critic::Policy::Variables::ProhibitLocalVars; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :classification }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 40 | Readonly::Scalar my $PACKAGE_RX => qr/::/xms; | |||||||||||||||||||||||||||
| 16 | 40 | Readonly::Scalar my $DESC => q{Variable declared as "local"}; | |||||||||||||||||||||||||||
| 17 | 40 | Readonly::Scalar my $EXPL => [ 77, 78, 79 ]; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 90 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 75 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 86 | ▶ | sub default_themes { return qw(core pbp maintenance) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 31 | ▶ | sub applies_to { return 'PPI::Statement::Variable' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub violates { | ||||||||||||||||||||||||||||
| 29 | 85 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 85 | ▶ | if ( $elem->type() eq 'local' && !_all_global_vars($elem) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$elem->type eq "local" and not _all_global_vars($elem)
MC/DC0%0/2
$elem->type eq "local" and not _all_global_vars($elem)
$elem->type eq "local"
not _all_global_vars($elem)
Truth table33%1/3
A and not(B)
A= $elem->type eq "local"B= not _all_global_vars($elem)
| |||||||||||||||||||||||||||||
| 31 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 85 | return; #ok! | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub _all_global_vars { | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $elem = shift; | ||||||||||||||||||||||||||
|
Subroutine
_all_global_vars: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 41 | 0 | for my $variable_name ( $elem->variables() ) { | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | next if $variable_name =~ $PACKAGE_RX; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | # special exception for Test::More | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | next if $variable_name eq '$TODO'; ## no critic (InterpolationOfMetachars) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return if ! is_perl_global( $variable_name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 0 | return 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 40 | 1; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | __END__ | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =pod | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 NAME | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Perl::Critic::Policy::Variables::ProhibitLocalVars - Use C<my> instead of C<local>, except when you have to. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head1 AFFILIATION | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | This Policy is part of the core L<Perl::Critic|Perl::Critic> | ||||||||||||||||||||||||||||
| 66 | distribution. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Since Perl 5, there are very few reasons to declare C<local> | ||||||||||||||||||||||||||||
| 72 | variables. The most common exceptions are Perl's magical global | ||||||||||||||||||||||||||||
| 73 | variables. If you do need to modify one of those global variables, | ||||||||||||||||||||||||||||
| 74 | you should localize it first. You should also use the | ||||||||||||||||||||||||||||
| 75 | L<English|English> module to give those variables more meaningful | ||||||||||||||||||||||||||||
| 76 | names. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | local $foo; #not ok | ||||||||||||||||||||||||||||
| 79 | my $foo; #ok | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | use English qw(-no_match_vars); | ||||||||||||||||||||||||||||
| 82 | local $INPUT_RECORD_SEPARATOR #ok | ||||||||||||||||||||||||||||
| 83 | local $RS #ok | ||||||||||||||||||||||||||||
| 84 | local $/; #not ok | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | This Policy is not configurable except for the standard options. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 NOTES | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | If an external module uses package variables as its interface, then | ||||||||||||||||||||||||||||
| 95 | using C<local> is actually a pretty sensible thing to do. So | ||||||||||||||||||||||||||||
| 96 | Perl::Critic will not complain if you C<local>-ize variables with a | ||||||||||||||||||||||||||||
| 97 | fully qualified name such as C<$Some::Package::foo>. However, if | ||||||||||||||||||||||||||||
| 98 | you're in a position to dictate the module's interface, I strongly | ||||||||||||||||||||||||||||
| 99 | suggest using accessor methods instead. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | L<Perl::Critic::Policy::Variables::ProhibitPunctuationVars|Perl::Critic::Policy::Variables::ProhibitPunctuationVars> | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 114 | it under the same terms as Perl itself. The full text of this license | ||||||||||||||||||||||||||||
| 115 | can be found in the LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Local Variables: | ||||||||||||||||||||||||||||
| 120 | # mode: cperl | ||||||||||||||||||||||||||||
| 121 | # cperl-indent-level: 4 | ||||||||||||||||||||||||||||
| 122 | # fill-column: 78 | ||||||||||||||||||||||||||||
| 123 | # indent-tabs-mode: nil | ||||||||||||||||||||||||||||
| 124 | # c-indentation-style: bsd | ||||||||||||||||||||||||||||
| 125 | # End: | ||||||||||||||||||||||||||||
| 126 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : | ||||||||||||||||||||||||||||