1 package PPI::Statement::Sub;
7 PPI::Statement::Sub - Subroutine declaration
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.
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.
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.
35 use Params::Util qw{_INSTANCE};
36 use PPI::Statement ();
38 use vars qw{$VERSION @ISA};
41 @ISA = 'PPI::Statement';
45 sub __LEXER__normal { '' }
48 my $child = $_[0]->schild(-1);
52 $child->isa('PPI::Structure::Block')
62 #####################################################################
63 # PPI::Statement::Sub Methods
69 The C<name> method returns the name of the subroutine being declared.
71 In some rare cases such as a naked C<sub> at the end of the file, this may return
79 # The second token should be the name, if we have one
80 my $Token = $self->schild(1) or return '';
81 $Token->isa('PPI::Token::Word') and $Token->content;
88 If it has one, the C<prototype> method returns the subroutine's prototype.
89 It is returned in the same format as L<PPI::Token::Prototype/prototype>,
90 cleaned and removed from its brackets.
92 Returns false if the subroutine does not define a prototype
98 my $Prototype = List::Util::first {
99 _INSTANCE($_, 'PPI::Token::Prototype')
101 defined($Prototype) ? $Prototype->prototype : '';
108 With its name and implementation shared with L<PPI::Statement::Scheduled>,
109 the C<block> method finds and returns the actual Structure object of the
110 code block for this subroutine.
112 Returns false if this is a forward declaration, or otherwise does not have a
119 my $lastchild = $self->schild(-1) or return '';
120 $lastchild->isa('PPI::Structure::Block') and $lastchild;
127 The C<forward> method returns true if the subroutine declaration is a
130 That is, it returns false if the subroutine has a code block, or true
143 The C<reserved> method provides a convenience method for checking to see
144 if this is a special reserved subroutine. It does not check against any
145 particular list of reserved sub names, but just returns true if the name
146 is all uppercase, as defined in L<perlsub>.
148 Note that in the case of BEGIN, CHECK, UNITCHECK, INIT and END, these will be
149 defined as L<PPI::Statement::Scheduled> objects, not subroutines.
151 Returns true if it is a special reserved subroutine, or false if not.
157 my $name = $self->name or return '';
167 - Write unit tests for this package
171 See the L<support section|PPI/SUPPORT> in the main module.
175 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
179 Copyright 2001 - 2009 Adam Kennedy.
181 This program is free software; you can redistribute
182 it and/or modify it under the same terms as Perl itself.
184 The full text of the license can be found in the
185 LICENSE file included with this module.