good chemistry is complicated,
and a little bit messy -LW
Detecting if a scalar has a number or stringby rrwo (Friar)
|on Nov 17, 2004 at 21:48 UTC||Need Help??|
rrwo has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to come up with some code to detect if a scalar variable contains a number or string. The difficulty is that I want to detect objects which have the appropriate conversion and comparison operators overloaded.
The reason is that it's common to insist that certain values be strings or numbers, but checks for them that rule out references will rule out objects that have numberlike or stringlike behaviour (such as BigInts or custom classes). So I want to know that references (blessed things) refer to objects which act just like numbers or strings.
This turns out to be not-so-simple for the case of numbers.
According to the perlfunc manpage on sort, I should be able to use <=> to compare things, and if it's NaN, it turns out undefined. I think the page is outdated. The following turns out true in ActiveState Perl Build 810 (Perl 5.8.4):
The == operator also seems to work for NaNs (Not-a-Numbers) when it's not supposed to.
I do know there's been discussions/complaints in the past about how bad it is when Perl distinquishes between == and eq (and found this frustrating when I was a Perl newbie...) but the docs should have been updated if this was changed. Was it changed???
Furthermore, I seem to have run into the following hiccup from overload:
I run this and get the following error: "Operation `+': no method found, left argument has no overloaded magic, right argument in overloaded package Foo at bug.pl line 22."
This is based on a section from the cookbook in the overload manpage!
So my attempts to detect if 0+$num do not work.
To add to this, it seems that the looks_like_number function in the Scalar::Util module is broken, in that it says some references are numbers:
The code I was experimenting with was the following:
In case you're interested, the "stringlike" function I have (which seems to work) is:
To explain how these work, I am checking that the appropriate number/strong conversions work, and that the comparison operators work. Overload is supposed to create the less than/greater than variations from the comparison operators. For my purposes, these are all that I need to check for.
Also.... you might recognize this issue from some posts of mine on the module-authors list.