Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: RFC: Transactions.pm

by Aristotle (Chancellor)
on Apr 28, 2003 at 11:19 UTC ( [id://253653]=note: print w/replies, xml ) Need Help??


in reply to RFC: Transactions.pm

I haven't tested this but believe it is cleaner to write as
sub commit () { ($T || ${ caller() . '::T' })->commit; goto END_XACTION; } sub rollback () { ($T || ${ caller() . '::T' })->rollback; goto END_XACTION; } sub transaction (&) { my ($block) = @_; local $T = ${ caller() . '::T' } or croak "\$T not set"; local $@; eval { $T->begin_work; $block->(); }; $@ ? rollback : commit; END_XACTION: die $@ . "commit not safe after errors, transaction r +olled back" if $@; }
I would suggest you work with die instead though - that will work with nested transactions and require fewer hoop jumps. You can then also easily add some more error checking:
sub commit { require Carp; Carp::croak("Can't commit outside a transaction"); } sub rollback { require Carp; Carp::croak("Can't rollback outside a transaction"); } sub transaction (&) { my ($block) = @_; my $caller = caller(); local *{"${caller}::commit"} = sub { die "!COMMIT\n" }; local *{"${caller}::rollback"} = sub { die "!ROLLBACK\n" }; local $@; eval { $T->begin_work; $block->(); }; if(!$@ or $@ eq "!COMMIT\n") { ${"${caller}::T"}->commit; } elsif($@ eq "!ROLLBACK\n") { ${"${caller}::T"}->rollback; } else { ${"${caller}::T"}->rollback; require Carp; Carp::croak($@ . "commit not safe after errors, transaction ro +lled back"); } }

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: RFC: Transactions.pm
by Juerd (Abbot) on Apr 28, 2003 at 12:16 UTC

    I would suggest you work with die instead though - that will work with nested transactions and require fewer hoop jumps. You can then also easily add some more error checking:

    I don't like your goto LABEL idea at all, but I do like the idea of using die very much. It even makes sense! :)

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      last is nothing but a glorified goto - esp the way your were using it.

      Makeshifts last the longest.

        last is nothing but a glorified goto

        But I still like it better. Loops, loop control operators, subroutines -- all glorified gotos, but I like the structured versions much better.

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-26 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found