Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: I don't understand.

by rrwo (Friar)
on Nov 19, 2004 at 13:15 UTC ( [id://408986]=note: print w/replies, xml ) Need Help??


in reply to I don't understand.
in thread Detecting if a scalar has a number or string

Because if $x is an object, then your regex won't work. (Nevermind that it won't work for reals or negative numbers.)

A stringification method (which your regex assumes) isn't reliable, because I could write a stringification method to output the number in hexidecimal, Base64, or as a roman numeral (see below) but as far as Perl was concerned, it could still operate just like a number):

package RomanNumber; use Roman 'roman'; sub new { my $class = shift; my $self = { value => shift }; bless $self, $class; } sub as_num { my $self = shift; return $self->{value}; } sub as_string { my $self = shift; return roman($self->as_num); } sub compare { my ($a,$b) = @_; return ($a->as_num <=> $b->as_num); } use overload '0+' => \&as_num, '""' => \&as_string, '<=>' => \&compare;

If I add a few more numeric operators (it's an incomplete example), Perl can treat it just like a number, except that it outputs a roman numeral in string context.

There are cases where I would like to require a value to be number, but I would like to give the user of a module the ability to use a 'number-like' object rather than a number.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2025-01-18 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (56 votes). Check out past polls.