Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Too many "or die" clauses?

by TGI (Parson)
on Jun 16, 2008 at 22:11 UTC ( [id://692393]=note: print w/replies, xml ) Need Help??


in reply to Too many "or die" clauses?

You could use a dispatch table and a loop to simplify repetitive code.

use constant { TYPE => 0, ACTION => 1, EXCEPTION => 2, }; my @tasks = ( # TYPE ACTION EXCEPTION [ 'system', "foo --bar baz", "foo failed." ], [ 'chdir', 'foobar', "Couldn't chdir to foobar." ], [ 'system', "foo --bar baz2", "Couldn't pull it off." ], [ 'system', "foo2 --bar baz", "No joy. foo2 had problems." ], [ 'chdir', '..', "Couldn't go back up to base dir." ], [ 'system', "bar --foo baz", "Darn. bar didn't make it." ], ); %TASK_DISPATCHER = ( system => sub { system( $_[ACTION] ) == 0 or die "$_[EXCEPTION]\n"; }, chdir => sub { chdir $_[ACTION] or die "$_[EXCEPTION] $!\n"; }, '' => sub { die "Illegal task type '$_[TYPE]' specified\n"; } ); foreach my $task ( @tasks ) { my $type = exists $TASK_DISPATCHER{$task->[TYPE]} ? $task->[TYPE] : ''; $TASK_DISPATCHER{$type}->( @$task ); }


TGI says moo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found