jandrew has asked for the wisdom of the Perl Monks concerning the following question:
Hello all,
I have one of those questions where the possible answer may be don't do that. However, I am attempting to build Type::Coercion objects/instances on the fly and I am having some trouble figuring out what I'm doing wrong.
to print out the following;#! perl use strict; use warnings; use Type::Utils -all; use Type::Library -base, -declare => qw( IncreaseMagnetude ); use Types::Standard qw( Num Str ); use Type::Coercion; my $lookup ={ Ten => 10 }; declare_coercion IncreaseMagnetude, to_type Num, from Str, via{ $lookup->{$_} * 10 }; # Alternate on-the-fly version my $more_magnetude = Type::Coercion->new( name => 'MoreMagnitude', to_type => Num, from => Str, via => sub{ $lookup->{$_}* 11 }, ); print "Using: " . IncreaseMagnetude->display_name . " -> "; print IncreaseMagnetude->( 'Ten' ) . "\n"; print "Using: " . $more_magnetude->display_name . " -> "; print $more_magnetude->( 'Ten' ) . "\n";
but instead I get;Using: IncreaseMagnetude -> 100 Using: MoreMagnitude -> 110
Using: IncreaseMagnetude -> 100 Using: MoreMagnitude -> Ten
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Type::Coercion on the fly
by tobyink (Canon) on Jul 08, 2014 at 15:36 UTC | |
by jandrew (Chaplain) on Jul 08, 2014 at 16:08 UTC |