in reply to Why the variable $n was undef?

See perlsyn on Foreach-Loops. The looop variable gets localized, that is, at loop end its value gets restored to what it was before the loop started.

Replies are listed 'Best First'.
Re^2: Why the variable $n was undef?
by JavaFan (Canon) on Aug 16, 2010 at 13:29 UTC
    That may be true, but it's complete irrelevant. $n isn't the loop variable, $_ is.

    IMO, this is a bug. Either there's an assumed block around the statement, or there isn't. If there is, the use of $n afterwards should be a compile time error; if it isn't, $n should be 2 afterwards. (As if the my wasn't in the same line).

Re^2: Why the variable $n was undef?
by appleii (Novice) on Aug 16, 2010 at 13:08 UTC
    and this?
    our $n = $_ foreach (1 .. 2); warn "\$n = $n";

      See my and our to find the difference.

      Just a something something...