Dispatch::Class(3pm)
π Dispatch::Class β the Dispatch::Class(3pm) glow-up π
okay so bestie, this is a page from the User Contributed Perl Documentation and honestly? it's giving "tiny module, huge main character energy." π
β¨ NAME β¨
Dispatch::Class β dispatch on the type (class) of an argument. literally that's it, that's the whole vibe. π―
πΈ SYNOPSIS πΈ
use Dispatch::Class qw(
class_case
dispatch
);
# analyze the class of an object
my $analyze = class_case(
'Some::Class' => 1,
'Other::Class' => 2,
'UNIVERSAL' => "???",
);
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(
'Dog::Tiny' => sub { ... }, # handle objects of the class Dog::Tiny
'Dog' => sub { ... },
'Mammal' => sub { ... },
'Tree' => sub { ... },
'ARRAY' => sub { ... }, # handle array refs
':str' => sub { ... }, # handle non-reference strings
'*' => sub { ... }, # handle any value
);
# call the appropriate handler, passing $obj as an argument
my $result = $dispatch->($obj);
π¦ DESCRIPTION π¦
This module offers a (mostly) simple way to check the class of an object and handle specific cases specially. "mostly" bestie, she said mostly π β the author is being honest and we respect it. π
π Functions π
The following functions are available and can be imported on request:
class_caseβclass_casetakes a list ofKEY, VALUEpairs 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 correspondingVALUEof the first matchingKEY. π
Example:
my $subref = class_case(
KEY1 => VALUE1,
KEY2 => VALUE2,
...
);
my $value = $subref->($some_object);
This will check the class of $some_object against KEY1, KEY2, ... in order and return the corresponding VALUEn of the first match. If no key matches, an empty list/undef is returned in list/scalar context, respectively. (yes, context-sensitive, as is tradition π«Ά)
The following things can be used as keys:
*β This will match any value. No actual check is performed. the chillest key in the whole cast, she simply does not gatekeep π:strβ This special key will match any non-reference.SCALAR,ARRAY,HASH, ... β These values match references of the specified type even if they aren't objects (i.e. notblessed). That is, for unblessed references the string returned byrefis compared witheq. so like, a plain array ref still gets caught, no blessing required π- CLASS β Any other string is interpreted as a class name and matches if the input value is an object for which
$obj->isa($CLASS)is true. To match any kind of object (blessed value), use the key'UNIVERSAL'.
Starting with Perl 5.10.0 Perl supports checking for roles with DOES, so Dispatch::Class actually uses $obj->DOES($CLASS) instead of isa. This still returns true for normal base classes but it also accepts roles that have been composed into the object's class. role girlies, you're covered π§β¨
dispatchβ This works likeclass_caseabove, but the VALUEs must be code references and get invoked automatically:
sub dispatch {
my $analyze = class_case @_;
sub {
my ($obj) = @_;
my $handler = $analyze->($obj) or return;
$handler->($obj)
}
}
That is, the matching object is passed on to the matched VALUEs and the return value of the inner sub is whatever the handler returns (or the empty list/undef if no KEY matches). no match? no drama, you just get nothing back π«₯
This module uses Exporter::Tiny, so you can rename the imported functions at use time. nicknames? iconic. π
β SEE ALSO β
Exporter::Tiny
π AUTHOR π
Lukas Mai, "<l.mai at web.de>" β thank you for your service, king π«Ά
π COPYRIGHT & LICENSE π
Copyright 2013, 2014 Lukas Mai.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See <https://dev.perl.org/licenses/> for more information.
and that's the whole page bestie, no cap, every single option and key accounted for πβ¨
this tour was brought to you by perl v5.40.1, dated 2025-11-08, and the lovely people behind Dispatch::Class(3pm) in the User Contributed Perl Documentation. mwah. πππ¦
π¦ where this page comes from
- ubuntu resolute: libdispatch-class-perl 0.04-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdispatch-class-perl 0.04-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms