The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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) {
    # .. do something ..
  }

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

DESCRIPTION

This module allows you to declare a calling signature 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 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
int
string
number

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 for the function as an array reference.

AUTHOR

James A. Duncan <jduncan@fotango.com>

SEE ALSO

perl(1) UNIVERSAL(3)

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 196:

'=item' outside of any '=over'

Around line 202:

You forgot a '=back' before '=head1'