Since Perl will autostringify as needed, just check to see if $value looks like a number or not. I'd probably do something radically sub-optimal like
$value = lookupString($value) if($value !~ /^\d+$/);
I'm sure one of the monks who has Perl skills above mine (i.e. almost everybody) has a better way, but I think my fragment will avoid too many errors (I'd probably demonstrate paranoia by checking to see if $value is defined before the match)
added in update
As
chromatic pointed out, the regex I showed only checks for positive integers, with neither signs nor blank padding. Since the OP's posted code includes
int($value), I'm concluding values including a decimal point or in exponential notation aren't a concern. The regex should probably look more like this:
$value =~ /^\s*[+-]?\s*\d+\s*$/
which should deal with signed and unsigned integers, but will reject cases where
$value includes a radix point or is in exponential notation.
emc
" The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents.
" — Nathaniel S. Borenstein
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.