Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: Detecting if a scalar has a number or stringby water (Deacon) |
on Nov 18, 2004 at 02:23 UTC ( [id://408679]=note: print w/replies, xml ) | Need Help?? |
of course, the assumption above is that an object that stringifies to a number will support other number-like operations, like == or + , -, * etc. if you can't make that assumption, then you'd need to test if the scalar is an object, and if so, use it in various math contexts w/in an eval loop to see if the object supports the overloaded math ops you need. ack. even that is an assumption -- let's say the object doesn't throw an exception when +=0 or *=1. even in this case, you don't know the overloaded op is doing what you think: the object may have overloaded * to mean convolution or scalar product or who-knows-what, rather than common multiplication. so.... it depends on how much you can assume. in most cases, i'd be comfortable assuming that if an object stringifies to something numberlike, it's worth trying to use it as a number. and eval is your friend, so if you assume wrong, you can trap and respond gracefully.
In Section
Seekers of Perl Wisdom
|
|