Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: What's the best way to make my script execute different sections without using goto?

by revdiablo (Prior)
on Feb 24, 2009 at 21:03 UTC ( #746101=note: print w/ replies, xml ) Need Help??


in reply to What's the best way to make my script execute different sections without using goto?

I would use a dispatch table with a bunch of subroutines, one for each action. The dispatch table makes it easy to use the command line argument to call the proper bit of code. Simple example:

my %ACTION_NAMED = define_actions(); my $action = shift; # First argument my @args = @ARGV; # Remaining arguments if (exists $ACTION_NAMED{$action}) { $ACTION_NAMED{$action}->(@args); } else { $ACTION_NAMED{_default_}->(@args); } sub define_actions { return ( action1 => \&action1, action2 => \&action2, _default_ => \&usage, ); } sub action1 { ... } sub action2 { ... } sub usage { ... }


Comment on Re: What's the best way to make my script execute different sections without using goto?
Download Code
Re^2: What's the best way to make my script execute different sections without using goto?
by gctaylor1 (Hermit) on Feb 25, 2009 at 00:48 UTC
    I'm going to go read about dispatch tables

    Thank-you for your time and advice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-08-31 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls