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.


In reply to Re: I don't understand. by rrwo
in thread Detecting if a scalar has a number or string by rrwo

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.