From: Vincent Pit Date: Mon, 2 Aug 2010 09:59:09 +0000 (+0200) Subject: Allow returning undef from the autocoercion mapper X-Git-Tag: v0.02~3 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=4822c0bbb4054d2e5068c0e9592c3965ee9d87f3;p=perl%2Fmodules%2FLaTeX-TikZ.git Allow returning undef from the autocoercion mapper --- diff --git a/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm b/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm index 0dc4d9d..cdbc69c 100644 --- a/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm +++ b/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm @@ -28,6 +28,9 @@ extends any_moose('Meta::TypeConstraint'); =head2 C +A code reference that maps an object class name to the name of the package in which the coercion can be found, or C to disable coercion for this class name. +It is called with the type constraint object as first argument, followed by the class name. + =cut has 'mapper' => ( @@ -115,22 +118,24 @@ around 'coerce' => sub { # If $thing isn't even an object, don't bother trying to autoload a coercion my $class = blessed($thing); if (defined $class) { - # Find the file to autoload - my $mapper = $tc->mapper; - my $pm = $class = $tc->$mapper($class); - $pm =~ s{::}{/}g; - $pm .= '.pm'; - - unless ($INC{$pm}) { # Not loaded yet - local $@; - eval { - # We die often here, even though we're not really interested in the error. - # However, if a die handler is set (e.g. to \&Carp::confess), this can get - # very slow. Resetting the handler shows a 10% total time improvement for - # the geodyn app. - local $SIG{__DIE__}; - require $pm; - }; + $class = $tc->mapper->($tc, $class); + + if (defined $class) { + # Find the file to autoload + (my $pm = $class) =~ s{::}{/}g; + $pm .= '.pm'; + + unless ($INC{$pm}) { # Not loaded yet + local $@; + eval { + # We die often here, even though we're not really interested in the error. + # However, if a die handler is set (e.g. to \&Carp::confess), this can get + # very slow. Resetting the handler shows a 10% total time improvement for + # the geodyn app. + local $SIG{__DIE__}; + require $pm; + }; + } } }