in reply to Redirection within a Perl script

Seeing that you have some repeated subroutines, you might want to use this multiple key dispatch method to map values to subroutines. Then just do something like:
(exists $hash{$funct} ? $hash{$funct} : \&some_default)->();
Update: While the trinary operator is overkill in this example, if the script and the dispatch hash were persistent, you would want to do it this way so as to not define any new keys in the hash, and open up a possible source of a memory leak. Also Zaxo++ for pointing out what a regular dispatch hash is, which is more straightforward than the code I linked to (though a regular example is in the parent of the linked node).

Replies are listed 'Best First'.
Re: Re: Redirection within a Perl script
by bradcathey (Prior) on Dec 04, 2003 at 03:13 UTC
    The amazing thing about this place (the Monastery) is that by asking one question, I end up needing to ask many more because I'm always get pushed into new territory. All that to say, I'm not sure I understand Meryln's code as much I as I do yours, runrig. Both you and Zaxo talk about "dispatch tables" which I have read about here at PMs, but can't find in my well-worn Camel book. So, I'm going to Google and try to get my head around this, 'cuz it looks like I'm being pointed in that direction.

    —Brad
    "A little yeast leavens the whole dough."

Re: Re: Redirection within a Perl script
by simonm (Vicar) on Dec 04, 2003 at 04:17 UTC
    ... you might want to use this reverse dispatch method...

    I agree with the "hash-of-subrefs as dispatch table" recommendation, but the trinary operator is overkill here -- we don't have to worry about catching the "exists but false case" so a regular alternation would be enough:

    ( $hash{$funct} || \&some_default )->();