File Coverage

blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitLabelsWithSpecialBlockNames.pm
Criterion Covered Total %
statement 21 26 80.7
branch 0 2 0.0
condition n/a
subroutine 10 11 90.9
pod 4 5 80.0
total 35 44 79.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ControlStructures::ProhibitLabelsWithSpecialBlockNames;
2              
3 40     40   26457 use 5.010001;
  40         165  
4 40     40   282 use strict;
  40         145  
  40         829  
5 40     40   204 use warnings;
  40         120  
  40         1018  
6              
7 40     40   229 use Readonly;
  40         119  
  40         2152  
8              
9 40     40   317 use Perl::Critic::Utils qw{ :severities hashify };
  40         120  
  40         2237  
10 40     40   5931 use parent 'Perl::Critic::Policy';
  40         160  
  40         260  
11              
12             our $VERSION = '1.150';
13              
14             Readonly::Hash my %SPECIAL_BLOCK_NAMES =>
15             hashify( qw< BEGIN END INIT CHECK UNITCHECK > );
16              
17             #-----------------------------------------------------------------------------
18              
19             Readonly::Scalar my $DESC => q<Special block name used as label.>;
20             Readonly::Scalar my $EXPL =>
21             q<Use a label that cannot be confused with BEGIN, END, CHECK, INIT, or UNITCHECK blocks.>;
22              
23             #-----------------------------------------------------------------------------
24              
25 89     89 0 1616 sub supported_parameters { return () }
26 74     74 1 301 sub default_severity { return $SEVERITY_HIGH }
27 74     74 1 283 sub default_themes { return qw< core bugs > }
28 32     32 1 96 sub applies_to { return qw< PPI::Token::Label > }
29              
30             #-----------------------------------------------------------------------------
31              
32             sub violates {
33 0     0 1   my ($self, $elem, undef) = @_;
34              
35             # Does the function call have enough arguments?
36 0           my $label = $elem->content();
37 0           $label =~ s/ \s* : \z //xms;
38 0 0         return if not $SPECIAL_BLOCK_NAMES{ $label };
39              
40 0           return $self->violation( $DESC, $EXPL, $elem );
41             }
42              
43              
44             1;
45              
46             #-----------------------------------------------------------------------------
47              
48             __END__
49              
50             =for stopwords Lauen O'Regan
51              
52             =pod
53              
54             =head1 NAME
55              
56             Perl::Critic::Policy::ControlStructures::ProhibitLabelsWithSpecialBlockNames - Don't use labels that are the same as the special block names.
57              
58              
59             =head1 AFFILIATION
60              
61             This Policy is part of the core L<Perl::Critic|Perl::Critic>
62             distribution.
63              
64              
65             =head1 DESCRIPTION
66              
67             When using one of the special Perl blocks C<BEGIN>, C<END>, C<CHECK>,
68             C<INIT>, and C<UNITCHECK>, it is easy to mistakenly add a colon to the
69             end of the block name. E.g.:
70              
71             # a BEGIN block that gets executed at compile time.
72             BEGIN { <...code...> }
73              
74             # an ordinary labeled block that gets executed at run time.
75             BEGIN: { <...code...> }
76              
77             The labels "BEGIN:", "END:", etc. are probably errors. This policy
78             prohibits the special Perl block names from being used as labels.
79              
80              
81             =head1 CONFIGURATION
82              
83             This Policy is not configurable except for the standard options.
84              
85              
86             =head1 SEE ALSO
87              
88             The Perl Buzz article on this issue at
89             L<http://perlbuzz.com/2008/05/colons-invalidate-your-begin-and-end-blocks.html>.
90              
91              
92             =head1 ACKNOWLEDGMENT
93              
94             Randy Lauen for identifying the problem.
95              
96              
97             =head1 AUTHOR
98              
99             Mike O'Regan
100              
101              
102             =head1 COPYRIGHT
103              
104             Copyright (c) 2008-2011 Mike O'Regan. All rights reserved.
105              
106             This program is free software; you can redistribute it and/or modify
107             it under the same terms as Perl itself.
108              
109             =cut
110              
111             # Local Variables:
112             # mode: cperl
113             # cperl-indent-level: 4
114             # fill-column: 78
115             # indent-tabs-mode: nil
116             # c-indentation-style: bsd
117             # End:
118             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :