John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing my Skunk game, and decided to use 'Skunk' instead of 1 but 2..6 as numeric. This is so that user code that tries to manipulate it as a number will get a warning, if he forgot to check for 1/Skunk first but needed to. Skunk is NOT 1 in arithmetic, but an error. The warning is good enough, so I'm not writing a full-blown class with overloaded arithmetic!

So, a pair of dice has two distinguished enumerated values (Skunk and Double Skunk) in addition to a possible numeric value. To test this, I can use Scaler::Util stuff in 5.8, but if I wanted to avoid being incompatible with 5.6, what's an elegant way to do this?

Hmm, I don't see the function I thought was in Scaler::Util now. I don't see the functions for checking the byte/char mode of a string, either. Where are they? I thought this was all supplied with 5.8 now.

—John

Replies are listed 'Best First'.
Re: Test for numeric
by japhy (Canon) on Jun 21, 2003 at 06:28 UTC
    You're looking to make a dual scalar? That is, one that has a separate string and scalar value? If so, Scalar::Util does indeed have a dualvar() function.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Test for numeric
by broquaint (Abbot) on Jun 21, 2003 at 14:43 UTC
    You might want to check out merlyn's is_numeric function
    sub is_numeric { ($_[0] & ~ $_[0]) eq "0"; }
    Which tests whether a scalar is a numeric value or a string value e.g 42 vs "42".
    HTH

    _________
    broquaint

      More like "this is not a digit at all" vs 42. It evaluates to 0 with a warning, which is the intention--to make sure you check for the zeros before doing arithmetic. But what's an elegant way to see if it's one of the strings without generating the warning? I was thinking of no warning whatever-it-is;, but thought that something direct was available with 5.8.