.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" 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 "Dispatch::Class 3pm" .TH Dispatch::Class 3pm "2022-10-15" "perl v5.36.0" "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" Dispatch::Class \- dispatch on the type (class) of an argument .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 4 \& use Dispatch::Class qw( \& class_case \& dispatch \& ); \& \& # analyze the class of an object \& my $analyze = class_case( \& \*(AqSome::Class\*(Aq => 1, \& \*(AqOther::Class\*(Aq => 2, \& \*(AqUNIVERSAL\*(Aq => "???", \& ); \& my $foo = $analyze\->(Other::Class\->new); # 2 \& my $bar = $analyze\->(IO::Handle\->new); # "???" \& my $baz = $analyze\->(["not an object"]); # undef \& \& # build a dispatcher \& my $dispatch = dispatch( \& \*(AqDog::Tiny\*(Aq => sub { ... }, # handle objects of the class Dog::Tiny \& \*(AqDog\*(Aq => sub { ... }, \& \*(AqMammal\*(Aq => sub { ... }, \& \*(AqTree\*(Aq => sub { ... }, \& \& \*(AqARRAY\*(Aq => sub { ... }, # handle array refs \& \& \*(Aq:str\*(Aq => sub { ... }, # handle non\-reference strings \& \& \*(Aq*\*(Aq => sub { ... }, # handle any value \& ); \& \& # call the appropriate handler, passing $obj as an argument \& my $result = $dispatch\->($obj); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module offers a (mostly) simple way to check the class of an object and handle specific cases specially. .SS "Functions" .IX Subsection "Functions" The following functions are available and can be imported on request: .ie n .IP """class_case""" 4 .el .IP "\f(CWclass_case\fR" 4 .IX Item "class_case" \&\f(CW\*(C`class_case\*(C'\fR takes a list of \f(CW\*(C`KEY, VALUE\*(C'\fR pairs and returns a code reference that (when called on an object) will analyze the object's class according to the rules described below and return the corresponding \fI\s-1VALUE\s0\fR of the first matching \fI\s-1KEY\s0\fR. .Sp Example: .Sp .Vb 6 \& my $subref = class_case( \& KEY1 => VALUE1, \& KEY2 => VALUE2, \& ... \& ); \& my $value = $subref\->($some_object); .Ve .Sp This will check the class of \f(CW$some_object\fR against \f(CW\*(C`KEY1\*(C'\fR, \f(CW\*(C`KEY2\*(C'\fR, ... in order and return the corresponding \f(CW\*(C`VALUEn\*(C'\fR of the first match. If no key matches, an empty list/undef is returned in list/scalar context, respectively. .Sp The following things can be used as keys: .RS 4 .ie n .IP """*""" 4 .el .IP "\f(CW*\fR" 4 .IX Item "*" This will match any value. No actual check is performed. .ie n .IP """:str""" 4 .el .IP "\f(CW:str\fR" 4 .IX Item ":str" This special key will match any non-reference. .ie n .IP """SCALAR"", ""ARRAY"", ""HASH"", ..." 4 .el .IP "\f(CWSCALAR\fR, \f(CWARRAY\fR, \f(CWHASH\fR, ..." 4 .IX Item "SCALAR, ARRAY, HASH, ..." These values match references of the specified type even if they aren't objects (i.e. not \f(CW\*(C`bless\*(C'\fRed). That is, for unblessed references the string returned by \f(CW\*(C`ref\*(C'\fR is compared with \&\f(CW\*(C`eq\*(C'\fR. .IP "\s-1CLASS\s0" 4 .IX Item "CLASS" Any other string is interpreted as a class name and matches if the input value is an object for which \f(CW\*(C`$obj\->isa($CLASS)\*(C'\fR is true. To match any kind of object (blessed value), use the key \f(CW\*(AqUNIVERSAL\*(Aq\fR. .Sp Starting with Perl 5.10.0 Perl supports checking for roles with \f(CW\*(C`DOES\*(C'\fR, so \&\f(CW\*(C`Dispatch::Class\*(C'\fR actually uses \f(CW\*(C`$obj\->DOES($CLASS)\*(C'\fR instead of \f(CW\*(C`isa\*(C'\fR. This still returns true for normal base classes but it also accepts roles that have been composed into the object's class. .RE .RS 4 .RE .ie n .IP """dispatch""" 4 .el .IP "\f(CWdispatch\fR" 4 .IX Item "dispatch" This works like \f(CW\*(C`class_case\*(C'\fR above, but the \fI\s-1VALUE\s0\fRs must be code references and get invoked automatically: .Sp .Vb 8 \& sub dispatch { \& my $analyze = class_case @_; \& sub { \& my ($obj) = @_; \& my $handler = $analyze\->($obj) or return; \& $handler\->($obj) \& } \& } .Ve .Sp That is, the matching object is passed on to the matched \fI\s-1VALUE\s0\fRs and the return value of the inner sub is whatever the handler returns (or the empty list/undef if no \fI\s-1KEY\s0\fR matches). .PP This module uses \f(CW\*(C`Exporter::Tiny\*(C'\fR, so you can rename the imported functions at \f(CW\*(C`use\*(C'\fR time. .SH "SEE ALSO" .IX Header "SEE ALSO" Exporter::Tiny .SH "AUTHOR" .IX Header "AUTHOR" Lukas Mai, \f(CW\*(C`\*(C'\fR .SH "COPYRIGHT & LICENSE" .IX Header "COPYRIGHT & LICENSE" Copyright 2013, 2014 Lukas Mai. .PP This program is free software; you can redistribute it and/or modify it under the terms of either: the \s-1GNU\s0 General Public License as published by the Free Software Foundation; or the Artistic License. .PP See http://dev.perl.org/licenses/ for more information.