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


In reply to Type::Coercion on the fly by jandrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.