Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

ISO lightweight wrapper class for numbers

by tlm (Prior)
on Feb 21, 2009 at 17:39 UTC ( [id://745551]=perlquestion: print w/replies, xml ) Need Help??

tlm has asked for the wisdom of the Perl Monks concerning the following question:

Dear brothers and sisters,

I am looking for a lightweight class that I can wrap around numeric values.

Granted, as wheels go, this looks like a pretty easy one to re-invent... For my immediate needs, this may be enough:

# !!! UNTESTED !!! package MyNumberWrapper; use Scalar::Util 'looks_like_number'; sub new { my $class = shift; my $value; if ( @_ == 1 and defined $_[ 0 ] and looks_like_number( $_[ 0 ] ) { $value = 0 + shift; } else { # raise hell } return bless \$value, $class; } sub value { my $self = shift; return $$self + 0; } 1; __END__
But from bitter experience I've learned to resist as much as possible these seemingly trivial wheel re-invention projects. Hence I come to you for wisdom and guidance. Do you know of a well-tested class in CPAN that could serve this purpose?

the lowliest monk

PS: The reason behind this quest is convoluted, and I'd hate to bore you with it, but if you must know, here it is. The Storable module does dastardly things to numeric values, and my code must JSON-encode and transmit stuff that has been through one Storable::store/Storable::retrieve cycle. The purpose of this wrapper class is to selectively tag those data items that I want treated as numbers by JSON::XS before committing them to Storable::store, and then replace them with their underlying values once the whole thing has been Storable::retrieve'd. If there's a better way around this annoyance, please let me know.

Replies are listed 'Best First'.
Re: ISO lightweight wrapper class for numbers
by Anonymous Monk on Feb 21, 2009 at 23:04 UTC
    Have a look in CPAN searching under modules for "numeric". Test::Numeric and Scalar::Util::Numeric are included in a long list of possibilities.
Re: ISO lightweight wrapper class for numbers
by DrHyde (Prior) on Feb 24, 2009 at 10:30 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://745551]
Approved by salva
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found