# untested! use Regexp::Common; sub isnumeric { my ($x) = @_; # force stringification, if $x is an object. my $s = "$x"; # determine if numberish. roll-your-own or i prefer CPAN. return $s =~ /$RE{num}{real}/; }
of course, the assumption above is that an object that stringifies to a number will support other number-like operations, like == or  + , -, * etc.

if you can't make that assumption, then you'd need to test if the scalar is an object, and if so, use it in various math contexts w/in an eval loop to see if the object supports the overloaded math ops you need. ack.

even that is an assumption -- let's say the object doesn't throw an exception when  +=0 or  *=1. even in this case, you don't know the overloaded op is doing what you think: the object may have overloaded * to mean convolution or scalar product or who-knows-what, rather than common multiplication.

so....

it depends on how much you can assume. in most cases, i'd be comfortable assuming that if an object stringifies to something numberlike, it's worth trying to use it as a number.

and  eval is your friend, so if you assume wrong, you can trap and respond gracefully.

water


In reply to Re: Detecting if a scalar has a number or string by water
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.