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( ... );