Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: calling a function as a value from a hash

by rodion (Chaplain)
on Apr 15, 2007 at 18:01 UTC ( [id://610214]=note: print w/replies, xml ) Need Help??


in reply to calling a function as a value from a hash

Since the discussion is becoming one of style preferenece, I'll add mine. Dispatch tables are often a suprise to someone reading through code. It's common for there to be only one in a module, if that, so it's good to call the reader's attention to what's going on. While I might use
$op_map{$op_choice}->(@args);
for a one-off, or where I was using it a lot, for most code I'd do something along the lines of naikonta's later suggestion, only with some correspondence between the names, as in
my $op = $op_map{$op_choice}; $op->(@args);
This separates the lookup from the invocation, so the invocation is easier to see. It also makes adding a default function easier, as naikonta already showed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-29 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found