Vincent Pit


LaTeX::TikZ::Meta::TypeConstraint::Autocoerce - Type constraint metaclass that autoloads type coercions.


Version 0.02


    # The target class of the autocoercion (cannot be changed)
     package X;
     use Any::Moose;
     has 'id' => (
      is  => 'ro',
      isa => 'Int',
     use LaTeX::TikZ::Meta::TypeConstraint::Autocoerce;
     use Any::Moose 'Util::TypeConstraints';
       name   => 'X::Autocoerce',
       parent => find_type_constraint(__PACKAGE__),
       mapper => sub { join '::', __PACKAGE__, 'From', $_[1] },

    # The class that does the coercion (cannot be changed)
     package Y;
     use Any::Moose;
     has 'x' => (
      is      => 'ro',
      isa     => 'X::Autocoerce',
      coerce  => 1,
      handles => [ 'id' ],

    # Another class the user wants to use instead of X (cannot be changed)
     package Z;
     use Any::Moose;
     has 'id' => (
      is  => 'ro',
      isa => 'Num',

    # The autocoercion class, defined by the user in X/From/
     package X::From::Z;
     use Any::Moose 'Util::TypeConstraints';
     coerce 'X::Autocoerce'
         => from 'Z'
         => via { X->new(id => int $_->id) };

    my $z = Z->new(id => 123);
    my $y = Y->new(x => $z);
    print $y->id; # 123


This type constraint metaclass tries to autoload a specific module when a type coercion is attempted, which is supposed to contain the actual coercion code. This allows you to declare types that can be replaced (through coercion) at the end user's discretion.

It works with both Moose and Mouse by using Any::Moose.

Note that you will need "register_type_constraint" in Moose::Util::TypeConstraints or "register_type_constraint" in Mouse::Util::TypeConstraints to install this type constraint, and that the latter is only available starting Mouse 0.63.


This class inherits from Moose::Meta::TypeConstraint or Mouse::Meta::TypeConstraint, depending on which mode Any::Moose runs.



The name of the type constraint. This must be the target of both the classes that want to use the autocoercion feature and the user defined coercions in the autoloaded classes.

This attribute is inherited from the Moose or Mouse type constraint metaclass.


A code reference that maps an object class name to the name of the package in which the coercion can be found, or undef to disable coercion for this class name. It is called with the type constraint object as first argument, followed by the class name.


A type constraint that defines which objects are already valid and do not need to be coerced. This is somewhat different from "parent" in Moose::Meta::TypeConstraint. If it is given as a plain string, then a type constraint with the same name is searched for in the global type constraint registry.


An optional user defined code reference which predates checking the parent for validity.


new name => $name, mapper => $mapper, parent => $parent, [ user_constraint => sub { ... } ]

Constructs a type constraint object that will attempt to autocoerce objects that are not valid according to $parent by loading the class returned by $mapper.

coerce $thing

Tries to coerce $thing by first loading a class that might contain a type coercion for it.


Moose::Meta::TypeConstraint, Mouse::Meta::TypeConstraint.


Vincent Pit, <perl at>,

You can contact me by mail or on (vincent).


Please report any bugs or feature requests to bug-latex-tikz at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc LaTeX::TikZ


Copyright 2010 Vincent Pit, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.