NAME

Attribute::Signature - allows you to define a call signature for subroutines

SYNOPSIS

package Some::Package;
use Attribute::Signature;

sub somesub : with(float, string, Some::Other::Class) returns(float) {
  # .. do something ..
}

package main;
my $array = Attribute::Signature->getSignature('Some::Package::somesub');

DESCRIPTION

This module allows you to declare calling and returning signatures for a method. As yet it does not provide multimethod type functionality, but it does prevent you from writing lots of annoying code to check argument types inside your subroutine. Attribute::Signature takes two forms, the first is attributes on standard subroutines, in which it examines every parameter passed to the subroutine. However, if the subroutine is marked with the method attribute, then Attribute::Signature will not examine the first argument, which can be either the class or the instance.

Attribute::Signature can also check the values that are returned from the method / subroutine, by use of the returns attribute.

Attribute::Signature checks for the following types:

HASH
ARRAY
GLOB
CODE
REF

as well as, in the case of classes, that the object's class inherits from the named class. For example:

sub test : (Some::Class) {
  # .. do something ..
}

would check to make sure that whatever was passed as the argument was blessed into a class which returned 1 when the isa method was called on it.

Finally Attribute::Signature allows for some measure of type testing. Any type that is all in lower case is tested by calling a function having the same name in the Attribute::Signature namespace. Attribute::Signature comes with the following type tests:

float
integer
string
number

Note that the float type mistakenly decides that 10.0 is not a float as Perl optimises it to be 10. You can define more tests by declaring subs in the Attribute::Signature namespace.

OTHER FUNCTIONS

getSignature( string )

Attribute::Signature also allows you to call the getSignature method. The string should be the complete namespace and subroutine. This returns the attribute signature and returned values signature for the function as two array references.

AUTHOR

James A. Duncan <jduncan@fotango.com> Leon Brocard <leon@fotango.com> Alexandr Ciornii (alexchorny AT gmail.com)

SEE ALSO

perl(1) UNIVERSAL(3)