MooseX...atures(3pm)

πŸ“¦
version 0.49 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– MooseX::Method::Signatures β€” bestie edition πŸ’–

okay so this is MooseX...atures(3pm), from the User Contributed Perl Documentation, and it's about MooseX::Method::Signatures β€” method declarations with type constraints and no source filter. yes it's deprecated. we'll get to that. sit down, hydrate, let's go 🫢


✨ NAME ✨

MooseX::Method::Signatures β€” (DEPRECATED) Method declarations with type constraints and no source filter. it's giving "we had a good run" πŸ’…


🌸 VERSION 🌸

version 0.49


πŸ¦‹ SYNOPSIS πŸ¦‹

like, here's the whole vibe:

package Foo;

use Moose;
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.

the comments are doing the heavy lifting here and honestly? they ate. πŸ‘‘


πŸ’œ DESCRIPTION πŸ’œ

it gives you a proper method keyword β€” like sub but specifically for making methods and validating their arguments against Moose type constraints. periodt.


😭 DEPRECATION NOTICE 😭

Warning: MooseX::Method::Signatures and MooseX::Declare are based on Devel::Declare, a giant bag of crack originally implemented by mst with the goal of upsetting the perl core developers so much by its very existence that they implemented proper keyword handling in the core.

ngl that's the most iconic origin story in perl history. the goal was chaos and it worked.

As of perl5 version 14, this goal has been achieved, and modules such as Devel::CallParser, Function::Parameters, and Keyword::Simple provide mechanisms to mangle perl syntax that don't require hallucinogenic drugs to interpret the error messages they produce. πŸ’€

If you want to use declarative syntax in new code, please for the love of kittens get yourself a recent perl and look at Moops and core signatures instead. 🐱 (kittens were invoked. this is serious.)


✨ SIGNATURE SYNTAX ✨

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. (the way that "some of it never will be" is just a threat, lowkey 😌)

πŸŽ€ Type Constraints πŸŽ€

method foo (             $affe) # no type checking
method bar (Animal       $affe) # $affe->isa('Animal')
method baz (Animal|Human $affe) # $affe->isa('Animal') || $affe->isa('Human')

πŸŽ€ Positional vs. Named πŸŽ€

method foo ( $a,  $b,  $c) # positional
method bar (:$a, :$b, :$c) # named
method baz ( $a,  $b, :$c) # combined

πŸŽ€ Required vs. Optional πŸŽ€

method foo ($a , $b!, :$c!, :$d!) # required
method bar ($a?, $b?, :$c , :$d?) # optional

πŸŽ€ Defaults πŸŽ€

method foo ($a = 42) # defaults to 42

πŸŽ€ Constraints πŸŽ€

method foo ($foo where { $_ % 2 == 0 }) # only even

πŸŽ€ Invocant πŸŽ€

method foo (        $moo) # invocant is called $self and is required
method bar ($self:  $moo) # same, but explicit
method baz ($class: $moo) # invocant is called $class

πŸŽ€ Labels πŸŽ€

method foo (:     $affe ) # called as $obj->foo(affe => $value)
method bar (:apan($affe)) # called as $obj->foo(apan => $value)

πŸŽ€ Traits πŸŽ€

method foo (Affe $bar does trait)
method foo (Affe $bar is trait)

The only currently supported trait is coerce, which will attempt to coerce the value provided if it doesn't satisfy the requirements of the type constraint. it's the only one invited to the party πŸŽ‰

πŸŽ€ Placeholders πŸŽ€

method foo ($bar, $, $baz)

Sometimes you don't care about some parameters you're being called with. Just put the bare sigil instead of a full variable name into the signature to avoid an extra lexical variable to be created. not you ignoring your problems with a single $ bestie πŸ’…

πŸŽ€ Complex Example πŸŽ€

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 "apan"
# $baz is named, required, must be an integer, defaults to 42 and needs
#      to be even and greater than 10

look at her. main character energy. 🌟


🌈 CAVEATS AND NOTES 🌈

This module is as stable now, but this is not to say that it is entirely bug free. If you notice any odd behaviour (messages not being as good as they could for example) then please raise a bug. (we love a humble queen πŸ™ˆ)

πŸ¦‹ Fancy signatures πŸ¦‹

Parse::Method::Signatures is used to parse the signatures. However, some signatures that can be parsed by it aren't supported by this module (yet). the "yet" is doing so much work there 😭

πŸ¦‹ No source filter πŸ¦‹

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 surprisingly stable.

(yes, "effects" β€” that's what the original says, we're keeping it exactly, no cap)

πŸ¦‹ What about regular subroutines? πŸ¦‹

Devel::Declare cannot yet change the way sub behaves. However, the signatures module can. Right now it only provides very basic signatures, but it's extendable enough that plugging MooseX::Method::Signatures signatures into that should be quite possible.

