Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: RFC: Transactions.pm

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


in reply to RFC: Transactions.pm

Why don't you just export $T? You could roll your own import for this module to alias it to any name requested. Instead of saying (as originally)
use Transaction \$dbh;
one would write the more familiar looking
use Transaction qw($dbh);
and get $T exported to their namespace as $dbh.

Makeshifts last the longest.

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

    Why don't you just export $T?

    That wouldn't allow different packages to have their own $Ts.

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

      If the goal is a module that can be used in multiple instances though then a package global is a horrible interface to begin with. How about something like this?
      use Exporter::Tidy default => [ qw(transaction select_handle commit ro +llback) ]; sub select_handle { require Carp; Carp::croak("Can't select handle outside transaction"); } sub _cant_change { require Carp; Carp::croak("Can't change handle during transaction"); } sub transaction (&) { my ($block) = @_; my ($caller, $handle); my $caller = caller(); local *{"${caller}::select_handle"} = sub { $handle = shift; *{"${caller}::select_handle"} = \&_cant_change; }; # ... }
      To use it you then have to say
      transaction { select_handle($dhb); # ... };
      which allows to use the module for as many handles even within the same package as you like. You can even nest transactions with no adverse effects.

      Makeshifts last the longest.

        transaction { select_handle($dhb); ... };

        Not an option, because you need the handle before you begin the transaction. Won't begin the transaction inside the block, because that would allow stuff to be done outside the transaction while visually, it's in the transaction code block.

        Simple to use modules have their limitations. The limitation of this one is that you shouldn't use nested transactions. I think that's acceptable in most cases.

        I've only seen nested transactions being used to rollback multiple things. But it's easy to create a wrapper module for that:

        package NeedsAName; sub new { return bless \@_, shift } sub AUTOLOAD { $_->$AUTOLOAD(@_) for @{+shift}; } package main; use Transactions; my $dbh1 = DBI->connect(...); my $dbh2 = DBI->connect(...); our $T = NeedsAName->new($dbh1, $dbh2); transaction { ... };

        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://253639]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2024-04-23 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found