Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Moose - ensuring a getter returns a number

by Fletch (Chancellor)
on Feb 17, 2011 at 16:03 UTC ( #888745=note: print w/ replies, xml ) Need Help??


in reply to Moose - ensuring a getter returns a number

Maybe use around?

around 'now' => sub { my $orig = shift; my $self = shift; return $self->$orig( @_ ) + 0; }

The cake is a lie.
The cake is a lie.
The cake is a lie.


Comment on Re: Moose - ensuring a getter returns a number
Select or Download Code
Re^2: Moose - ensuring a getter returns a number
by perl5ever (Pilgrim) on Feb 17, 2011 at 20:40 UTC
    Thanks for the idea.

    Is there anything that can be done with Moose type constraints?

      Not likely, since Perl is so permissive about what is a number and what is a string, the Moose type constraint for numbers allows them to also be strings as well.

      The solution with the around is actually pretty good, or as another responder points out, make sure that the value is a number before it is actually added to your data object.

      -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://888745]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-12-29 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls