in reply to Re: $_ getting clobbered by inner loop.
in thread $_ getting clobbered by inner loop.

Ah, I see. Thanks. Can you explain the philosophy behind this design choice? That is, does localizing $_ in for loops, but not in while loops, provide something useful to Perl? Thanks. -db
  • Comment on Re^2: $_ getting clobbered by inner loop.

Replies are listed 'Best First'.
Re^3: $_ getting clobbered by inner loop.
by SuicideJunkie (Vicar) on Apr 09, 2008 at 17:59 UTC

    Localizing $_ in a while loop would cause any changes to that variable to mysteriously be lost when you exit the while.

    Code should not mess around with things that are not its concern, since that would generate unintended and unexpected side effects. The while loop does not inherently use $_, so therefore it should not localize or otherwise muck up $_.

    The same reasoning would explain why a while loop does not automatically localize @test_files as well.

Re^3: $_ getting clobbered by inner loop.
by ikegami (Patriarch) on Apr 10, 2008 at 00:04 UTC
    Because $_ has nothing to do with while loops. You're looking at one specific while loop (while (defined($_ = <FILE>)) when asking that question, but while is used in many other ways too. For example, why should Perl localize $_ in while (@todo)? Or in while (my ($key, $val) = each %hash)?
Re^3: $_ getting clobbered by inner loop.
by Anonymous Monk on Apr 09, 2008 at 14:37 UTC
    larry he is not