[Purdue-pm] Perl 6 multisubs and multimethods

Mark Senn mark at purdue.edu
Wed Oct 28 06:00:46 PDT 2015


(
    Brad, Broc,

    I'm not going to cc stuff to you anymore.  Sign up for the mailing list
    (see pm.purdue.org) if you wantt to get stuff I send to the mailing list
    in the future.
)

In Perl 6 one can write multisubs and multimethods where you define a
routine and declare what types, values, and number of arguments it takes.
This saves one from having to write code to do different things based
on that information and is just another way you can "Let Perl 6 Do It"
instead of having to write code to do it.

For example, let's say you want to have a program named door that you
can either call with "door open" or "door close".  Here is one way to
do it:

    List-Subscribe: <mailto:perl6-users-subscribe at perl.org>
    List-Id: <perl6-users.perl.org>
    Date: Sun, 28 Jun 2015 12:24:31 +0200
    From: Moritz Lenz
    To: perl6-users at perl.org
    Hi,
    
    On 06/28/2015 12:39 AM, Tom Browder wrote:
    > I'm trying to take advantage of the MAIN suroutine to handle most all of
    > my routine command line arg handling.  One idiom I use a lot is for the
    > user to choose only one of two args, but one must be chosen.
    
    So since it's not optional, you might consider not making it an option
    (prefixed with "--"), but rather a simple command:
    
    
    $ cat door
    #!/usr/bin/env perl6
    
    multi MAIN('open') {
        say "Opening door";
    }
    multi MAIN('close') {
        say "Closing door";
    }
    
    $ ./door
    Usage:
      ./door open
      ./door close
    
    $ ./door open
    Opening door
    
    Cheers,
    Moritz

Again, this feature is general and calls the right version of  the routine
automatically depending on types, values, and number of arguments a routine
is called with.  Thought you might be interested.    -mark


More information about the Purdue-pm mailing list