Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: What I Most Recently Learned in Perl, But Should Have Already Known

by sfink (Deacon)
on Aug 18, 2006 at 16:48 UTC ( #568217=note: print w/ replies, xml ) Need Help??


in reply to Re^2: What I Most Recently Learned in Perl, But Should Have Already Known
in thread What I Most Recently Learned in Perl, But Should Have Already Known

I always run into the problem of the default case when I use that -- what if $function isn't a key in %functions? The error message "Undefined subroutine &main:: called" can be very difficult to figure out.

And the straightforward fix isn't all that pretty:

($functions{$function} || $functions{default})->();
But I admit that I still use it. With comments, if there is any chance that someone with a capacity to do me bodily harm might read the code.


Comment on Re^3: What I Most Recently Learned in Perl, But Should Have Already Known
Download Code
Replies are listed 'Best First'.
Re^4: What I Most Recently Learned in Perl, But Should Have Already Known
by izut (Chaplain) on Aug 18, 2006 at 20:43 UTC

    I think this way is prettier.

    sub do_something { my ($function) = @_; $function = 'default' unless exists $functions{$function}; $functions{$function}->(); }

    Igor 'izut' Sutton
    your code, your rules.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2015-07-31 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (280 votes), past polls