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.
I would like the code below;
#! 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";
to print out the following;
Using: IncreaseMagnetude -> 100
Using: MoreMagnitude -> 110
but instead I get;
Using: IncreaseMagnetude -> 100
Using: MoreMagnitude -> Ten