.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Kavorka::Manual::MultiSubs 3pm" .TH Kavorka::Manual::MultiSubs 3pm "2019-01-17" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Kavorka::Manual::MultiSubs \- multi subs and multi methods .SH "DESCRIPTION" .IX Header "DESCRIPTION" Kavorka supports multi methods and multi subs: .PP .Vb 2 \& multi method process (ArrayRef $x) { say "here" } \& multi method process (HashRef $x) { say "there" } \& \& _\|_PACKAGE_\|_\->process( [] ); # here \& _\|_PACKAGE_\|_\->process( {} ); # there .Ve .PP Multi subs and multi methods must be named (cannot be anonymous coderefs). .PP 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. .SS "Multi methods versus multi subs" .IX Subsection "Multi methods versus multi subs" The word after \f(CW\*(C`multi\*(C'\fR (i.e. \f(CW\*(C`method\*(C'\fR 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 \&\f(CW\*(C`invocation_style\*(C'\fR method (see Kavorka::Sub). .PP If the \f(CW\*(C`invocation_style\*(C'\fR is \*(L"fun\*(R" (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. .PP If the \f(CW\*(C`invocation_style\*(C'\fR is \*(L"method\*(R" (like Kavorka::Sub::Method), then if no successful candidate is found in the current class, candidates in superclasses are also considered. .SS "Long names" .IX Subsection "Long names" It is possible to define alternative \*(L"long names\*(R" for the candidates of a multi method or multi sub using the \f(CW\*(C`:long\*(C'\fR attribute: .PP .Vb 3 \& 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 .Ve .PP (Actually, \f(CW\*(C`:long\*(C'\fR isn't a real attribute; we just borrow the syntax. If you try to use attributes' introspection stuff, you won't find it.) .PP Prototypes, subroutine attributes, etc. declared on the multi subs will appear on the \*(L"long name\*(R" subs, but not the multi sub. .SS "Definition at run time" .IX Subsection "Definition at run time" Multi subs and multi methods are added to the symbol at run time (like methods, see \*(L"The Method Name\*(R" in Kavorka::Manual::Methods). .PP This means that it's possible to partly define a multi sub, call it, then further define it, before calling it again. .PP .Vb 2 \& use Kavorka qw( multi fun ); \& use Try::Tiny; \& \& multi fun plus_one (Int $x) { $x + 1 } \& \& try { \& plus_one(41); # 42 \& plus_one(1.1); # throws \& }; \& \& multi fun plus_one (Num $x) { $x + 1 } \& \& plus_one(41); # 42 \& plus_one(1.1); # 2.1 .Ve .SH "BUGS" .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" Kavorka::Manual, Kavorka::Manual::Signatures, Kavorka::Manual::PrototypeAndAttributes, Kavorka::Manual::Functions, Kavorka::Manual::Methods. .SH "AUTHOR" .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2013\-2014, 2017 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "DISCLAIMER OF WARRANTIES" \&\s-1THIS PACKAGE IS PROVIDED \*(L"AS IS\*(R" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.\s0