Hello brothers and sisters

I am writing a code that, at a certain point, will have to modify some data on a directory server; what I would like to do is to make my program work in a transaction-like fashion, that is: if any of the update fails, rollback all the changes we made so far and then stop. I am using the perl-ldap-0.26 distribution.

I sketched the snippet that should do the transaction:

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!" ; }

Is it ok? Am I forgetting something important? Could I code it in a better way?

Thanks for help

Ciao!
--bronto

# Another Perl edition of a song:
# The End, by The Beatles
END {
  $you->take($love) eq $you->make($love) ;
}


In reply to how to build a transaction? by bronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.