LaTeX::TikZ::Meta::TypeConstraint::Autocoerce - Type constraint metaclass that autoloads type coercions.
Version 0.03
# The target class of the autocoercion (cannot be changed) { package X; use Mouse; has 'id' => ( is => 'ro', isa => 'Int', ); use LaTeX::TikZ::Meta::TypeConstraint::Autocoerce; use Mouse::Util::TypeConstraints; register_type_constraint( LaTeX::TikZ::Meta::TypeConstraint::Autocoerce->new( name => 'X::Autocoerce', target => find_type_constraint(__PACKAGE__), mapper => sub { join '::', __PACKAGE__, 'From', $_[1] }, ); ); __PACKAGE__->meta->make_immutable; } # The class that does the coercion (cannot be changed) { package Y; use Mouse; has 'x' => ( is => 'ro', isa => 'X::Autocoerce', coerce => 1, handles => [ 'id' ], ); __PACKAGE__->meta->make_immutable; } # Another class the user wants to use instead of X (cannot be changed) { package Z; use Mouse; has 'id' => ( is => 'ro', isa => 'Num', ); __PACKAGE__->meta->make_immutable; } # The autocoercion class, defined by the user in X/From/Z.pm { package X::From::Z; use Mouse::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
When a type coercion is attempted, this type constraint metaclass tries to autoload a specific module 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 only supports Mouse currently.
Note that you will need "register_type_constraint" in Mouse::Util::TypeConstraints to install this type constraint, which is only available starting Mouse 0.63.
0.63
This class inherits from Mouse::Meta::TypeConstraint.
name
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 Mouse type constraint metaclass.
mapper
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.
undef
target
A type constraint that defines into what the objects are going to be coerced. Objects satisfying this type constraint will be automatically considered as valid and will not be coerced. 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.
new
my $tc = LaTeX::TikZ::Meta::TypeConstraint::Autocoerce->new( name => $name, mapper => $mapper, target => $target, );
Constructs a type constraint object that will attempt to autocoerce objects that are not valid according to $target by loading the class returned by $mapper.
$target
$mapper
coerce
$tc->coerce($thing)
Tries to coerce $thing by first loading a class that might contain a type coercion for it.
$thing
Mouse::Meta::TypeConstraint.
Vincent Pit, <perl at profvince.com>, http://www.profvince.com.
<perl at profvince.com>
You can contact me by mail or on irc.perl.org (vincent).
irc.perl.org
Please report any bugs or feature requests to bug-latex-tikz at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-TikZ. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-latex-tikz at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc LaTeX::TikZ
Copyright 2010,2011,2012,2013,2014,2015 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.
To install LaTeX::TikZ, copy and paste the appropriate command in to your terminal.
cpanm
cpanm LaTeX::TikZ
CPAN shell
perl -MCPAN -e shell install LaTeX::TikZ
For more information on module installation, please visit the detailed CPAN module installation guide.