You'll have to forgive me, I didn't see the original code before it was changed. However, the following simple test works fine for me. It even handles the case where you change the value of the handle after use'ing the module. The key, of course, being that you have to explicitly take the reference to the variable in the use line (and never dereference it in the module until you're actually using it).

Test Module:

package MyTst; use warnings; use strict; our $Object; sub import { my $class = shift; $Object = shift; { no strict 'refs'; *{caller().'::transaction'} = \&transaction; } } sub transaction(&) { $$Object->begin(); shift()->(); $$Object->end(); } 1;

Test program:

#!/usr/bin/perl use warnings; use strict; our $tst = TstPkgA->new(); use MyTst \$tst; transaction { print "Transaction A!\n"; }; $tst = TstPkgB->new(); transaction { print "Transaction B!\n"; }; exit; #*************** Test Classes ***************# package TstPkgA; sub new { return bless {}, 'TstPkgA'; } sub begin { print "TstPkgA::begin\n"; } sub end { print "TstPkgA::end\n\n"; } package TstPkgB; sub new { return bless {}, 'TstPkgB'; } sub begin { print "TstPkgB::begin\n"; } sub end { print "TstPkgB::end\n"; }

Output:

TstPkgA::begin Transaction A! TstPkgA::end TstPkgB::begin Transaction B! TstPkgB::end

Of course, none of this helps with the case where you want multiple packages to be able to use their own transaction handle. *shrug*

Update: I vote for crenz's way of handling this. You could do it (even the for-style) with source filters. I'm not sure I agree that filters are really a bad way to go with this, as you are adding syntax to the language. *shrug* As long as you handle it with care, you shouldn't necessarily add any parsing bugs. Update2: Heck, you could even steal code from TheDamian's Switch and blame any bugs on him. ;)

bbfu
Black flowers blossom
Fearless on my breath


In reply to Re3: RFC: Transactions.pm by bbfu
in thread RFC: Transactions.pm by Juerd

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.