Um, Don't Do That Then?

http://perldoc.perl.org/perlsyn.html:

NOTE: The behaviour of a my statement modified with a statement modifier conditional or loop construct (e.g. my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
A short explanation of the behavior you are seeing: my has both a compile-time and run-time effect. At compile-time, it establishes a lexical variable for the remainder of the current scope, so any mention of the variable name is bound to an offset in an area known as the pad. All pads are initially set up with undefined variables. When a scope is exited, any lexicals that were actually used are reset to undef so they are ready for the next time the scope is entered. When the my is reached at run-time, it records that the lexical was used and needs to be cleared when the scope is left. By having the if there, you are preventing the my() from executing at run-time some of the time, so the scope-exit code doesn't know that the lexical was used, so it isn't reset to undef, so next time the scope is entered the previous value is still there.

In reply to Re: "if" in a my declaration statement causes problem by ysth
in thread "if" in a my declaration statement causes problem by ganeshk

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.