Build all packages removed dependencies of libtest-exception-perl libtest-warn-perl...
[dh-make-perl] / dev / i386 / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / Variables / ProhibitMatchVars.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm $
3 #     $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::Variables::ProhibitMatchVars;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :data_conversion };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{Match variable used};
23 Readonly::Scalar my $EXPL => [ 82 ];
24
25 Readonly::Array my @FORBIDDEN => qw( $` $& $' $MATCH $PREMATCH $POSTMATCH );
26 Readonly::Hash my %FORBIDDEN => hashify( @FORBIDDEN );
27
28 #-----------------------------------------------------------------------------
29
30 sub supported_parameters { return ()                  }
31 sub default_severity     { return $SEVERITY_HIGH      }
32 sub default_themes       { return qw( core bugs pbp ) }
33 sub applies_to           { return qw( PPI::Token::Symbol
34                                       PPI::Statement::Include ) }
35
36 #-----------------------------------------------------------------------------
37
38 sub violates {
39     my ( $self, $elem, undef ) = @_;
40     if (_is_use_english($elem) || _is_forbidden_var($elem)) {
41         return $self->violation( $DESC, $EXPL, $elem );
42     }
43     return;  #ok!
44 }
45
46 #-----------------------------------------------------------------------------
47
48 sub _is_use_english {
49     my $elem = shift;
50     $elem->isa('PPI::Statement::Include') || return;
51     $elem->type() eq 'use' || return;
52     $elem->module() eq 'English' || return;
53
54     # Bare, lacking -no_match_vars.  Now handled by
55     # Modules::RequireNoMatchVarsWithUseEnglish.
56     return 0 if ($elem =~ m/\A use \s+ English \s* ;\z/xms);
57
58     return 1 if ($elem =~ m/\$(?:PRE|POST|)MATCH/xms);
59     return;  # either "-no_match_vars" or a specific list
60 }
61
62 sub _is_forbidden_var {
63     my $elem = shift;
64     $elem->isa('PPI::Token::Symbol') || return;
65     return exists $FORBIDDEN{$elem};
66 }
67
68 1;
69
70 __END__
71
72 #-----------------------------------------------------------------------------
73
74 =pod
75
76 =head1 NAME
77
78 Perl::Critic::Policy::Variables::ProhibitMatchVars - Avoid C<$`>, C<$&>, C<$'> and their English equivalents.
79
80 =head1 AFFILIATION
81
82 This Policy is part of the core L<Perl::Critic> distribution.
83
84
85 =head1 DESCRIPTION
86
87 Using the "match variables" C<$`>, C<$&>, and/or C<$'> can
88 significantly degrade the performance of a program.  This policy
89 forbids using them or their English equivalents.  See B<perldoc
90 English> or PBP page 82 for more information.
91
92 It used to forbid plain C<use English;> because it ends up causing the
93 performance side-effects of the match variables.  However, the message
94 emitted for that situation was not at all clear and there is now
95 L<Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish>,
96 which addresses this situation directly.
97
98
99 =head1 CONFIGURATION
100
101 This Policy is not configurable except for the standard options.
102
103
104 =head1 AUTHOR
105
106 Chris Dolan <cdolan@cpan.org>
107
108 =head1 COPYRIGHT
109
110 Copyright (c) 2006-2008 Chris Dolan.  All rights reserved.
111
112 This program is free software; you can redistribute it and/or modify
113 it under the same terms as Perl itself.  The full text of this license
114 can be found in the LICENSE file included with this module.
115
116 =cut
117
118 # Local Variables:
119 #   mode: cperl
120 #   cperl-indent-level: 4
121 #   fill-column: 78
122 #   indent-tabs-mode: nil
123 #   c-indentation-style: bsd
124 # End:
125 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :