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:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Testing For Numberness Vs Stringness
by BrooklineTom (Novice) on Nov 29, 2004 at 13:19 UTC |