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.

Replies are listed 'Best First'.
Re: help with scoping (code), part 2
by danger (Priest) on Nov 04, 2001 at 03:04 UTC

    One technique for 'static' variables in Perl (that you should be able to adapt to your underlying problem) is to wrap the sub in a bare block (or possibly a BEGIN block depending on initialization requirements and such). Your pseudo-code example becomes:

    #!/usr/bin/perl -w use strict; foo(4); foo(); { my $bar; sub foo { $bar = shift unless defined $bar; print "$bar\n"; } } __END__

    Which, in the case of an expensive initialization can be along the lines of:

    #!/usr/bin/perl -w use strict; print scalar localtime(),"\n"; slow_once(5); slow_once(); { my $expensive_thingy; sub slow_once { unless (defined $expensive_thingy) { # make expensive thingy: sleep 5; $expensive_thingy = shift; } print scalar localtime()," :$expensive_thingy:\n"; } } __END__ OUTPUT: Sat Nov 3 15:55:31 2001 Sat Nov 3 15:55:36 2001 :5: Sat Nov 3 15:55:36 2001 :5:

    If you wanted to create more than one expensive thingy, go with a parameterized sub that sets up the expensive thingy and returns a closure that does what you want with it.

Re: help with scoping (code), part 2
by traveler (Parson) on Nov 04, 2001 at 03:03 UTC
    When I want a static like that I do:
    { my $bar; sub foo { ... } }
    This creates foo local to a block including just foo and bar.

    HTH, --traveler

    update: fix tiny typo