But still japhy is right.

A closure is a subroutine that contains a reference to a lexical bound to it at its creation. In the case of named functions created within a scope where Perl thinks that you are likely to have multiple variables masquerading as that lexical, Perl gives you a warning because only one variable will get bound to the function, and on your next time through the code what goes in that lexical will not be reflected in the named sub.

That is why you got a warning.

However it worked anyways because you expected the global to be tied to the variable created with that lexical scope that it is, in fact, tied to.

And it would have worked had you pushed it farther because after each pass you would wipe out any existing function of that name and create a new one bound to the current lexical.

So you are being warned about something that might not work as you expect which actually will work as you expect. But it works exactly as it is supposed to. Is that a bug? Not in my books. Perl does what it is supposed to, and warnings are flagged specifically because they are constructs which indicate that things may be wrong in your program, but which might be just fine in reality.

In reply to Re (tilly) 4: A Real Closure by tilly
in thread Unusual Closure Behaviour by tachyon

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, 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, 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.