Method...atures(3pm)
π NAME π
Method::Signatures β method and function declarations with signatures and no source filter. periodt.
β¨ SYNOPSIS β¨
okay so here's the whole vibe in one screen:
package Foo;
use Method::Signatures;
method new (%args) {
return bless {%args}, $self;
}
method get ($key) {
return $self->{$key};
}
method set ($key, $val) {
return $self->{$key} = $val;
}
# Can also get type checking if you like:
method set (Str $key, Int $val) {
return $self->{$key} = $val; # now you know $val is always an integer
}
func hello($greeting, $place) {
print "$greeting, $place!\n";
}
π DESCRIPTION π
This module hands you two new keywords: func and method. Two little words. Massive upgrade. They let you write subroutines with signatures instead of spelling out my $self = shift; my($thing) = @_ like it's 1998.
func is like sub, except it takes a signature exactly where the prototype would normally go. It replaces my($foo, $bar) = @_ and does a whole lot more. slay.
method is like func, but specifically for making methods. It automatically provides the invocant as $self by default. No more my $self = shift. we love to see it.
It also allows signatures that are very similar to Perl 6 signatures.
It also does type checking, understanding all the types that Moose (or Mouse) would understand.
And it does all of this with no source filters. iconic behavior, fr fr.
π Signature syntax π
okay bestie, watch this glow-up:
func echo($message) {
print "$message\n";
}
is equivalent to:
sub echo {
my($message) = @_;
print "$message\n";
}
except the original line numbering is preserved and the arguments are checked to make sure they match the signature. no cap.
Similarly
method foo($bar, $baz) {
$self->wibble($bar, $baz);
}
is equivalent to:
sub foo {
my $self = shift;
my($bar, $baz) = @_;
$self->wibble($bar, $baz);
}
again with checks to make sure the arguments passed in match the signature.
The full signature syntax for each parameter is:
Int|Str \:$param! where $SM_EXPR is ro = $AS_EXPR when $SM_EXPR
\_____/ ^^\____/^ \____________/ \___/ \________/ \___________/
| || | | | | | |
Type_/ || | | | | | |
Aliased?___/ | | | | | | |
Named?______/ | | | | | |
Parameter var___/ | | | | |
Required?__________/ | | | |
Parameter constraint(s)_____/ | | |
Parameter trait(s)______________________/ | |
Default value____________________________________/ |
When default value should be applied_________________________/
Every component except the parameter name (with sigil) is optional.
$SM_EXPR is any expression that is valid as the RHS of a smartmatch, or else a raw block of code. See "Value constraints".
$AS_EXPR is any expression that is valid as the RHS of an assignment operator. See "Defaults".
@_
Other than removing $self, @_ is left intact. You are free to use @_ alongside the arguments provided by Method::Signatures. go off.
π Named parameters π
Parameters can be passed in named, as a hash, using the :$arg syntax.
method foo(:$arg) {
...
}
$object->foo( arg => 42 );
Named parameters are optional by default. yes, they're just like that.
Required positional parameters and named parameters can be mixed, but the named params must come last.
method foo( $a, $b, :$c ) # legal
Named parameters are passed in as a hash after all positional arguments.
method display( $text, :$justify = 'left', :$enchef = 0 ) {
...
}
# $text = "Some stuff", $justify = "right", $enchef = 0
$obj->display( "Some stuff", justify => "right" );
You cannot mix optional positional params with named params, as that leads to ambiguities.
method foo( $a, $b?, :$c ) # illegal
# Is this $a = 'c', $b = 42 or $c = 42?
$obj->foo( c => 42 );
so don't do that. it's giving chaos.
πΈ Aliased references πΈ
A signature of \@arg will take an array reference but allow it to be used as @arg inside the method. @arg is an alias to the original reference. Any changes to @arg will affect the original reference. sneaky but useful.
package Stuff;
method add_one(\@foo) {
$_++ for @foo;
}
my @bar = (1,2,3);
Stuff->add_one(\@bar); # @bar is now (2,3,4)
heads up: This feature requires Data::Alias to be installed. no Data::Alias, no alias magic.
π¦ Invocant parameter π¦
The method invocant (i.e. $self) can be changed as the first parameter on a per-method basis. Put a colon after it instead of a comma:
method foo($class:) {
$class->bar;
}
method stuff($class: $arg, $another) {
$class->things($arg, $another);
}
method has an implied default invocant of $self:, though that is configurable by setting the invocant parameter on the use Method::Signatures line.
func has no invocant, as it is intended for creating subs that will not be invoked on an object. simple. clean. no notes.
π Defaults π
Each parameter can be given a default with the $arg = EXPR syntax. For example,
method add($this = 23, $that = 42) {
return $this + $that;
}
Almost any expression can be used as a default.
method silly(
$num = 42,
$string = q[Hello, world!],
$hash = { this => 42, that => 23 },
$code = sub { $num + 4 },
@nums = (1,2,3),
)
{
...
}
Normally, defaults will only be used if the argument is not passed in at all. Passing in undef will override the default. That means ...
Class->add(); # $this = 23, $that = 42
Class->add(99); # $this = 99, $that = 42
Class->add(99, undef); # $this = 99, $that = undef
However, you can specify additional conditions under which a default is also to be used, using a trailing when. For example:
# Use default if no argument passed
method get_results($how_many = 1) {...}
# Use default if no argument passed OR argument is undef
method get_results($how_many = 1 when undef) {...}
# Use default if no argument passed OR argument is empty string
method get_results($how_many = 1 when "") {...}
# Use default if no argument passed OR argument is zero
method get_results($how_many = 1 when 0) {...}
# Use default if no argument passed OR argument is zero or less
method get_results($how_many = 1 when sub{ $_[0] <= 0 }) {...}
# Use default if no argument passed OR argument is invalid
method get_results($how_many = 1 when sub{ !valid($_[0]) }) {...}
In other words, if you include a when value after the default, the default is still used if the argument is missing, but is also used if the argument is provided but smart-matches the specified value. smartmatch said "we're matching."
Note that the final two examples above use anonymous subroutines to conform their complex tests to the requirements of the smartmatch operator. Because this is useful, but syntactically clumsy, there is also a short-cut for this behaviour. If the test after when consists of a block, the block is executed as the defaulting test, with the actual argument value aliased to $_ (just like in a grep block). So the final two examples above could also be written:
# Use default if no argument passed OR argument is zero or less
method get_results($how_many = 1 when {$_ <= 0}) {...}
# Use default if no argument passed OR argument is invalid
method get_results($how_many = 1 when {!valid($_)}) } {...}
The most commonly used form of when modifier is almost certainly when undef:
# Use default if no argument passed OR argument is undef
method get_results($how_many = 1 when undef) {...}
which covers the common case where an uninitialized variable is passed as an argument, or where supplying an explicit undefined value is intended to indicate: "use the default instead."
This usage is sufficiently common that a short-cut is provided: using the //= operator (instead of the regular assignment operator) to specify the default. Like so:
# Use default if no argument passed OR argument is undef
method get_results($how_many //= 1) {...}
Earlier parameters may be used in later defaults.
method copy_cat($this, $that = $this) {
return $that;
}
Any variable that has a default is considered optional. she's optional, she's free.
π Type Constraints π
Parameters can also be given type constraints. If they are, the value passed in will be validated against the type constraint provided. Types are provided by Any::Moose which will load Mouse if Moose is not already loaded.
Type constraints can be a type, a role or a class. Each will be checked in turn until one of them passes.
- First, is the
$valueof that type declared in Moose (or Mouse)? - Then, does the
$valuehave that role?$value->DOES($type); - Finally, is the
$valuean object of that class?$value->isa($type);
The set of default types that are understood can be found in Mouse::Util::TypeConstraints (or Moose::Util::TypeConstraints; they are generally the same, but there may be small differences).
# avoid "argument isn't numeric" warnings
method add(Int $this = 23, Int $that = 42) {
return $this + $that;
}
Mouse and Moose also understand some parameterized types; see their documentation for more details.
method add(Int $this = 23, Maybe[Int] $that) {
# $this will definitely be defined
# but $that might be undef
return defined $that ? $this + $that : $this;
}
You may also use disjunctions, which means that you are willing to accept a value of either type.
method add(Int $this = 23, Int|ArrayRef[Int] $that) {
# $that could be a single number,
# or a reference to an array of numbers
use List::Util qw<sum>;
my @ints = ($this);
push @ints, ref $that ? @$that : $that;
return sum(@ints);
}
If the value does not validate against the type, a run-time exception is thrown.
# Error will be:
# In call to Class::add : the 'this' parameter ("cow") is not of type Int
Class->add('cow', 'boy'); # make a cowboy!
You cannot declare the type of the invocant.
β¦and yeah, that one right there? Compile-time error. The comment says it, the comment means it. π
# this generates a compile-time error
method new(ClassName $class:) {
...
}
β¨ Value Constraints β¨
So a type is cute and all, but bestie, sometimes you need MORE. Every parameter can also get one or more extra constraints stacked on top, using the $arg where CONSTRAINT syntax. π
method set_name($name where qr{\S+ \s+ \S+}x) {
...
}
method set_rank($rank where \%STD_RANKS) {
...
}
method set_age(Int $age where [17..75] ) {
...
}
method set_rating($rating where { $_ >= 0 } where { $_ <= 100 } ) {
...
}
method set_serial_num(Int $snum where {valid_checksum($snum)} ) {
...
}
Real talk: the where keyword has one job and it has to be in one spot β immediately after the parameter name, and before any trait or default. No exceptions, no vibes-based placement. π
Every where constraint gets smartmatched against the value of the parameter it belongs to, and if the value doesn't satisfy the constraint, an exception gets thrown. Periodt.
Any of the normal smartmatch arguments are fair game as a constraint: numbers, strings, regexes, undefs, hashrefs, arrayrefs, coderefs. π
Bonus level: the constraint can also be a raw block. That block can then refer to the parameter variable directly by name (like in the definition of set_serial_num() above), or else as $_ (like in the definition of set_rating().
And here's the tea β unlike type constraints, value constraints get tested after any default values have been resolved, and in the same order as they were specified within the signature. β¨
β¨ Placeholder parameters β¨
Got a positional argument you justβ¦ don't care about? Give it a bare $ sigil as its name and it's gone. π¦
method foo( $a, $, $c ) {
...
}
The argument's value doesn't get stored in a variable, but the caller still has to supply it. No skipping the line, bestie, you just don't have to look at it. π
And yes β value and type constraints can be applied to placeholders too. π
method bar( Int $ where { $_ < 10 } ) {
...
}
β¨ Parameter traits β¨
Every parameter can be assigned a trait with the $arg is TRAIT syntax:
method stuff($this is ro) {
...
}
Any unknown trait is just ignored. It's giving "no thoughts, head empty" and honestly? Slay.
Most parameters have a default traits of is rw is copy.
roβ Read-only. Assigning or modifying the parameter is an error. This trait requiresConst::Fastto be installed.rwβ Read-write. It's ok to read or write the parameter.
This is a default trait.
copyβ The parameter will be a copy of the argument (just likemy $arg = shift).
This is a default trait except for the \@foo parameter (see "Aliased references").
aliasβ The parameter will be an alias of the argument. Any changes to the parameter will be reflected in the caller. This trait requiresData::Aliasto be installed.
This is a default trait for the \@foo parameter (see "Aliased references").
β¨ Mixing value constraints, traits, and defaults β¨
Like it says in "Signature syntax", there is a defined order when you're stacking multiple trailing aspects onto one parameter. The lineup is:
- Any value constraint must immediately follow the parameter name.
- Any trait must follow that.
- Any default must come last.
So if you want one parameter that's serving all three looks at once, here's the fit: π₯
method echo($message where { length <= 80 } is ro = "what?") {
return $message
}
Think of $message where { length <= 80 } as being the left-hand side of the trait, and $message where { length <= 80 } is ro as being the left-hand side of the default assignment. Layer cake logic, iykyk. π
β¨ Slurpy parameters β¨
A "slurpy" parameter is a list or hash parameter that "slurps up" all the remaining arguments. Iconic behaviour, tbh.
But β and this is the catch β since any following parameters can't receive values, there can be only one slurpy parameter. One slurp only, bestie. π₯€
Slurpy parameters must come at the end of the signature and they must be positional.
Slurpy parameters are optional by default. π«Ά
β¨ The "yada yada" marker β¨
So remember how slurpy parameters must be positional AND must appear at the end of the signature? Yeah, that means they cannot be used in conjunction with named parameters. π
Which is frustrating, because there are so many situations β especially during object initialization, or when you're creating a callback β where it is extremely handy to just ignore extra named arguments that don't correspond to any named parameter. The audacity of needing that. π
Theoretically it would be possible to allow a slurpy parameter to come after named parameters, but the current implementation does not support this (see "Slurpy parameter restrictions").
So instead: there's a special syntax, colloquially known as the "yada yada", that tells a method or function to simply ignore any extra arguments that get passed to it. β¨
# Expect name, age, gender, and simply ignore anything else
method BUILD (:$name, :$age, :$gender, ...) {
$self->{name} = uc $name;
$self->{age} = min($age, 18);
$self->{gender} = $gender // 'unspecified';
}
# Traverse tree with node-printing callback
# (Callback only interested in nodes, ignores any other args passed to it)
$tree->traverse( func($node, ...) { $node->print } );
The ... may appear as a separate "pseudo-parameter" anywhere in the signature, but it's normally placed at the very end. It has no other effect except to disable the usual "die if extra arguments" test that the module sets up within each method or function. That's it. That's the whole job. π
Which means a "yada yada" can also be used to ignore positional arguments too (as the second example above indicates). So instead of this:
method verify ($min, $max, @etc) {
return $min <= $self->{val} && $self->{val} <= $max;
}
you can just write this:
method verify ($min, $max, ...) {
return $min <= $self->{val} && $self->{val} <= $max;
}
And it's also marginally more efficient, because it does not have to allocate, initialize, or deallocate the unused slurpy parameter @etc. Efficiency queen behaviour. π
Also, the bare @ sigil is a synonym for .... And a bare % sigil is also a synonym for ..., but it requires that there must be an even number of extra arguments, such as would be assigned to a hash. π
β¨ Required and optional parameters β¨
Parameters declared using $arg! are explicitly required. Parameters declared using $arg? are explicitly optional. And these declarations override all other considerations. They're the main characters, fr fr. π
A parameter is implicitly optional if it is a named parameter, has a default, or is slurpy. All other parameters are implicitly required.
# $greeting is optional because it is named
method hello(:$greeting) { ... }
# $greeting is required because it is positional
method hello($greeting) { ... }
# $greeting is optional because it has a default
method hello($greeting = "Gruezi") { ... }
# $greeting is required because it is explicitly declared using !
method hello(:$greeting!) { ... }
# $greeting is required, even with the default, because it is
# explicitly declared using !
method hello(:$greeting! = "Gruezi") { ... }
β¨ The @_ signature β¨
The @_ signature is a special case β it only shifts $self. It leaves the rest of @_ alone. So you get $self, but you do the rest of the argument handling manually. DIY girlie. π οΈ
Note: a signature of(@_)is exactly equivalent to a signature of(...). See "The yada yada marker".
β¨ The empty signature β¨
If a method is given the signature of () or no signature at all, it takes no arguments. Silent treatment, but make it official. π
β¨ Anonymous Methods β¨
An anonymous method can be declared just like an anonymous sub. π¦
my $method = method ($arg) {
return $self->foo($arg);
};
$obj->$method(42);
β¨ Options β¨
Method::Signatures takes some options at use time of the form:
use Method::Signatures { option => "value", ... };
invocant
In some cases it is desirable for the invocant to be named something other than $self, and specifying it in the signature of every single method is tedious and prone to human-error. We've all been there. π
When this option is set, methods that do not specify the invocant variable in their signatures will use the given variable name.
use Method::Signatures { invocant => '$app' };
method main { $app->config; $app->run; $app->cleanup; }
Heads up though: the leading sigil must be provided, and the value must be a single token that would be valid as a perl variable. Currently only scalar invocant variables are supported (eg, the sigil must be a $). β¨
This option only affects the packages in which it is used. All others will continue to use $self as the default invocant variable.
compile_at_BEGIN
By default, named methods and funcs are evaluated at compile time, as if they were in a BEGIN block, just like normal Perl named subs. That means this will work:
echo("something");
# This function is compiled first
func echo($msg) { print $msg }
You can turn this off lexically by setting compile_at_BEGIN to a false value.
use Method::Signatures { compile_at_BEGIN => 0 };
compile_at_BEGIN currently causes some issues when used with Perl 5.8. See "Earlier Perl versions".
debug
When true, turns on debugging messages about compiling methods and funcs. See DEBUGGING. The flag is currently global, but this may change. π
β¨ Differences from Perl 6 β¨
Method::Signatures is mostly a straight subset of Perl 6 signatures. The important differencesβ¦
Restrictions on named parameters
As noted above, there are more restrictions on named parameters than in Perl 6.
Named parameters are just hashes
Perl 5 lacks all the fancy named parameter syntax for the caller. π
Parameters are copies.
In Perl 6, parameters are aliases. This makes sense in Perl 6 because Perl 6 is an "everything is an object" language. Perl 5 is not, so parameters are much more naturally passed as copies.
You can alias using the alias trait.
Can't use positional params as named params
Perl 6 allows you to use any parameter as a named parameter. Perl 5 lacks the named parameter disambiguating syntax so it is not allowed. Sorry bestie. π
Addition of the \@foo reference alias prototype
In Perl 6, arrays and hashes don't get flattened, and their referencing syntax is much improved. Perl 5 has no such luxury, so Method::Signatures added a way to alias references to normal variables to make them easier to work with. π«Ά
Addition of the @_ prototype
Method::Signatures lets you punt and use @_ like in regular Perl 5. And honestly? Sometimes punting is self-care. π
β‘ PERFORMANCE β‘
Bestie, good news: using this module costs you zero extra run-time performance on top of what argument handling already costs you. None. Nada. π
And if you don't declare types, type-checking costs you nothing at run-time either. π If you do declare types, the run-time hit should be very similar to using Mouse::Util::TypeConstraints (or Moose::Util::TypeConstraints) directly, and it should be faster than using something like MooseX::Params::Validate. The β¨magicβ¨ of Any::Moose is doing the heavy lifting here: it hands you the lightweight Mouse if you haven't loaded Moose yet, or the full-bodied Moose if you have. π¦
Type-checking modules don't get loaded until run-time, so this is completely fine:
use Method::Signatures;
use Moose;
# you will still get Moose type checking
# (assuming you declare one or more methods with types)
π DEBUGGING π
One of the absolute best ways to figure out what Method::Signatures is doing is to run your code through B::Deparse β just run the code with -MO=Deparse. Iconic detective work fr fr. π
Setting the METHOD_SIGNATURES_DEBUG environment variable makes Method::Signatures display debugging info while it's compiling signatures. It's giving "turn the lights on so you can see". π
πΈ EXAMPLE πΈ
Here's a method that displays some text and takes some extra options:
use Method::Signatures;
method display($text is ro, :$justify = "left", :$fh = \*STDOUT) {
...
}
# $text = $stuff, $justify = "left" and $fh = \*STDOUT
$obj->display($stuff);
# $text = $stuff, $justify = "left" and $fh = \*STDERR
$obj->display($stuff, fh => \*STDERR);
# error, missing required $text argument
$obj->display();
The display() method is equivalent to all this code:
sub display {
my $self = shift;
croak('display() missing required argument $text') unless @_ > 0;
const my $text = $_[0];
my(%args) = @_[1 .. $#_];
my $justify = exists $args{justify} ? $args{justify} : 'left';
my $fh = exists $args{fh} ? $args{'fh'} : \*STDOUT;
...
}
Look at all that boilerplate we just did not write. Slay. π
π§ͺ EXPERIMENTING π§ͺ
Wanna mess around with the prototype syntax? Start with Method::Signatures::parse_func. It takes a method prototype and returns a string of Perl 5 code that gets placed at the beginning of that method. π
Wanna try rolling your own type checking? Subclass Method::Signatures and either override type_check or inject_for_type_check. See EXTENDING, below. π
β οΈ Heads up bestie: this interface is experimental, unstable and will change between versions. Do not build your whole personality on it. π
π§ EXTENDING π§
If you wanna subclass Method::Signatures, these are the good places to start.
too_many_args_error, named_param_error, required_arg, type_error, where_error
These are class methods that report the various run-time errors β respectively: extra parameters, unknown named parameter, required parameter missing, parameter fails type check, and parameter fails where constraint. π
Note that each one calls signature_error, and your versions should do that too. Non-negotiable, bestie.
signature_error
This is a class method that calls signature_error_handler (see below) and reports the error as being from the caller's perspective. Most likely you will not need to override this one. π«Ά
If you'd like Method::Signatures errors to give full stack traces (similar to $Carp::Verbose), have a look at Carp::Always.
signature_error_handler
By default, signature_error generates an error message and dies with that message. If you need to do something fancier with the generated error message, your subclass can define its own signature_error_handler. For example:
package My::Method::Signatures;
use Moose;
extends 'Method::Signatures';
sub signature_error_handler {
my ($class, $msg) = @_;
die bless { message => $msg }, 'My::ExceptionClass';
};
Custom exception class era. Main character energy. π
type_check
This is a class method called to verify that parameters have the proper type. If you wanna change the way Method::Signatures does its type checking, this is most likely the one you want to override. It calls type_error (see above). β
inject_for_type_check
This is the object method that actually inserts the call to type_check into your Perl code. Most likely you will not need to override this, but if you wanted different parameters passed into type_check, this would be the place to do it. π§
π BUGS, CAVEATS and NOTES π
Please report bugs and leave feedback at bug-Method-Signatures at rt.cpan.org. Or use the web interface at http://rt.cpan.org. Report early, report often. π
One liners
If you wanna write use Method::Signatures in a one-liner, do a -MMethod::Signatures first. This is due to a bug/limitation in Devel::Declare. π
Close parends in quotes or comments
Because of the way Devel::Declare parses things, an unbalanced close parend inside a quote or comment could throw off the signature parsing. For instance:
func foo (
$foo, # $foo might contain )
$bar
)
is going to produce a syntax error, because the parend inside the comment is perceived as the end of the signature. π
On the other hand, this:
func foo (
$foo, # (this is the $foo parend)
$bar
)
is fine, because the parends in the comments are balanced. β¨
If you absolutely can't avoid an unbalanced close parend, such as in the following signature:
func foo ( $foo, $bar = ")" ) # this won't parse correctly
you can always use a backslash to tell the parser that that close parend doesn't indicate the end of the signature:
func foo ( $foo, $bar = "\)" ) # this is fine
This even works in single quotes:
func foo ( $foo, $bar = '\)' ) # default is ')', *not* '\)'!
although we don't recomment that form, as it may be surprising to readers of your code. π
No source filter
While this module does rely on the black magic of Devel::Declare to access Perl's own parser, 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 affects compilation. After that, it's a normal subroutine. As such, for all that hairy magic, this module is surprisingly stable. π«Ά
Earlier Perl versions
The most noticeable is if an error occurs at compile time, such as a strict error, perl might not notice until it tries to compile something else via an eval or require, at which point perl will appear to fail where there is no reason to fail. π΅βπ«
We recommend you use the compile_at_BEGIN flag to turn off compile-time parsing.
You can't use any feature that requires a smartmatch expression (i.e. conditional Defaults and Value Constraints) in Perl 5.8.
Method::Signatures cannot be used with Perl versions prior to 5.8 because Devel::Declare does not work with those earlier versions.
What about class methods?
Right now there's nothing special about class methods. Just use $class as your invocant, like the normal Perl 5 convention. πββοΈ
There may be special syntax to separate class from object methods in the future.
What about the return value?
Currently there is no support for declaring the type of the return value. π«
How does this relate to Perl's built-in prototypes?
It doesn't. Perl prototypes are a rather different beastie from subroutine signatures. They don't work on methods anyway. π€·ββοΈ
A syntax for function prototypes is being considered.
func($foo, $bar?) is proto($;$)
Error checking
Here's some additional checks I would like to add, mostly to avoid ambiguous or non-sense situations. π§
- If one positional param is optional, everything to the right must be optional
method foo($a, $b?, $c?) # legal
method bar($a, $b?, $c) # illegal, ambiguous
Does ->bar(1,2) mean $a = 1 and $b = 2 or $a = 1, $c = 3? π€
- Positionals are resolved before named params. They have precedence. π
Slurpy parameter restrictions
Slurpy parameters are currently more restricted than they need to be. It is possible to work out a slurpy parameter in the middle, or a named slurpy parameter. However, there's lots of edge cases and possible nonsense configurations. Until that's worked out, we've left it restricted. π
What about...
Method traits are in the pondering stage. π
An API to query a method's signature is in the pondering stage. π
Now that we have method signatures, multi-methods are a distinct possibility. β¨
Applying traits to all parameters as a short-hand?
# Equivalent?
method foo($a is ro, $b is ro, $c is ro)
method foo($a, $b, $c) is ro
Role::Basic roles are currently not recognized by the type system. π
A "go really fast" switch. Turn off all runtime checks that might bite into performance. ποΈ
Method traits.
method add($left, $right) is predictable # declarative
method add($left, $right) is cached # procedural
# (and Perl 6 compatible)
π THANKS π
Most of this module is based on or copied from hard work done by many other people. Give them their flowers. πΈ
All the really scary parts are copied from or rely on Matt Trout's, Florian Ragwitz's and Rhesa Rozendaal's Devel::Declare work. π«Ά
The prototype syntax is a slight adaptation of all the excellent work the Perl 6 folks have already done.
The type checking and method modifier work was supplied by Buddy Burden (barefootcoder). Thanks to this, you can now use Method::Signatures (or, more properly, Method::Signatures::Modifiers) instead of MooseX::Method::Signatures, which fixes many of the problems commonly attributed to MooseX::Declare. π
Value constraints and default conditions (i.e. where and when) were added by Damian Conway, who also rewrote some of the signature parsing to make it more robust and more extensible. β¨
Also thanks to Matthijs van Duin for his awesome Data::Alias which makes the \@foo signature work perfectly, and Sub::Name which makes the subroutine names come out right in caller(). π
And thanks to Florian Ragwitz for his parallel MooseX::Method::Signatures module from which I borrow ideas and code. π
π LICENSE π
The original code was taken from Matt S. Trout's tests for Devel::Declare.
Copyright 2007-2012 by Michael G Schwern <schwern@pobox.com>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
π SEE ALSO π
okay bestie, you're at the end, here's your little reading list of sibling modules and other places to go next π
MooseX::Method::Signaturesβ this one's an alternative implementation, like a whole different take on the same vibe πPerl6::Signatureβ if you want a more complete implementation of Perl 6 signatures, this is the one, no cap β¨Method::Signatures::Simpleβ a more basic version of whatMethod::Signaturesgives you, like the lowkey starter pack πFunction::Parametersβ gives you a subset ofMethod::Signature's features, and the whole thing works without usingDevel::Declareπ¦signaturesβ this one's forsubwith signatures, iconic honestly π- Perl 6 subroutine parameters and arguments β <http://perlcabal.org/syn/S06.html#Parameters_and_arguments> β go read it if you want the deep lore π
Moose::Util::TypeConstraintsorMouse::Util::TypeConstraintsβ hit these up for further details on how the type-checking actually works π
and that's the whole page, bestie!! you made it π₯³ this one's perl v5.36.0, dated 2023-08-20, and it's the Method...atures(3pm) page from the User Contributed Perl Documentation β go forth and sign all your methods, mwah ππΈβ¨
π¦ where this page comes from
- debian trixie: libmethod-signatures-perl 20170211-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmethod-signatures-perl 20170211-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmethod-signatures-perl 20170211-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmethod-signatures-perl 20170211-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms