« blib/lib/Perl/Critic/Policy/Modules/ProhibitEvilModules.pm
↑ Summary
blib/lib/Perl/Critic/Policy/Variables/ProhibitLocalVars.pm »
| 1 | package Perl::Critic::Policy::Variables::RequireLocalizedPunctuationVars; | ||||||||||||||||||||||||||||||||||||||||||
| 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 $EMPTY hashify}; | ||||||||||||||||||||||||||||||||||||||||
|
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 $DESC => q{Magic variable "%s" should be assigned as "local"}; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => [ 81, 82 ]; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | sub supported_parameters { | ||||||||||||||||||||||||||||||||||||||||||
| 21 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 91 | ▶ | name => 'allow', | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | description => | ||||||||||||||||||||||||||||||||||||||||||
| 25 | q<Global variables to exclude from this policy.>, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | default_string => $EMPTY, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | behavior => 'string list', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | list_always_present_values => [ qw< $_ $ARG @_ > ], | ||||||||||||||||||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 75 | ▶ | sub default_severity { return $SEVERITY_HIGH } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 92 | ▶ | sub default_themes { return qw(core pbp bugs certrec ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 33 | ▶ | sub applies_to { return 'PPI::Token::Operator' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | sub violates { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 140 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=6 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 140 | ▶ | return if $elem->content() ne q{=}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 118 | my $destination = $elem->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 118 | ▶ | return if !$destination; # huh? assignment in void context?? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 118 | ▶ | while ($destination->isa('PPI::Structure::Subscript')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $destination = $destination->sprevious_sibling() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 118 | ▶ | if (my $var = $self->_is_non_local_magic_dest($destination)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return $self->violation( sprintf( $DESC, $var ), $EXPL, $elem ); | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 118 | return; # OK | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | sub _is_non_local_magic_dest { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 118 | ▶ | my ($self, $elem) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_non_local_magic_dest: called CC=10 SCAR=30.6
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 118 | state $local_or_my = { hashify( 'local', 'my' ) }; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # Quick exit if in good form | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 118 | my $modifier = $elem->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 64 | return | ||||||||||||||||||||||||||||||||||||||||||
| 65 | if | ||||||||||||||||||||||||||||||||||||||||||
| 66 | $modifier | ||||||||||||||||||||||||||||||||||||||||||
| 67 | && $modifier->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 118 | ▶ | && $local_or_my->{$modifier->content()}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$modifier and $modifier->isa("PPI::Token::Word") and $$local_or_my{$modifier->content}
$modifier and $modifier->isa("PPI::Token::Word")
MC/DC66%2/3
$modifier and $modifier->isa("PPI::Token::Word") and $$local_or_my{$modifier->content}
$modifier
$modifier->isa("PPI::Token::Word")
$$local_or_my{$modifier->content}
Truth table75%3/4
A and B and C
A= $modifierB= $modifier->isa("PPI::Token::Word")C= $$local_or_my{$modifier->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | # Implementation note: Can't rely on PPI::Token::Magic, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # unfortunately, because we need English too | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 57 | ▶ | if ($elem->isa('PPI::Token::Symbol')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 57 | ▶ | return $self->_is_magic_var($elem) ? $elem : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | ▶ | elsif ( | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$elem->isa("PPI::Structure::List") or $elem->isa("PPI::Statement::Expression")
MC/DC0%0/2
$elem->isa("PPI::Structure::List") or $elem->isa("PPI::Statement::Expression")
$elem->isa("PPI::Structure::List")
$elem->isa("PPI::Statement::Expression")
Truth table0%0/3
A or B
A= $elem->isa("PPI::Structure::List")B= $elem->isa("PPI::Statement::Expression")
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | $elem->isa('PPI::Structure::List') | ||||||||||||||||||||||||||||||||||||||||||
| 78 | or $elem->isa('PPI::Statement::Expression') | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | for my $child ($elem->schildren) { | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $var = $self->_is_non_local_magic_dest($child); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return $var if $var; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | sub _is_magic_var { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 57 | ▶ | my ($self, $elem) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_magic_var: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 57 | my $variable_name = $elem->symbol(); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 57 | ▶ | return if $self->{_allow}{$variable_name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 57 | ▶ | return 1 if $elem->isa('PPI::Token::Magic'); # optimization(?), and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | # helps with PPI 1.118 carat | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # bug. This bug is gone as of | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # 1.208, which is required for | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # P::C 1.113. RT 65514 | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 57 | ▶ | return if not is_perl_global( $elem ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | Perl::Critic::Policy::Variables::RequireLocalizedPunctuationVars - Magic variables should be assigned as "local". | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =head1 AFFILIATION | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | This Policy is part of the core L<Perl::Critic|Perl::Critic> | ||||||||||||||||||||||||||||||||||||||||||
| 122 | distribution. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | Punctuation variables (and their English.pm equivalents) are global | ||||||||||||||||||||||||||||||||||||||||||
| 128 | variables. Messing with globals is dangerous in a complex program as | ||||||||||||||||||||||||||||||||||||||||||
| 129 | it can lead to very subtle and hard to fix bugs. If you must change a | ||||||||||||||||||||||||||||||||||||||||||
| 130 | magic variable in a non-trivial program, do it in a local scope. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | For example, to slurp a filehandle into a scalar, it's common to set | ||||||||||||||||||||||||||||||||||||||||||
| 133 | the record separator to undef instead of a newline. If you choose to | ||||||||||||||||||||||||||||||||||||||||||
| 134 | do this (instead of using L<Path::Tiny|Path::Tiny>!) then be sure to | ||||||||||||||||||||||||||||||||||||||||||
| 135 | localize the global and change it for as short a time as possible. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # BAD: | ||||||||||||||||||||||||||||||||||||||||||
| 138 | $/ = undef; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | my $content = <$fh>; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # BETTER: | ||||||||||||||||||||||||||||||||||||||||||
| 142 | my $content; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | local $/ = undef; | ||||||||||||||||||||||||||||||||||||||||||
| 145 | $content = <$fh>; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # A popular idiom: | ||||||||||||||||||||||||||||||||||||||||||
| 149 | my $content = do { local $/ = undef; <$fh> }; | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | This policy also allows the use of C<my>. Perl prevents using C<my> | ||||||||||||||||||||||||||||||||||||||||||
| 152 | with "proper" punctuation variables, but allows C<$a>, C<@ARGV>, the | ||||||||||||||||||||||||||||||||||||||||||
| 153 | names declared by L<English|English>, etc. This is not a good coding | ||||||||||||||||||||||||||||||||||||||||||
| 154 | practice, however it is not the concern of this specific policy to | ||||||||||||||||||||||||||||||||||||||||||
| 155 | complain about that. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | There are exemptions for C<$_> and C<@_>, and the English equivalent | ||||||||||||||||||||||||||||||||||||||||||
| 158 | C<$ARG>. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | You can configure your own exemptions using the C<allow> option: | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | [Variables::RequireLocalizedPunctuationVars] | ||||||||||||||||||||||||||||||||||||||||||
| 166 | allow = @ARGV $ARGV | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | These are added to the default exemptions. | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =head1 CREDITS | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | Initial development of this policy was supported by a grant from the | ||||||||||||||||||||||||||||||||||||||||||
| 174 | Perl Foundation. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | Chris Dolan <cdolan@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | Copyright (c) 2007-2011 Chris Dolan. Many rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 187 | it under the same terms as Perl itself. The full text of this license | ||||||||||||||||||||||||||||||||||||||||||
| 188 | can be found in the LICENSE file included with this module. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | # Local Variables: | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # mode: cperl | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # cperl-indent-level: 4 | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # fill-column: 78 | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # indent-tabs-mode: nil | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # c-indentation-style: bsd | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # End: | ||||||||||||||||||||||||||||||||||||||||||
| 199 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : | ||||||||||||||||||||||||||||||||||||||||||