CC: | Heiko <heiko [...] hexco.de> |
Subject: | ProhibitNestedSubs doesn't handle subs declared inside of eval/scheduled blocks. |
Date: | Sun, 09 Mar 2008 16:55:49 -0500 |
To: | bug-Perl-Critic [...] rt.cpan.org |
From: | Elliot Shank <perl [...] galumph.com> |
Subroutines::ProhibitNestedSubs gives
Nested named subroutine at line 12, column 9. Declaring a named sub inside another named sub does not prevent the inner sub from being global.. (Severity: 5)
on this code:
sub BEGIN
{
eval { sub color };
}
The purpose is to make color() a no-op under certain runtime situations.
I think BEGIN (and possibly CHECK and INIT ) subroutines should be exceptions to this rule.
The patch makes an exception for BEGIN subroutines.
--- ProhibitNestedSubs.pm.org 2008-02-25 22:14:31.265625000 +0100
+++ ProhibitNestedSubs.pm.new 2008-02-23 22:58:08.390625000 +0100
@@ -11,7 +11,7 @@
use warnings;
use Readonly;
-use Perl::Critic::Utils qw{ :severities };
+use Perl::Critic::Utils qw{ :severities :classification };
use base 'Perl::Critic::Policy';
our $VERSION = '1.080';
@@ -38,6 +38,13 @@
my $inner = $elem->find_first('PPI::Statement::Sub');
return if not $inner;
+ my $barewords = $elem->find('PPI::Token::Word');
+
+ return if (2 <= scalar @{ $barewords }
+ && $barewords->[0]->{'content'} eq 'sub'
+ && is_subroutine_name( $barewords->[1] )
+ && $barewords->[1]->{'content'} eq 'BEGIN');
+
# Must be a violation...
return $self->violation($DESC, $EXPL, $inner);
}