MooseX...ethods(3pm)

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

πŸ’… MooseX::MultiMethods

okay bestie, so this is MooseX::MultiMethods(3pm) β€” straight out of the User Contributed Perl Documentation section, so you know it's the people's manpage πŸ’–


✨ VERSION ✨

version 0.10 β€” tiny number, huge energy πŸ‘‘


🌈 SYNOPSIS 🌈

here's the whole vibe, literally a rock-paper-scissors-lizard-spock situation and it's iconic:

    package Paper;    use Moose;
    package Scissors; use Moose;
    package Rock;     use Moose;
    package Lizard;   use Moose;
    package Spock;    use Moose;

    package Game;
    use Moose;
    use MooseX::MultiMethods;

    multi method play (Paper    $x, Rock     $y) { 1 }
    multi method play (Paper    $x, Spock    $y) { 1 }
    multi method play (Scissors $x, Paper    $y) { 1 }
    multi method play (Scissors $x, Lizard   $y) { 1 }
    multi method play (Rock     $x, Scissors $y) { 1 }
    multi method play (Rock     $x, Lizard   $y) { 1 }
    multi method play (Lizard   $x, Paper    $y) { 1 }
    multi method play (Lizard   $x, Spock    $y) { 1 }
    multi method play (Spock    $x, Rock     $y) { 1 }
    multi method play (Spock    $x, Scissors $y) { 1 }
    multi method play (Any      $x, Any      $y) { 0 }

    my $game = Game->new;
    $game->play(Paper->new, Rock->new);     # 1, Paper covers Rock
    $game->play(Spock->new, Paper->new);    # 0, Paper disproves Spock
    $game->play(Spock->new, Scissors->new); # 1, Spock smashes Scissors

eleven whole multi method play variants, all chillin' in one package, and the last one is the Any, Any fallback that just returns 0 when nobody wins. it's giving main character energy fr fr πŸ¦‹


πŸŽ€ DESCRIPTION πŸŽ€

so the way this works, bestie: this module does multi method dispatch based on Moose type constraints. the way it pulls that off is by handing you a multi keyword that extends the method keyword that MooseX::Method::Signatures already gives you. it's a glow-up, not a replacement πŸ’…

and here's the moment-to-moment tea β˜• when you invoke a method that's been declared as multi:

  • a matching variant gets searched for across all the declared multi variants β€” it's looking at the parameters you passed and the declared type constraints πŸ•΅οΈβ€β™€οΈ
  • if a variant has been found, it will be invoked. slay ✨
  • if no variant could be found, an exception will be thrown. πŸ’€ no silent failure, no pretending, it WILL tell you.
lowkey the important part: no match = exception. that's not a vibe, that's the rules πŸ™ˆ

πŸ‘‘ AUTHOR πŸ‘‘

  Florian Ragwitz <rafl@debian.org>

give them their flowers πŸ’


This software is copyright (c) 2010 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 page, babe 🫢 perl v5.36.0, dated 2022-12-13 β€” thanks for coming to my ted talk, now go dispatch some methods πŸ’œπŸ“

πŸ“¦ where this page comes from