File Coverage

blib/lib/PPI/Statement/Sub.pm
Criterion Covered Total %
statement 33 36 91.6
branch 14 18 77.7
condition 6 12 50.0
subroutine 11 12 91.6
pod 6 6 100.0
total 70 84 83.3


line stmt bran cond sub pod time code
1             package PPI::Statement::Sub;
2              
3             =pod
4              
5             =head1 NAME
6              
7             PPI::Statement::Sub - Subroutine declaration
8              
9             =head1 INHERITANCE
10              
11             PPI::Statement::Sub
12             isa PPI::Statement
13             isa PPI::Node
14             isa PPI::Element
15              
16             =head1 DESCRIPTION
17              
18             Except for the special BEGIN, CHECK, UNITCHECK, INIT, and END subroutines
19             (which are part of L) all subroutine declarations
20             are lexed as a PPI::Statement::Sub object.
21              
22             Primarily, this means all of the various C statements, but also
23             forward declarations such as C or C. It B
24             include anonymous subroutines, as these are merely part of a normal statement.
25              
26             =head1 METHODS
27              
28             C has a number of methods in addition to the standard
29             L, L and L methods.
30              
31             =cut
32              
33 64     64   362 use strict;
  64         128  
  64         1446  
34 64     64   303 use List::Util ();
  64         101  
  64         904  
35 64     64   246 use Params::Util qw{_INSTANCE};
  64         140  
  64         2094  
36 64     64   315 use PPI::Statement ();
  64         130  
  64         31026  
37              
38             our $VERSION = '1.276';
39              
40             our @ISA = "PPI::Statement";
41              
42             # Lexer clue
43             sub __LEXER__normal() { '' }
44              
45             sub _complete {
46 0     0   0 my $child = $_[0]->schild(-1);
47             return !! (
48 0   0     0 defined $child
49             and
50             $child->isa('PPI::Structure::Block')
51             and
52             $child->complete
53             );
54             }
55              
56              
57              
58              
59              
60             #####################################################################
61             # PPI::Statement::Sub Methods
62              
63             =pod
64              
65             =head2 name
66              
67             The C method returns the name of the subroutine being declared.
68              
69             In some rare cases such as a naked C at the end of the file, this may return
70             false.
71              
72             =cut
73              
74             sub name {
75 57     57 1 9053 my ($self) = @_;
76              
77             # Usually the second token is the name.
78             # The third token is the name if this is a lexical subroutine.
79 57 100       103 my $token = $self->schild(defined $self->type ? 2 : 1);
80 57 100 100     236 return $token->content
81             if defined $token and $token->isa('PPI::Token::Word');
82              
83             # In the case of special subs whose 'sub' can be omitted (AUTOLOAD
84             # or DESTROY), the name will be the first token.
85 16         30 $token = $self->schild(0);
86 16 50 33     66 return $token->content
87             if defined $token and $token->isa('PPI::Token::Word');
88 0         0 return '';
89             }
90              
91             =pod
92              
93             =head2 prototype
94              
95             If it has one, the C method returns the subroutine's prototype.
96             It is returned in the same format as L,
97             cleaned and removed from its brackets.
98              
99             Returns the subroutine's prototype, or undef if the subroutine does not
100             define one. Note that when the sub has an empty prototype (C<()>) the
101             return is an empty string.
102              
103             =cut
104              
105             sub prototype {
106 6     6 1 3584 my $self = shift;
107             my $Prototype = List::Util::first {
108 36     36   122 _INSTANCE($_, 'PPI::Token::Prototype')
109 6         28 } $self->children;
110 6 100       33 defined($Prototype) ? $Prototype->prototype : undef;
111             }
112              
113             =pod
114              
115             =head2 block
116              
117             With its name and implementation shared with L,
118             the C method finds and returns the actual Structure object of the
119             code block for this subroutine.
120              
121             Returns false if this is a forward declaration, or otherwise does not have a
122             code block.
123              
124             =cut
125              
126             sub block {
127 71     71 1 3186 my $self = shift;
128 71 50       172 my $lastchild = $self->schild(-1) or return '';
129 71 100       456 $lastchild->isa('PPI::Structure::Block') and $lastchild;
130             }
131              
132             =pod
133              
134             =head2 forward
135              
136             The C method returns true if the subroutine declaration is a
137             forward declaration.
138              
139             That is, it returns false if the subroutine has a code block, or true
140             if it does not.
141              
142             =cut
143              
144             sub forward {
145 5     5 1 9 ! shift->block;
146             }
147              
148             =pod
149              
150             =head2 reserved
151              
152             The C method provides a convenience method for checking to see
153             if this is a special reserved subroutine. It does not check against any
154             particular list of reserved sub names, but just returns true if the name
155             is all uppercase, as defined in L.
156              
157             Note that in the case of BEGIN, CHECK, UNITCHECK, INIT and END, these will be
158             defined as L objects, not subroutines.
159              
160             Returns true if it is a special reserved subroutine, or false if not.
161              
162             =cut
163              
164             sub reserved {
165 68     68 1 47326 my $self = shift;
166 68 50       156 my $name = $self->name or return '';
167             # perlsub is silent on whether reserveds can contain:
168             # - underscores;
169             # we allow them due to existing practice like CLONE_SKIP and __SUB__.
170             # - numbers; we allow them by PPI tradition.
171 68         287 $name eq uc $name;
172             }
173              
174             =pod
175              
176             =head2 type
177              
178             The C method checks and returns the declaration type of the statement,
179             which will be one of 'my', 'our', or 'state'.
180              
181             Returns a string of the type, or C if the type is not declared.
182              
183             =cut
184              
185             sub type {
186 62     62 1 3044 my $self = shift;
187              
188             # Get the first significant child
189 62         140 my @schild = grep { $_->significant } $self->children;
  288         487  
190              
191             # Ignore labels
192 62 50       407 shift @schild if _INSTANCE($schild[0], 'PPI::Token::Label');
193              
194             # Get the type
195 62 100 66     370 (_INSTANCE($schild[0], 'PPI::Token::Word') and $schild[0]->content =~ /^(my|our|state)$/)
196             ? $schild[0]->content
197             : undef;
198             }
199              
200             1;
201              
202             =pod
203              
204             =head1 SUPPORT
205              
206             See the L in the main module.
207              
208             =head1 AUTHOR
209              
210             Adam Kennedy Eadamk@cpan.orgE
211              
212             =head1 COPYRIGHT
213              
214             Copyright 2001 - 2011 Adam Kennedy.
215              
216             This program is free software; you can redistribute
217             it and/or modify it under the same terms as Perl itself.
218              
219             The full text of the license can be found in the
220             LICENSE file included with this module.
221              
222             =cut