http://www.perlmonks.org?node_id=888745


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.

Replies are listed 'Best First'.
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