I've tried Scalar::Util::looks_like_number and also the following:
sub is_numeric { ($_[0] & ~ $_[0]) eq "0"; }

Perhaps a bit of explanation on how this works, might be welcomed. Bitwise operators are the only operators in Perl that actually behave differently on two numbers (bitwise manipulation of integers) than on two strings (bitwise manipulation of each byte of the strings — on two operands, it's combining byte $i of string A with byte $i of string B). n.b. If one of the two arguments is a number, the other one will be converted to a number, too, possibly with a warning as a consequence. It may also change the nature of that scalar, as it'll be thought of by perl as a number, afterwards.

So, the two basic cases are:

  1. A numerical argument. This argument is treated as an integer, and is bitwise inverted as an integer, thus every bit is flipped in the result. Bitwise and on these two integers, $n & ~$n, results in an integer with every bit cleared, as there are no bits set in both arguments (They're opposites, remember?) This one integer, with all bits cleared, is zero. Converted to a string, this will be "0".
  2. A string as argument. This string is first inverted as a string, byte by byte. $s & ~$s will also be a string, containing only null bytes for the same reason as above; thus the result will be "\0" x length $s. Note the difference between "0", chr(48) in Ascii, and "\0", chr(0). The result's length may vary, but the result string will never be "0".
And that is how it works.

In reply to Re: Testing For Numberness Vs Stringness by bart
in thread Testing For Numberness Vs Stringness by BrooklineTom

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.