Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^2: elsif chain vs. dispatch

by sflitman (Hermit)
on Apr 26, 2009 at 23:50 UTC ( #760224=note: print w/replies, xml ) Need Help??

in reply to Re: elsif chain vs. dispatch
in thread elsif chain vs. dispatch

For me, it is that the codebase is becoming unwieldy, so I wanted to switch to the dispatch style, and I was (honest) going to be putting it outside a sub so it only gets compiled once!

Have you ever considered using the symbol table as the hash, would this be any good:

$::{"handle_fieldcode_$code"}->($args); # for $code = 'A'..'Z'

Replies are listed 'Best First'.
Re^3: elsif chain vs. dispatch
by ikegami (Pope) on Apr 27, 2009 at 02:57 UTC

    The slow bit is the function call, not the hash lookup which you'd also have to do with the symtab anyway. With a hash, you have better control over what inputs are allowed and how they are dispatched.

    With symtab:

    my $handler = do { no strict 'refs'; \&{"handle_fieldcode_$code"} }; ...handle expections somehow?... die if !$handler; $handler->(@args);

    With hash;

    my $handler = $lookup{$code}; die if !$handler; $handler->(@args);

    with a one-time setup of

    my %lookup = map { no warnings 'refs'; $_ => \&{"handle_fieldcode_$_"} } 'A'..'Z'; ...modify %lookup for expections...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://760224]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2016-10-28 23:37 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (387 votes). Check out past polls.