Hello Monks!
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:$x=15; $x.=4; $x+=2; # ==> 156
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).$temp1=eval { 0+$value1 }; $temp1=$value1 if $@; Perl says it is not a number $temp2=eval { 0+$value2 }; $temp2=$value2; # By virtue of eval, we can now use string equality # even if the values were numbers before. if ($temp1 eq $temp2) { ... }
In reply to Exploiting Perls idea of what is a number by rovf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |