in reply to Testing for existence of subroutine ref?

Normally, I hate code that uses ref, but this is precisely what you need here. Something like:
if (ref $runmodes{$action} eq "CODE") { # it's a coderef, so run it... ... } else { # it's something else {grin} ... }

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: Re: Testing for existence of subroutine ref?
by feanor_269 (Beadle) on Feb 19, 2003 at 03:10 UTC
    Why do you hate code that uses ref? Am I missing something?
    Am I posting alot today, or is it just me? (the last bit is just rhetorical, for those who would think to actually answer this, I know it's subjective.)

    feanor_269

      It's often a symptom of poorly organized code if you have to check explicitly what something is in order to determine what to do with it. Ideally you'd want the structure of your code to shape the flow of data/entities such that at any given point you can reliably expect a certain type of thing.

      Imagine walking around with a single instruction "doIt" along with a collection of minor instructions that you had to follow according to what the thingy was to which you were to "doIt". You don't want to have to decide to "drink" from a coffee cup and "drive" a car; rather, you want to be able to rely on the thing you attempt to drink from being a coffee cup, and the thing you hotwire being a car.

      Another way of thinking about it is that it's better to offload decision-making onto the surrounding structure as opposed to navigating an open-ended environment according to a complex rulebook.


      "The dead do not recognize context" -- Kai, Lexx
Re: •Re: Testing for existence of subroutine ref?
by ihb (Deacon) on Feb 20, 2003 at 01:25 UTC
    Wouldn't UNIVERSAL::isa() suite better?