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.


In reply to help with scoping (code), part 2 by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.