http://qs1969.pair.com?node_id=123097

deprecated has asked for the wisdom of the Perl Monks concerning the following question:

As I outlined in 123089, I have some questions about scoping.

In college, we learned about 'static' variables. Alas, that was long enough ago that I dont recall whether it was pascal or C in which they were available.

At any rate, the gist of it was that I could expect something like this:

# warning, pseudocode foo(4); # prints "4" foo(); # prints "4" sub foo { static int bar; bar = shift() unless defined bar; print bar; }
In the above, because foo received a value for bar, and bar was static, i could continue to use it after the sub had exited.

The reason for this is I would like a persistance of objects. Lets say that I do this:

sub too_slow { my $object = ReallyHuge::SlowObject -> new(%args); # do stuff with $object return "stuff"; }
The problem is $object might take forever to create, or, even worse, it might have a socket connection in it (as is the case with MP3::Napster), and continually recreating it is not good. What I would like to do is this:
sub less_slow { my $object; $object = $dbh -> @{selectall_arrayref("select object from storage") +}[0]; if ($object -> exists()) { # behave normally } else { $object = ReallyHuge::SlowObject -> new(%args); $dbh -> do("update storage set object='$object'"); } }
I realize that some trickery would have to be employed, like using storable or dumper to actually store the object rather than a direct reference to it.

This would allow me to call less_slow from within @children without losing connections, re-creating the object, et cetera.

thanks,
brother deprecated

--
Laziness, Impatience, Hubris, and Generosity.