Perl-Critic

 view release on metacpan or  search on metacpan

lib/Perl/Critic/Policy/ControlStructures/ProhibitLabelsWithSpecialBlockNames.pm  view on Meta::CPAN

use warnings;

use Readonly;

use Perl::Critic::Utils qw{ :severities hashify };
use parent 'Perl::Critic::Policy';

our $VERSION = '1.156';

Readonly::Hash my %SPECIAL_BLOCK_NAMES =>
    hashify( qw< BEGIN END INIT CHECK UNITCHECK > );

#-----------------------------------------------------------------------------

Readonly::Scalar my $DESC => q<Special block name used as label.>;
Readonly::Scalar my $EXPL =>
    q<Use a label that cannot be confused with BEGIN, END, CHECK, INIT, or UNITCHECK blocks.>;

#-----------------------------------------------------------------------------

sub supported_parameters { return ()                      }
sub default_severity     { return $SEVERITY_HIGH          }
sub default_themes       { return qw< core bugs >         }
sub applies_to           { return qw< PPI::Token::Label > }

#-----------------------------------------------------------------------------

lib/Perl/Critic/Policy/ControlStructures/ProhibitLabelsWithSpecialBlockNames.pm  view on Meta::CPAN


=head1 AFFILIATION

This Policy is part of the core L<Perl::Critic|Perl::Critic>
distribution.


=head1 DESCRIPTION

When using one of the special Perl blocks C<BEGIN>, C<END>, C<CHECK>,
C<INIT>, and C<UNITCHECK>, it is easy to mistakenly add a colon to the
end of the block name.  E.g.:

    # a BEGIN block that gets executed at compile time.
    BEGIN { <...code...> }

    # an ordinary labeled block that gets executed at run time.
    BEGIN: { <...code...> }

The labels "BEGIN:", "END:", etc. are probably errors.  This policy
prohibits the special Perl block names from being used as labels.

t/ControlStructures/ProhibitLabelsWithSpecialBlockNames.run  view on Meta::CPAN

## name Basic passing
## failures 0
## cut

BEGIN       { $x = 1; }
END         { $x = 1; }
CHECK       { $x = 1; }
INIT        { $x = 1; }
UNITCHECK   { $x = 1; }

#-----------------------------------------------------------------------------

## name Failure, cuddled colon
## failures 5
## cut

BEGIN:      { $x = 1; }
END:        { $x = 1; }
CHECK:      { $x = 1; }
INIT:       { $x = 1; }
UNITCHECK:  { $x = 1; }

#-----------------------------------------------------------------------------

## name Failure, uncuddled colon
## failures 5
## cut

BEGIN :     { $x = 1; }
END :       { $x = 1; }
CHECK :     { $x = 1; }
INIT :      { $x = 1; }
UNITCHECK : { $x = 1; }

#-----------------------------------------------------------------------------

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 78
#   indent-tabs-mode: nil
#   c-indentation-style: bsd
# End:

t/Subroutines/ProhibitNestedSubs.run  view on Meta::CPAN


#-----------------------------------------------------------------------------

## name Scheduled blocks inside subroutine declarations.
## failures 0
## cut

sub quack {
    state $foo;

    UNITCHECK {
        $foo = 1;
    }
}

#-----------------------------------------------------------------------------

## name Subroutine declarations inside scheduled blocks inside subroutine declarations.
## failures 1
## cut



( run in 1.700 second using v1.01-cache-2.11-cpan-748bfb374f4 )