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