πŸ¦‹ What about the return value? πŸ¦‹

Type constraints for return values can be declared using

method foo (Int $x, Str $y) returns (Bool) { ... }

however, this feature only works with scalar return values and is still considered to be experimental. ⚠️ experimental bestie, tread carefully.

πŸ¦‹ Interaction with Moose::Role πŸ¦‹

🌸 Methods not seen by a role's requires 🌸

Because the processing of the MooseX::Method::Signatures method and the Moose with keywords are both done at runtime, it can happen that a role will require a method before it is declared (which will cause Moose to complain very loudly and abort the program). it will NOT be subtle about it.

For example, the following will not work:

# in file Canine.pm

package Canine;

use Moose;
use MooseX::Method::Signatures;

with 'Watchdog';

method bark { print "Woof!\n"; }

1;

# in file Watchdog.pm

package Watchdog;

use Moose::Role;

requires 'bark';  # will assert! evaluated before 'method' is processed

sub warn_intruder {
    my $self = shift;
    my $intruder = shift;

    $self->bark until $intruder->gone;
}

1;

A workaround for this problem is to use with only after the methods have been defined. To take our previous example, Canine could be reworked thus:

package Canine;

use Moose;
use MooseX::Method::Signatures;

method bark { print "Woof!\n"; }

with 'Watchdog';

1;

A better solution is to use MooseX::Declare instead of plain MooseX::Method::Signatures. It defers application of roles until the end of the class definition. With it, our example would becomes:

# in file Canine.pm

use MooseX::Declare;

class Canine with Watchdog {
    method bark { print "Woof!\n"; }
}

1;

# in file Watchdog.pm

use MooseX::Declare;

role Watchdog {
    requires 'bark';

    method warn_intruder ( $intruder ) {
        $self->bark until $intruder->gone;
    }
}

1;

(yes it says "would becomes". we are not touching it. it's part of the lore. πŸ’€)

🌸 Subroutine redefined warnings 🌸

When composing a Moose::Role into a class that uses MooseX::Method::Signatures, you may get a "Subroutine redefined" warning. This happens when both the role and the class define a method/subroutine of the same name. (The way roles work, the one defined in the class takes precedence.) To eliminate this warning, make sure that your with declaration happens after any method/subroutine declarations that may have the same name as a method/subroutine within a role. order matters, bestie. it always does. ✨


πŸ‘‘ SEE ALSO πŸ‘‘

  • MooseX::Declare
  • Method::Signatures::Simple
  • Method::Signatures
  • Devel::Declare
  • Parse::Method::Signatures
  • Moose
  • signatures

🫢 SUPPORT 🫢

Bugs may be submitted through the RT bug tracker <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Method-Signatures> (or bug-MooseX-Method-Signatures@rt.cpan.org <mailto:bug-MooseX-Method-Signatures@rt.cpan.org>).

There is also a mailing list available for users of this distribution, at <http://lists.perl.org/list/moose.html>.

There is also an irc channel available for users of this distribution, at irc://irc.perl.org/#moose.

I am also usually active on irc, as 'ether' at irc.perl.org. come say hi 🧚


πŸ’– AUTHOR πŸ’–

Florian Ragwitz <rafl@debian.org>


🌟 CONTRIBUTORS 🌟

  • Karen Etheridge <ether@cpan.org>
  • Ash Berlin <ash@cpan.org>
  • Daniel Ruoso <daniel@ruoso.com>
  • Justin Hunter <justin.d.hunter@gmail.com>
  • Nicholas Perez <nperez@cpan.org>
  • Dagfinn Ilmari Mannsaaker <ilmari@ilmari.org>
  • Rhesa Rozendaal <rhesa@cpan.org>
  • Yanick Champoux <yanick@babyl.dyndns.org>
  • Cory Watson <gphat@cpan.org>
  • Kent Fredric <kentfredric@gmail.com>
  • Lukas Mai <l.mai@web.de>
  • Matt Kraai <kraai@ftbfs.org>
  • Jonathan Scott Duff <duff@pobox.com>
  • Jesse Luehrs <doy@tozt.net>
  • Hakim Cassimally <osfameron@cpan.org>
  • Dave Rolsky <autarch@urth.org>
  • Ricardo SIGNES <rjbs@cpan.org>
  • Sebastian Willert <willert@cpan.org>
  • Steffen Schwigon <ss5@renormalist.net>

the whole group chat showed up. we love to see it. πŸ”₯


This software is copyright (c) 2008 by Florian Ragwitz.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


and that's the whole thing! this page was served up as perl v5.36.0, dated 2023-01-22, from the MooseX...atures(3pm) corner of the User Contributed Perl Documentation. deprecated but never forgotten. slay, go forth, and use core signatures instead πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from