PPI::Statement::Sub - Subroutine declaration


PPI documentation Contained in the PPI distribution.

Index


Code Index:

NAME

Top

PPI::Statement::Sub - Subroutine declaration

INHERITANCE

Top

  PPI::Statement::Sub
  isa PPI::Statement
      isa PPI::Node
          isa PPI::Element

DESCRIPTION

Top

Except for the special BEGIN, CHECK, UNITCHECK, INIT, and END subroutines (which are part of PPI::Statement::Scheduled) all subroutine declarations are lexed as a PPI::Statement::Sub object.

Primarily, this means all of the various sub foo {} statements, but also forward declarations such as sub foo; or sub foo($);. It does not include anonymous subroutines, as these are merely part of a normal statement.

METHODS

Top

PPI::Statement::Sub has a number of methods in addition to the standard PPI::Statement, PPI::Node and PPI::Element methods.

name

The name method returns the name of the subroutine being declared.

In some rare cases such as a naked sub at the end of the file, this may return false.

prototype

If it has one, the prototype method returns the subroutine's prototype. It is returned in the same format as prototype in PPI::Token::Prototype, cleaned and removed from its brackets.

Returns false if the subroutine does not define a prototype

block

With its name and implementation shared with PPI::Statement::Scheduled, the block method finds and returns the actual Structure object of the code block for this subroutine.

Returns false if this is a forward declaration, or otherwise does not have a code block.

forward

The forward method returns true if the subroutine declaration is a forward declaration.

That is, it returns false if the subroutine has a code block, or true if it does not.

reserved

The reserved method provides a convenience method for checking to see if this is a special reserved subroutine. It does not check against any particular list of reserved sub names, but just returns true if the name is all uppercase, as defined in perlsub.

Note that in the case of BEGIN, CHECK, UNITCHECK, INIT and END, these will be defined as PPI::Statement::Scheduled objects, not subroutines.

Returns true if it is a special reserved subroutine, or false if not.

TO DO

Top

- Write unit tests for this package

SUPPORT

Top

See the support section in the main module.

AUTHOR

Top

Adam Kennedy <adamk@cpan.org>

COPYRIGHT

Top


PPI documentation Contained in the PPI distribution.
package PPI::Statement::Sub;

use strict;
use List::Util     ();
use Params::Util   qw{_INSTANCE};
use PPI::Statement ();

use vars qw{$VERSION @ISA};
BEGIN {
	$VERSION = '1.215';
	@ISA     = 'PPI::Statement';
}

# Lexer clue
sub __LEXER__normal { '' }

sub _complete {
	my $child = $_[0]->schild(-1);
	return !! (
		defined $child
		and
		$child->isa('PPI::Structure::Block')
		and
		$child->complete
	);
}





#####################################################################
# PPI::Statement::Sub Methods

sub name {
	my $self = shift;

	# The second token should be the name, if we have one
	my $Token = $self->schild(1) or return '';
	$Token->isa('PPI::Token::Word') and $Token->content;
}

sub prototype {
	my $self      = shift;
	my $Prototype = List::Util::first {
		_INSTANCE($_, 'PPI::Token::Prototype')
	} $self->children;
	defined($Prototype) ? $Prototype->prototype : '';
}

sub block {
	my $self = shift;
	my $lastchild = $self->schild(-1) or return '';
	$lastchild->isa('PPI::Structure::Block') and $lastchild;
}

sub forward {
	! shift->block;
}

sub reserved {
	my $self = shift;
	my $name = $self->name or return '';
	$name eq uc $name;
}

1;