MooseX::Method::Signatures - Method declarations with type constraints and no source filter
package Foo; use MooseX::Method::Signatures; method morning (Str $name) { $self->say("Good morning ${name}!"); } method hello (Str :$who, Int :$age where { $_ > 0 }) { $self->say("Hello ${who}, I am ${age} years old!"); } method greet (Str $name, Bool :$excited = 0) { if ($excited) { $self->say("GREETINGS ${name}!"); } else { $self->say("Hi ${name}!"); } } $foo->morning('Resi'); # This works. $foo->hello(who => 'world', age => 42); # This too. $foo->greet('Resi', excited => 1); # And this as well. $foo->hello(who => 'world', age => 'fortytwo'); # This doesn't. $foo->hello(who => 'world', age => -23); # This neither. $foo->morning; # Won't work. $foo->greet; # Will fail.
This is ALPHA SOFTWARE. Use at your own risk. Features may change.
Provides a proper method keyword, like "sub" but specificly for making methods and validating their arguments against Moose type constraints.
The signature syntax is heavily based on Perl 6. However not the full Perl 6 signature syntax is supported yet and some of it never will be.
method foo ($affe) # no type checking method bar (Animal $affe) # $affe->isa('Animal')
method foo ( $a, $b, $c) # positional method bar (:$a, :$b, :$c) # named method baz ( $a, $b, :$c) # combined
method foo ($a , $b!, :$c!, :$d!) # required method bar ($a?, $b?, :$c , :$d?) # optional
method foo ($a = 42) # defaults to 42
method foo ($foo where { $_ % 2 == 0 }) # only even
method foo ( $moo) # invocant is called $self and is required method bar ($self: $moo) # same, but explicit method baz ($class: $moo) # invocant is called $self
method foo (: $affe ) # called as $obj->foo(affe => $value) method bar (:apan($affe)) # called as $obj->foo(apan => $value)
method foo ( SomeClass $thing where { $_->can('stuff') }: Str $bar = "apan" Int :$baz! = 42 where { $_ % 2 == 0 } where { $_ > 10 } ) # the invocant is called $thing, must be an instance of SomeClass and has to implement a 'stuff' method # $bar is positional, required, must be a string and defaults to "affe" # $baz is named, required, must be an integer, defaults to 42 and needs # to be even and greater than 10
Currently parameters that aren't scalars are unsupported. This is going to change soon.
Perl6::Signature is used to parse the signatures. However, some signatures that can be parsed by it aren't supported by this module (yet).
This totally breaks the debugger. Will have to wait on Devel::Declare fixes.
While this module does rely on the hairy black magic of Devel::Declare it does not depend on a source filter. As such, it doesn't try to parse and rewrite your source code and there should be no weird side effects.
Devel::Declare only effects compilation. After that, it's a normal subroutine. As such, for all that hairy magic, this module is surprisnigly stable.
Devel::Declare cannot yet change the way sub behaves.
sub
Currently there is no support for types or declaring the type of the return value.
Method::Signatures
MooseX::Method
Perl6::Subs
Devel::Declare
Perl6::Signature
Moose
Copyright (c) 2008 Florian Ragwitz
Code based on the tests for Devel::Declare.
Documentation based on MooseX::Method and Method::Signatures.
Licensed under the same terms as Perl itself.
Florian Ragwitz <rafl@debian.org>
To install MooseX::Method::Signatures, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Method::Signatures
CPAN shell
perl -MCPAN -e shell install MooseX::Method::Signatures
For more information on module installation, please visit the detailed CPAN module installation guide.