To get the "won't stay shared" warning, you have to have something like:

sub a { my $x; sub b { $x } }
Your my $foo isn't inside a subroutine so you just have one lexical variable scoped to that bare block. If the my was inside your up subroutine, then each call to up would create a new instance of $foo and that would mean that your get subroutine wouldn't know which instances of $foo that you wanted it to use.

Since the bare block is only ever executed once, your my only ever creates one instance of the variable so it "stays shared". When a my is compiled, it declares the variable (which leaves it set to undef). When the my is executed, it initializes the variables. Executing the my a second or subsequent time creates a new instance of the variable and then initializes that. Even if you require or use a module more than once, the code for the module is only compiled and executed once (but the code for subroutines in that module could be executed more times or zero times).

The code you used would be a fine way to store module-specific results. If your block were in a different module, then it would be the module and not the caller storing them.

Updated.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Why no stay shared warning? by tye
in thread Why no stay shared warning? by tenya

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.