nmerriweather has asked for the wisdom of the Perl Monks concerning the following question:

i need to tell whether or not a function is accessing a number or a string ( the api once accepted strings, I'm shifting it to numerical constants, and need to provide support for migrations that I've missed )

usually, i do something like this:
$value = ( int($value) == $value ) ? $value : lookupString( $value ) +;
that tosses the non-numeric error in strict/warnings
i'd love to learn a way to acomplish this without tripping a warning

Replies are listed 'Best First'.
Re: need idiom - compare a number to a string
by davidrw (Prior) on Mar 17, 2006 at 23:00 UTC
    I think just doing int($value) eq $value should work (will still throw a warning if $value is undef). You could also regex it with something like $value =~ /^[+-]?\d+$/ or Regexp::Common::number.
Re: need idiom - compare a number to a string
by brian_d_foy (Abbot) on Mar 18, 2006 at 01:03 UTC

    Warnings are jsut warnings. Once you've seen it and decide that it's not a problem, you can turn it off for a bit.

    { # local $^W = 0; # old way no warnings; $value = ( int($value) == $value ) ? $value : lookupString( $value ); }

    However, if you're using constants in the code, strings are much better (if they are descriptive). Don't take a step backwards by making everything a magic number. :)

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: need idiom - compare a number to a string
by swampyankee (Parson) on Mar 17, 2006 at 23:03 UTC

    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

      Your regex only catches positive integers, which may be okay -- but it's certainly not all valid numbers. Consider negative integers, rationals, and numbers in scientific notation, for exammple.

        It don't think we have to catch those (well, maybe negatives). It sounds like the OP is talking error codes or something similar, which wouldn't be floating point numbers.