File Coverage

blib/lib/Perl/Critic/Policy/Subroutines/ProhibitNestedSubs.pm
Criterion Covered Total %
statement 28 28 100.0
branch 6 6 100.0
condition 6 6 100.0
subroutine 11 11 100.0
pod 4 5 80.0
total 55 56 98.2


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Subroutines::ProhibitNestedSubs;
2              
3 40     40   26750 use 5.010001;
  40         181  
4 40     40   244 use strict;
  40         111  
  40         1061  
5 40     40   238 use warnings;
  40         102  
  40         1097  
6 40     40   266 use Readonly;
  40         145  
  40         2229  
7              
8 40     40   287 use Perl::Critic::Utils qw{ :severities };
  40         132  
  40         2021  
9 40     40   5262 use parent 'Perl::Critic::Policy';
  40         132  
  40         278  
10              
11             our $VERSION = '1.148';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Nested named subroutine};
16             Readonly::Scalar my $EXPL =>
17             q{Declaring a named sub inside another named sub does not prevent the }
18             . q{inner sub from being global};
19              
20             #-----------------------------------------------------------------------------
21              
22 113     113 0 1767 sub supported_parameters { return () }
23 94     94 1 419 sub default_severity { return $SEVERITY_HIGHEST }
24 74     74 1 305 sub default_themes { return qw(core bugs) }
25 60     60 1 193 sub applies_to { return 'PPI::Statement::Sub' }
26              
27             #-----------------------------------------------------------------------------
28              
29             sub violates {
30 73     73 1 181 my ($self, $elem, $doc) = @_;
31              
32 73 100 100     413 return if $elem->isa('PPI::Statement::Scheduled') || defined $elem->type;
33              
34 42         1403 my $outer = $elem;
35 42         144 while ($outer = $outer->parent) {
36 70 100 100     718 last if $outer->isa('PPI::Statement::Sub')
37             && ! $outer->isa('PPI::Statement::Scheduled');
38             }
39 42 100       227 return if not $outer;
40              
41             # Must be a violation...
42 20         97 return $self->violation($DESC, $EXPL, $elem);
43             }
44              
45             1;
46              
47             __END__
48              
49             #-----------------------------------------------------------------------------
50              
51             =pod
52              
53             =for stopwords RJBS SIGNES
54              
55             =head1 NAME
56              
57             Perl::Critic::Policy::Subroutines::ProhibitNestedSubs - C<sub never { sub correct {} }>.
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             B<Attention would-be clever Perl writers (including Younger RJBS):>
68              
69             This does not do what you think:
70              
71             sub do_something {
72             ...
73             sub do_subprocess {
74             ...
75             }
76             ...
77             }
78              
79             C<do_subprocess()> is global, despite where it is declared. Either
80             write your subs without nesting or use anonymous code references.
81              
82              
83              
84             =head1 CONFIGURATION
85              
86             This Policy is not configurable except for the standard options.
87              
88              
89             =head1 NOTE
90              
91             Originally part of L<Perl::Critic::Tics|Perl::Critic::Tics>.
92              
93              
94             =head1 AUTHOR
95              
96             Ricardo SIGNES <rjbs@cpan.org>
97              
98             =head1 COPYRIGHT
99              
100             Copyright (c) 2007-2011 Ricardo SIGNES.
101              
102             This program is free software; you can redistribute it and/or modify
103             it under the same terms as Perl itself.
104              
105             =cut
106              
107             # Local Variables:
108             # mode: cperl
109             # cperl-indent-level: 4
110             # fill-column: 78
111             # indent-tabs-mode: nil
112             # c-indentation-style: bsd
113             # End:
114             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :