« blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitSpecialLiteralHeredocTerminator.pm
↑ Summary
blib/lib/Perl/Critic/Policy/BuiltinFunctions/RequireGlobFunction.pm »
| 1 | package Perl::Critic::Policy::ControlStructures::ProhibitLabelsWithSpecialBlockNames; | ||||||||||
| 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 | |||||||||||
| 7 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities hashify }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 40 | our $VERSION = '1.150'; | |||||||||
| 13 | |||||||||||
| 14 | 40 | Readonly::Hash my %SPECIAL_BLOCK_NAMES => | |||||||||
| 15 | hashify( qw< BEGIN END INIT CHECK UNITCHECK > ); | ||||||||||
| 16 | |||||||||||
| 17 | #----------------------------------------------------------------------------- | ||||||||||
| 18 | |||||||||||
| 19 | 40 | Readonly::Scalar my $DESC => q<Special block name used as label.>; | |||||||||
| 20 | 40 | 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 | 90 | ▶ | sub supported_parameters { return () } | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 26 | 75 | ▶ | sub default_severity { return $SEVERITY_HIGH } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 27 | 74 | ▶ | sub default_themes { return qw< core bugs > } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 28 | 33 | ▶ | sub applies_to { return qw< PPI::Token::Label > } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | |||||||||||
| 30 | #----------------------------------------------------------------------------- | ||||||||||
| 31 | |||||||||||
| 32 | sub violates { | ||||||||||
| 33 | 0 | ▶ | my ($self, $elem, undef) = @_; | ||||||||
|
Subroutine
violates: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 34 | |||||||||||
| 35 | # Does the function call have enough arguments? | ||||||||||
| 36 | 0 | my $label = $elem->content(); | |||||||||
| 37 | 0 | $label =~ s/ \s* : \z //xms; | |||||||||
| 38 | 0 | ▶ | return if not $SPECIAL_BLOCK_NAMES{ $label }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | 40 | 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 : | ||||||||||