Kavorka::Manual::MultiSubs - multi subs and multi methods
Kavorka supports multi methods and multi subs:
multi method process (ArrayRef $x) { say "here" } multi method process (HashRef $x) { say "there" } __PACKAGE__->process( [] ); # here __PACKAGE__->process( {} ); # there
This feature is shared with Perl 6 signatures, though Kavorka does not support some of Perl 6's more advanced features such as multi method prototypes. (Though method modifiers should more or less work with multi methods!) Kavorka includes both type constraints and value constraints in the dispatch decision, while Perl 6 only uses type constraints.
The current implementation is not especially efficient. For example, type constraints are checked when deciding which multi method candidate to dispatch to, and then re-checked once the dispatch has been done. However, there are opportunities for future versions of Kavorka to optimize some aspects of the multi method implementation.
The word after multi (i.e. method in the above example) can be any Kavorka keyword that has been set up in the current lexical scope, provided the implementation class provides a non-undef invocation_style method (see Kavorka::Sub).
multi
method
invocation_style
If the invocation_style is "fun" (like Kavorka::Sub::Fun), then the signature of each candidate function in package is checked in the order in which they were defined, and the first matching candidate is dispatched to.
If the invocation_style is "method" (like Kavorka::Sub::Method), then if no successful candidate is found in the current class, candidates in superclasses are also considered.
It is possible to define alternative "long names" for the candidates of a multi method or multi sub using the :long attribute:
:long
multi fun process (ArrayRef $x) :long(process_array) { say "here"; } multi fun process (HashRef $x) :long(process_hash) { say "there"; } process($a); # multi dispatch process_array($b); # single dispatch process_hash($c); # single dispatch
(Actually, :long isn't a real attribute; we just borrow the syntax. If you try to use attributes' introspection stuff, you won't find it.)
Future versions of Kavorka might skip some type constraint checks when a candidate is called directly by its long name.
Prototypes, subroutine attributes, etc declared on the multi subs will appear on the "long name" subs, but not the multi sub.
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Kavorka.
Kavorka, Kavorka::Sub, Kavorka::Signature, Kavorka::Signature::Parameter.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2013 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install Kavorka, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Kavorka
CPAN shell
perl -MCPAN -e shell install Kavorka
For more information on module installation, please visit the detailed CPAN module installation guide.