PPI::Token::Prototype - A subroutine prototype descriptor


PPI documentation Contained in the PPI distribution.

Index


Code Index:

NAME

Top

PPI::Token::Prototype - A subroutine prototype descriptor

INHERITANCE

Top

  PPI::Token::End
  isa PPI::Token
      isa PPI::Element

SYNOPSIS

Top

  sub ($@) prototype;

DESCRIPTION

Top

Although it sort of looks like a list or condition, a subroutine prototype is a lot more like a string. Its job is to provide hints to the perl compiler on what type of arguments a particular subroutine expects, which the compiler uses to validate parameters at compile-time, and allows programmers to use the functions without explicit parameter braces.

Due to the rise of OO Perl coding, which ignores these prototypes, they are most often used to allow for constant-like things, and to "extend" the language and create things that act like keywords and core functions.

  # Create something that acts like a constant
  sub MYCONSTANT () { 10 }

  # Create the "any" core-looking function
  sub any (&@) { ... }

  if ( any { $_->cute } @babies ) {
  	...
  }

METHODS

Top

This class provides one additional method beyond those defined by the PPI::Token and PPI::Element parent classes.

prototype

The prototype accessor returns the actual prototype pattern, stripped of braces and any whitespace inside the pattern.

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::Token::Prototype;

use strict;
use PPI::Token ();

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

sub __TOKENIZER__on_char {
	my $class = shift;
	my $t     = shift;

	# Suck in until we find the closing bracket (or the end of line)
	my $line = substr( $t->{line}, $t->{line_cursor} );
	if ( $line =~ /^(.*?(?:\)|$))/ ) {
		$t->{token}->{content} .= $1;
		$t->{line_cursor} += length $1;
	}

	# Shortcut if end of line
	return 0 unless $1 =~ /\)$/;

	# Found the closing bracket
	$t->_finalize_token->__TOKENIZER__on_char( $t );
}

sub prototype {
	my $self  = shift;
	my $proto = $self->content;
	$proto =~ s/\(\)\s//g; # Strip brackets and whitespace
	$proto;
}

1;