But note that you are now responsible for manually
syncronizing the variable you tie with the variable that
gets untied. I would hide that with something like (untested):
sub lazy_init {
tie $_[0], "MyGlobals", $_[1], \($_[0]);
}
# Then elsewhere
lazy_init(my $baz3, "LegacyRoutines::baz");
print $baz3, "\n";
print $baz3, "\n";
If you are going to propagate bad, unmaintainable hacks,
there is still no excuse to not try to identify and kill
potential causes of error. With enough hacks you can even
make it look halfway decent...
Of course it isn't really halfway decent. For instance if you avoid using the variable, you will have a memory leak. One solution is to manually untie somewhere. Of course
that is something that can easily go wrong. With 5.6.x
you can download and install WeakRef and use
that to break the self-reference. For older versions of
Perl you would need to untie. The cleanest safe way
to do that is to return a ReleaseAction handle
that will untie when it goes out of scope. Or you can
play with the reference counts directly at the C level.
With all of that, I think you get a reliable implementation
of this feature. I still don't like it though. |