my @commits = qw(user secg something more here) ; my @rollbacks ; my $do_rollback ; # dispatch table for commits my %dt_commit = ( user => sub { # try to update an entry or die # ...code here... }, secg => sub { # try to update another entry or die # ...code here... }, # ... et cetera... ) ; my %dt_rollback = ( # same as above, but this dispatch table contains # rollback subs instead of commit ones ) ; foreach my $cmt (@commits) { eval { &{$dt_commit{$cmt}} } ; if ($@) { @rollbacks = reverse @rollbacks ; $do_rollback = 'yes' ; last ; } push @rollbacks,$cmt ; } if ($do_rollback) { foreach my $rbk (@rollbacks) { # execute the rollback subs here } die "Something weird happened, stopping!" ; }