in reply to Reference and $_ weirdness

foreach aliases the $_ or $notUsed variables to the indexed element. That means that any changes to those variables will also change the array. Since $_ is a global (local()ized by foreach), that means that any changes to $_ without localizing it again will change the aliased element.

Solution: make sure $_ is localized before modifying it. Or use a lexical variable instead, like you do.

Replies are listed 'Best First'.
Re^2: Reference and $_ weirdness
by Marcello (Hermit) on Jun 27, 2005 at 09:53 UTC
    Thank you for the clear answer.

    Indeed the $_ variable was not localized in the specific functions.

    Will check the rest of the application for this bug too.