in reply to variable like $_

You cannot (at least not in Perl <= 5.8.8; I do not know about 5.10 or Perl 6). There is only one $_ and the second use of it destroys the contents of the first use.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: variable like $_
by JavaFan (Canon) on Feb 04, 2009 at 10:06 UTC
    There is only one $_
    True.
    and the second use of it destroys the contents of the first use.
    False.
    foreach ("a", "b") { # First use of $_ foreach ("c", "d") { # Second use of $_ say; } say; # 'Old' value of $_ } __END__ c d a c d b
    The second use of '$_' doesn't destroy the content of the first use. It stores it somewhere else, and restores the value after the second use is finished. That's the principle of local. And that's what's happening with a foreach loop.
      "Destroy was indeed not the right word, "hide" or "shadow" would have been better.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James