Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by izut (Chaplain)
on Aug 17, 2006 at 17:28 UTC ( #567974=note: print w/replies, xml ) Need Help??


in reply to Re: 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

Continuing the if-else statements problem, the idiom I like most is:

my $function = 'a'; my %functions = ( 'a' => sub { ... }, 'b' => sub { ... }, ); $functions{$function}->();

It's amazing how this replace an entire switch or if-else statements.

Igor 'izut' Sutton
your code, your rules.

  • Comment on Re^2: What I Most Recently Learned in Perl, But Should Have Already Known
  • Download Code

Replies are listed 'Best First'.
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
    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.

      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://567974]
help
Chatterbox?
[marto]: "Eugenie Scott, executive director of the National Center for Science Education, dubbed this approach the Gish gallop, describing it as "where the creationist is allowed to run on for 45 minutes or an hour, spewing forth torrents of error that the
[marto]: evolutionist hasn't a prayer of refuting in the format of a debate." She also criticized Gish for failing to answer objections raised by his opponents"
[erix]: one would hope evolutionists haven't any prayers anyway
[marto]: obviously someone could be religious, but not creationist
[erix]: "Nothing in Intelligent Design makes sense except in the light of Creationism" <-- I made that one up myself (free after Dobzhansky )
[erix]: yes. Deplorable marto, deplorable.
[marto]: the situation seemed similar to this one, majority of the contributrions are nonsense, doesn't address any questions ...
[marto]: meh, I've been called worse :P

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-07-28 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (431 votes). Check out past polls.