I have an interpretation for your results but no time for a formal proove.

At compiletime the pads for the lexicals $x and $y are associated to the sub. With a recursiv call perl has to allocate a new pad for the function, instead of reusing the old one.

Furthermore the if-branches are optimised away, so no "my $x" is executed at runtime to resetting $x to undef, so your accessing in 2 and 4 the last setting in the same level.

in short, it's one of these strange side effects of optimising code away, using B:Deparse you will most probably code-chunks missing.

you can also try this, which results in an error

goto label; if (0) { label: print "huhu"; }
(untested)

so be aware of optimised code-chunks.


In reply to Re: Confusion over scope of "my" vars by Anonymous Monk
in thread Confusion over scope of "my" vars by Anonymous Monk

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.