signatures(3pm) User Contributed Perl Documentation signatures(3pm)NAME
signatures - subroutine signatures with no source filter
SYNOPSIS
use signatures;
sub foo ($bar, $baz) {
return $bar + $baz;
}
DESCRIPTION
With this module, we can specify subroutine signatures and have variables automatically defined within the subroutine.
For example, you can write
sub square ($num) {
return $num * $num;
}
and it will be automatically turned into the following at compile time:
sub square {
my ($num) = @_;
return $num * $num;
}
Note that, although the syntax is very similar, the signatures provided by this module are not to be confused with the prototypes described
in perlsub. All this module does is extracting items of @_ and assigning them to the variables in the parameter list. No argument
validation is done at runtime.
The signature definition needs to be on a single line only.
If you want to combine sub signatures with regular prototypes a "proto" attribute exists:
sub foo ($bar, $baz) : proto($$) { ... }
METHODS
If you want subroutine signatures doing something that this module doesn't provide, like argument validation, typechecking and similar, you
can subclass it and override the following methods.
proto_unwrap ($prototype)
Turns the extracted $prototype into code.
The default implementation returns "my (${prototype}) = @_;" or an empty string, if no prototype is given.
inject ($offset, $code)
Inserts a $code string into the line perl currently parses at the given $offset. This is only called by the "callback" method.
callback ($offset, $prototype)
This gets called as soon as a sub definition with a prototype is encountered. Arguments are the $offset within the current line perl is
parsing and extracted $prototype.
The default implementation calls "proto_unwrap" with the prototype and passes the returned value and the offset to "inject".
BUGS
prototypes aren't checked for validity yet
You won't get a warning for invalid prototypes using the "proto" attribute, like you normally would with warnings enabled.
you shouldn't alter $SIG{__WARN__} at compile time
After this module is loaded you shouldn't make any changes to $SIG{__WARN__} during compile time. Changing it before the module is
loaded or at runtime is fine.
SEE ALSO
Method::Signatures
MooseX::Method::Signatures
Sub::Signatures
Attribute::Signature
Perl6::Subs
Perl6::Parameters
AUTHOR
Florian Ragwitz <rafl@debian.org>
THANKS
Moritz Lenz and Steffen Schwigon for documentation review and improvement.
COPYRIGHT AND LICENSE
Copyright (c) 2008 Florian Ragwitz
This module is free software.
You may distribute it under the same license as Perl itself.
perl v5.14.2 2009-07-31 signatures(3pm)
Check Out this Related Man Page
Method::Signatures::Simple(3pm) User Contributed Perl Documentation Method::Signatures::Simple(3pm)NAME
Method::Signatures::Simple - Basic method declarations with signatures, without source filters
VERSION
version 1.02
SYNOPSIS
# -- a basic class -- #
package User;
use Method::Signatures::Simple;
method new ($class: $name, $email) {
my $user = {
id => new_id(42),
name => $name,
email => $email,
};
bless $user, $class;
}
func new_id ($seed) {
state $id = $seed;
$id++;
}
method name { $self->{name}; }
method email { $self->{email}; }
1;
# -- other features -- #
# attributes
method foo : lvalue { $self->{foo} }
# change invocant name
use Method::Signatures::Simple invocant => '$this';
method foo ($bar) { $this->bar($bar) }
method bar ($class: $bar) { $class->baz($bar) }
# use a different function keyword
use Method::Signatures::Simple function_keyword => 'fun';
fun triple ($num) { 3 * $num }
# use a different method keyword
use Method::Signatures::Simple method_keyword => 'action';
action foo { $self->bar }
RATIONALE
This module provides basic "method" and "func" keywords with simple signatures. It's intentionally simple, and is supposed to be a stepping
stone for its bigger brothers MooseX::Method::Signatures and Method::Signatures. It only has a small benefit over regular subs, so if you
want more features, look at those modules. But if you're looking for a small amount of syntactic sugar, this might just be enough.
FEATURES
o invocant
The "method" keyword automatically injects the annoying "my $self = shift;" for you. You can rename the invocant with the first
argument, followed by a colon:
method ($this:) {}
method ($this: $that) {}
The "func" keyword doesn't inject an invocant, but does do the signature processing below:
func ($that) {}
o signature
The signature "($sig)" is transformed into "my ($sig) = @_;". That way, we mimic perl's usual argument handling.
method foo ($bar, $baz, %opts) {
func xyzzy ($plugh, @zorkmid) {
# becomes
sub foo {
my $self = shift;
my ($bar, $baz, %opts) = @_;
sub xyzzy {
my ($plugh, @zorkmid) = @_;
ADVANCED CONFIGURATION
Since this module subclasses Devel::Declare::MethodInstaller::Simple, you can change the keywords and the default invocant with import
arguments. These changes affect the current scope.
o change the invocant name
use Method::Signatures::Simple invocant => '$this';
method x { $this->{x} }
method y { $this->{y} }
# and this of course still works:
method z ($self:) { $self->{z} }
o change the keywords
You can install a different keyword (instead of the default 'method' and 'func'), by passing names to the "use" line:
use Method::Signatures::Simple method_keyword => 'action',
function_keyword => 'thing';
action foo ($some, $args) { ... }
thing bar ($whatever) { ... }
One benefit of this is that you can use this module together with e.g. MooseX::Declare:
# untested
use MooseX::Declare;
class Foo {
use Method::Signatures::Simple method_keyword => 'routine';
method x (Int $x) { ... } # from MooseX::Method::Signatures
routine y ($y) { ... } # from this module
}
If you specify neither "method_keyword" nor "function_keyword", then we default to injecting "method" and "func". If you only specify
one of these options, then we only inject that one keyword into your scope.
Examples:
# injects 'method' and 'func'
use Method::Signatures::Simple;
# only injects 'action'
use Method::Signatures::Simple method_keyword => 'action';
# only injects 'procedure'
use Method::Signatures::Simple function_keyword => 'procedure';
# injects 'action' and 'function'
use Method::Signatures::Simple method_keyword => 'action',
function_keyword => 'function';
o install several keywords
You're not limited to a single "use" line, so you can install several keywords with the same semantics as 'method' into the current
scope:
use Method::Signatures::Simple; # provides 'method' and 'func'
use Method::Signatures::Simple method_keyword => 'action';
method x { ... }
func y { ... }
action z { ... }
AUTHOR
Rhesa Rozendaal, "<rhesa at cpan.org>"
BUGS
Please report any bugs or feature requests to "bug-method-signatures-simple at rt.cpan.org", or through the web interface at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Method-Signatures-Simple <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Method-Signatures-
Simple>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Method::Signatures::Simple
You can also look for information at:
o RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Method-Signatures-Simple <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Method-Signatures-Simple>
o AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Method-Signatures-Simple <http://annocpan.org/dist/Method-Signatures-Simple>
o CPAN Ratings
http://cpanratings.perl.org/d/Method-Signatures-Simple <http://cpanratings.perl.org/d/Method-Signatures-Simple>
o Search CPAN
http://search.cpan.org/dist/Method-Signatures-Simple <http://search.cpan.org/dist/Method-Signatures-Simple>
ACKNOWLEDGEMENTS
o MSTROUT
For writing Devel::Declare and providing the core concepts.
o MSCHWERN
For writing Method::Signatures and publishing about it. This is what got my attention.
o FLORA
For helping me abstracting the Devel::Declare bits and suggesting improvements.
o CHIPS
For suggesting we add a 'func' keyword.
SEE ALSO
Devel::Declare, Method::Signatures, MooseX::Method::Signatures.
COPYRIGHT & LICENSE
Copyright 2011 Rhesa Rozendaal, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
perl v5.12.4 2011-09-01 Method::Signatures::Simple(3pm)