in reply to Testing For Numberness Vs Stringness

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.

Replies are listed 'Best First'.
Re^2: Testing For Numberness Vs Stringness
by BrooklineTom (Novice) on Nov 29, 2004 at 13:19 UTC
    Thanks, Bart. Perl's habit of permanently converting a var into a number is, in my opinion, at least an aggravating hack even if not an actual bug. In any case, your warning is well-taken. The comparion routine should work on a copy of its arg.