eval { TX: { # step 1: database $dbh->do("CREATE DATABASE foo"); push @undo, sub { $dbh->do("DROP DATABASE foo"); }; # step 2a: filesystem mkdir "/path/foo" or last TX; push @undo, sub { rmdir "/path/foo" }; # step 2b push @undo, sub { remove_tree "/path/foo" }; create_a_bunch_of_files_in("/path/foo") or last TX; # step 3: logger (usually also on filesystem) write_log("Database and files setup") or last TX; # step 4: email (it's okay to fail this step) send_email('foo@bar.com'); # mark success @undo = (); } # rollback, we force each step even though it dies for (reverse @undo) { eval { $_->() }; } };

Is there a module to do this sort of thing more clearly and correctly? In essence, I am changing different resources (filesystem, database, etc) in a number of steps. In the absense of a global transaction manager, the next best thing I can do is rollback/undo the previous steps should things go wrong in the middle.


In reply to Poor man's transaction construct by dgaramond2

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.