| Dispatch::Class(3pm) | User Contributed Perl Documentation | Dispatch::Class(3pm) |
Dispatch::Class - dispatch on the type (class) of an argument
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);
This module offers a (mostly) simple way to check the class of an object and handle specific cases specially.
The following functions are available and can be imported on request:
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.
The following things can be used as keys:
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.
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).
This module uses "Exporter::Tiny", so you can rename the imported functions at "use" time.
Exporter::Tiny
Lukas Mai, "<l.mai at web.de>"
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 http://dev.perl.org/licenses/ for more information.
| 2022-10-15 | perl v5.36.0 |