Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

papidave's scratchpad

by papidave (Monk)
on Oct 03, 2007 at 19:53 UTC ( #642489=scratchpad: print w/replies, xml ) Need Help??

I've been banging my head on the table with Apache::Session::MySQL today. Consider:

eval { ( my %session, $ok ); $ok = tie( %session, 'Apache::Session::MySQL', undef, ... ); if ( $ok ) { ... do something with %session ... } untie %session; }
This doesn't work from mod_perl, because the untie throws an exception:
untie attempted while 1 inner references still exist at foo.pl line 250.
Since I don't use any explicit references to %session, this caught me by surprise. Can you see the bug?

The trick here is to read the fine print on the return code provided by tie:
The object returned by the constructor is in turn returned by the tie function, which can be useful if you want to access other methods in CLASSNAME
In other words, I can't untie %session because $ok is still holding a reference to it. One fix is to assign $ok to a constant if and only if tie returns a non-false value, e.g.:
eval { ( my %session, $ok ); tie( %session, 'Apache::Session::MySQL', undef, ... ) and $ok = 1; if ( $ok ) { ... do something with %session ... } untie %session; }
This eliminates the duplicate reference to %session, so my session properly goes away. Thank you each for the alternate solutions suggested by monks in the CB:
  • if ( tie( ... ) ) { $ok = 1 }
  • $ok = tie( ... ) && 1;
  • and one particularly elegant answer, from tye:
  • $ok = !! tie( ... );
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[stevieb]: talexb I'm after by a few years, but we can still be friends because you live in my hometown ;)
[LanX]: White House
[LanX]: next inauguration ?

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2017-08-18 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (303 votes). Check out past polls.

    Notices?