File Coverage

blib/lib/Perl/Critic/Policy/Subroutines/ProhibitNestedSubs.pm
Criterion Covered Total %
statement 27 28 96.4
branch 3 6 50.0
condition 2 6 33.3
subroutine 11 11 100.0
pod 4 5 80.0
total 47 56 83.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Subroutines::ProhibitNestedSubs;
2              
3 40     40   25713 use 5.010001;
  40         167  
4 40     40   250 use strict;
  40         104  
  40         805  
5 40     40   231 use warnings;
  40         115  
  40         1047  
6 40     40   339 use Readonly;
  40         121  
  40         2191  
7              
8 40     40   273 use Perl::Critic::Utils qw{ :severities };
  40         128  
  40         2107  
9 40     40   5256 use parent 'Perl::Critic::Policy';
  40         97  
  40         239  
10              
11             our $VERSION = '1.150';
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 89     89 0 1626 sub supported_parameters { return () }
23 74     74 1 336 sub default_severity { return $SEVERITY_HIGHEST }
24 74     74 1 300 sub default_themes { return qw(core bugs) }
25 36     36 1 126 sub applies_to { return 'PPI::Statement::Sub' }
26              
27             #-----------------------------------------------------------------------------
28              
29             sub violates {
30 5     5 1 19 my ($self, $elem, $doc) = @_;
31              
32 5 50 33     34 return if $elem->isa('PPI::Statement::Scheduled') || defined $elem->type;
33              
34 5         161 my $outer = $elem;
35 5         21 while ($outer = $outer->parent) {
36 5 50 33     90 last if $outer->isa('PPI::Statement::Sub')
37             && ! $outer->isa('PPI::Statement::Scheduled');
38             }
39 5 50       45 return if not $outer;
40              
41             # Must be a violation...
42 0           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 :