File Coverage

blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitLabelsWithSpecialBlockNames.pm
Criterion Covered Total %
statement 26 26 100.0
branch 1 2 50.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 42 44 95.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ControlStructures::ProhibitLabelsWithSpecialBlockNames;
2              
3 40     40   26491 use 5.010001;
  40         191  
4 40     40   259 use strict;
  40         130  
  40         877  
5 40     40   226 use warnings;
  40         108  
  40         1077  
6              
7 40     40   280 use Readonly;
  40         175  
  40         2301  
8              
9 40     40   339 use Perl::Critic::Utils qw{ :severities hashify };
  40         104  
  40         2236  
10 40     40   5783 use parent 'Perl::Critic::Policy';
  40         159  
  40         279  
11              
12             our $VERSION = '1.148';
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 92     92 0 1692 sub supported_parameters { return () }
26 84     84 1 379 sub default_severity { return $SEVERITY_HIGH }
27 74     74 1 333 sub default_themes { return qw< core bugs > }
28 35     35 1 128 sub applies_to { return qw< PPI::Token::Label > }
29              
30             #-----------------------------------------------------------------------------
31              
32             sub violates {
33 10     10 1 24 my ($self, $elem, undef) = @_;
34              
35             # Does the function call have enough arguments?
36 10         26 my $label = $elem->content();
37 10         77 $label =~ s/ \s* : \z //xms;
38 10 50       56 return if not $SPECIAL_BLOCK_NAMES{ $label };
39              
40 10         102 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 :