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 );
}