in reply to "2" | "8" = ":" and 2|8=10
You could detect if a scalar is a string or a number, without resorting to XS, by making use of this different behavior. If you use bitwise XOR (^) and XOR a scalar with itself, you'll get the value 0 if it was a number, and a string of null bytes ("\0") with the original string length of the argument, if it was a string. Yes, even if the number doesn't fit into an integer, like 1E99.
I think this will work:
BTW dual-vars, like !1, are treated as a number.sub is_number { my $arg = shift; return defined $arg && !ref $arg && ($arg^$arg) eq '0'; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: "2" | "8" = ":" and 2|8=10
by moritz (Cardinal) on Nov 04, 2011 at 12:42 UTC | |
|
Re^2: "2" | "8" = ":" and 2|8=10
by JavaFan (Canon) on Nov 04, 2011 at 12:39 UTC | |
by bart (Canon) on Nov 04, 2011 at 12:46 UTC | |
by JavaFan (Canon) on Nov 04, 2011 at 12:59 UTC |