On Fri, Apr 02, 2004 at 03:26:00AM -0800, Randal L. Schwartz wrote:
> >>>>> "Tim" == Tim Bunce <[EMAIL PROTECTED]> writes:
> 
> Tim> But as I tinker with it I wonder if it's a solution chasing a problem.
> Tim> Where, and how, and why, would it be used?
> 
> I used something like this in
> http://www.stonehenge.com/merlyn/LinuxMag/col52.html, although it was
> layered via Class::DBI and called "atomically".

The use in make_working_atomically() doesn't seem to need it, but the
create_or_make_todo() does. Thanks for a real example.

> It's useful when you have a pattern of "do all these things together,
> or roll them back together".  That pattern comes up a lot,

I tend to work with databases and applications that always use
transactions or never use transactions. So I'm not really familar
with the "pattern". 

Tim.

p.s. Looks like you have the same wantarray bug as I had.
The wantarray inside the eval will pick up the context of the eval
not the atomically() sub. I suspect that's a common problem.
I've sent a docs bug report.

$ perl -l -we 'sub foo { eval { print (wantarray ? "list" : "scalar") };1 }; $a=foo; 
@a=foo;'
scalar
scalar
$ perl -l -we 'sub foo {   do { print (wantarray ? "list" : "scalar") };1 }; $a=foo; 
@a=foo;'
scalar
list

> so having a wrapper to add the external logic is nice.
> 
>   sub atomically {
>     my $class = shift;
>     my $action = shift;         # coderef
>     local $class->db_Main->{AutoCommit}; # turn off AutoCommit for this block
> 
>     my @result;
>     eval {
>       @result = wantarray ? $action->() : scalar($action->());
>       $class->dbi_commit;
>     };
>     if ($@) {
>       warn "atomically got error: $@";
>       my $commit_error = $@;
>       eval { $class->dbi_rollback };
>       die $commit_error;
>     }
>     die $@ if $@;
>     wantarray ? @result : $result[0];
>   }

Reply via email to