Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: You can hack anything...

by clemburg (Curate)
on Aug 08, 2001 at 18:51 UTC ( [id://103068]=note: print w/replies, xml ) Need Help??


in reply to You can hack anything...
in thread Using tie to initialize large datastructures

I am definitively with you on the point of using so many globals. I would never consider to do something like this on many global variables just because of efficiency concerns. A design that needs such hacks is probably flawed.

OTOH, I found this to be an interesting problem with respect to tie() usage.

As for your suggestion - thanks! This really works. Funny. You can even use the code like it stands. Just pass in a ref to the lexical, and you're done. Like this:

my $baz3; tie($baz3, "MyGlobals", "LegacyRoutines::baz", \$baz3); print $baz3, "\n"; print $baz3, "\n";

Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com

Replies are listed 'Best First'.
Re (tilly) 2: You can hack anything...
by tilly (Archbishop) on Aug 08, 2001 at 20:40 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://103068]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-16 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found