From: Vincent Pit Date: Tue, 20 Jul 2010 20:00:20 +0000 (+0200) Subject: Move most of the autoload logic into Autocoerce->load X-Git-Tag: v0.01~36 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=2ea319e77977b3651f90953e477e43e23244fcbd;p=perl%2Fmodules%2FLaTeX-TikZ.git Move most of the autoload logic into Autocoerce->load This saves space, simplify the coerce wrapper, and improve covering granularity. --- diff --git a/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm b/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm index 4706998..9cc2322 100644 --- a/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm +++ b/lib/LaTeX/TikZ/Meta/TypeConstraint/Autocoerce.pm @@ -60,47 +60,51 @@ around 'new' => sub { $parent_name =~ s/::+/_/g; my $tc; - $args{constraint} = Sub::Name::subname("${parent_name}_load" => sub { - my ($thing) = @_; + $args{constraint} = Sub::Name::subname('_load' => sub { + $tc->load(@_); + }); + + $tc = $class->$orig(%args); +}; - # First, try a possible user defined constraint - my $user = $tc->user_constraint; - if (defined $user) { - my $ok = $user->($thing); - return 1 if $ok; - } +sub load { + my ($tc, $thing) = @_; - # When ->check is called inside coerce, a return value of 0 means that - # coercion should take place, while 1 signifies that the value is already - # OK. + # First, try a possible user defined constraint + my $user = $tc->user_constraint; + if (defined $user) { + my $ok = $user->($thing); + return 1 if $ok; + } - my $class = blessed($thing); - return 0 unless $class; - return 1 if $class->isa($tc->parent_name); + # When ->check is called inside coerce, a return value of 0 means that + # coercion should take place, while 1 signifies that the value is already + # OK. - my $mapper = $tc->mapper; - my $pm = $class = $tc->$mapper($class); + my $class = blessed($thing); + return 0 unless $class; + return 1 if $class->isa($tc->parent_name); - $pm =~ s{::}{/}g; - $pm .= '.pm'; - return 0 if $INC{$pm}; # already loaded + my $mapper = $tc->mapper; + my $pm = $class = $tc->$mapper($class); - local $@; - eval { require $pm; 1 }; + $pm =~ s{::}{/}g; + $pm .= '.pm'; + return 0 if $INC{$pm}; # already loaded - return 0; - }); + local $@; + eval { require $pm; 1 }; - $tc = $class->$orig(%args); -}; + return 0; +} around 'coerce' => sub { my ($orig, $tc, $thing) = @_; # The original coerce gets an hold onto the type coercions *before* calling # the constraint. Thus, we have to force the loading before recalling into - # $orig. This is achieved by calling ->check. - return $thing if $tc->check($thing); + # $orig. This is achieved by calling ->load. + return $thing if $tc->load($thing); $tc->$orig($thing); };