my $value = "four"; # lexical variable
my @array = ( "one", "two", "three" );
foreach $value (@array)
{
# variable $value inside the loop is set
# to each element of @array at runtime
print "foreach value: $value\t";
function();
}
# outer scope lexical $value visible here
sub function
{
# outer scope lexical $value visible here
# with its initial value
print "function value: $value\n";
}
####
$value = "four";
my @array = ( "one", "two", "three" );
foreach $value (@array)
{
print "foreach value: $value\t";
function();
}
sub function {
print "function value: $value\n";
}
__END__
foreach value: one function value: one
foreach value: two function value: two
foreach value: three function value: three
####
my $value = "four";
my @array = ( "one", "two", "three" );
foreach $value (@array) # note outer $value variable
{
print "foreach value: $value\t";
function();
sub function {
print "function value: $value\n";
}
}
__END__
foreach value: one function value: four
foreach value: two function value: four
foreach value: three function value: four
####
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";
}
}
__END__
foreach value: one function value:
foreach value: two function value:
foreach value: three function value: