|Syntactic Confectionery Delight|
Exploiting Perls idea of what is a numberby rovf (Priest)
|on Jul 08, 2008 at 12:41 UTC||Need Help??|
rovf has asked for the wisdom of the Perl Monks concerning the following question:
As we all know, there is no real "number" or "string" data type in Perl, and Perl usually does The Right Thing depending on the context:
I am now looking for an elegant way of exploiting this in my program, in the following way:
I would like to compare two values for equality so that, iff Perl would agree to see them as a number, numerical equality should be used (i.e. '0' and '000' should be regarded equal), but if at least one of them is not a number, string equality should be used.
The only solution I came up with is very ugly and goes roughly like this:
Needless to say that this is extremely awful. Of course one might argue that what I want to achieve is a bit weird too, but still I would like to know whether there is an easy way to do it. Basically, I'm looking for an operation which would leave a string unchanged, but normalizes a number so that for example '001' would turn into '1' (this would be done by my addition of zero for instance).
Ronald Fischer <email@example.com>