in reply to foreach localizing variables

$value in the foreach loop is not a real variable, it is an alias to each element in the array. As perrin said, pass the value as a parameter to the subroutine. Global variables are A Bad Thing, except when you really need them (which is not often).

By the way, things may be clearer if you:
use strict; use warnings; my $value = "four"; my @array = ( "one", "two", "three" ); foreach my $value (@array) { print "foreach value: $value\t"; function(); } sub function { print "function value: $value\n"; }
Spot the difference.

Replies are listed 'Best First'.
Re^2: foreach localizing variables
by educated_foo (Vicar) on Feb 07, 2008 at 17:35 UTC
    The spotting is easier if you eliminate unnecessary junk...
    $value = "four"; @array = ( "one", "two", "three" ); foreach my $value (@array) { print "foreach value: $value\t"; function(); } sub function { print "function value: $value\n"; }