MooseX::Types::VariantTable(3) Type constraint based variant table


# see also MooseX::Types::VariantTable::Declare for a way to
# declare variant table based methods
use MooseX::Types::VariantTable;
my $dispatch_table = MooseX::Types::VariantTable->new(
variants => [
{ type => "Foo", value => \&foo_handler },
{ type => "Bar", value => \&bar_handler },
{ type => "Item", value => \&fallback },
# look up the correct handler for $thingy based on the type constraints it passes
my $entry = $dispatch_table->find_variant($thingy);
# or use the 'dispatch' convenience method if the entries are code refs
$dispatch_table->dispatch( $thingy, @args );


This object implements a simple dispatch table based on Moose type constraints.

Subtypes will be checked before their parents, meaning that the order of the declaration does not matter.

This object is used internally by Moose::Meta::Method::VariantTable and MooseX::Types::VariantTable::Declare to provide primitive multi sub support.



A code reference that'll be executed when find_variant found more than one matching variant for a value. It defaults to something that simply croaks with an error message like this:

  Ambiguous match %s

where %s contains a list of stringified types that matched.


add_variant $type, $value
Registers $type, such that $value will be returned by "find_variant" for items passing $type.

Subtyping is respected in the table.

find_variant $value
Returns the registered value for the most specific type that $value passes.
dispatch $value, @args
A convenience method for when the registered values are code references.

Calls "find_variant" and if the result is a code reference, it will "goto" this code reference with the value and any additional arguments.

has_type $type
Returns true if an entry for $type is registered.
has_parent $type
Returns true if a parent type of $type is registered.


The meta method composes in multiple inheritence but not yet with roles due to extensibility issues with the role application code.

When Moose::Meta::Role can pluggably merge methods variant table methods can gain role composition.


Yuval Kogman <[email protected]>

Florian Ragwitz <[email protected]>


        Copyright (c) 2008 Yuval Kogman. All rights reserved
        This program is free software; you can redistribute
        it and/or modify it under the same terms as Perl itself.