Question: ```
#!/usr/bin/env perl

use strict;
use warnings FATAL => qw ( all );

my ( $func, @list, $num );

$func = sub {
    print $num // "undef"; print "\n";
};

@list = ( 1,2,3 );

foreach $num ( @list ) {
    $func->();
};

```

This piece of perl prints 

```
undef
undef 
undef 

```

instead of 

```
1
2
3

```

The `$func` routine can see `@list`, so why not `$num`?

Answer: 
Because `foreach` loops implicitly localise their iterator variables. 

See: [`perlsyn`](http://perldoc.perl.org/perlsyn.html#Foreach-Loops)

> 
> The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop. This implicit localization occurs only in a foreach loop.
> 
> 
> 

But really - this doesn't come up often, because it's really bad form to do any sort of messing around with a loop iterator from outside. Narrow down your scope, and pass variables around to avoid bugs and troubleshooting pain.
I could quote perlsyn, but perlsyn does not appear to say clearly what is the case. The variable is not "localized", and there is nothing that "regains" its value upon exiting the loop. The outer variable is *hidden* when you declare the loop variable, and visible after you're out of the loop.

It implicitly *hides* the variable in the outer scope, which *retains* its value. 

The output from this code scrap will illustrate: 

```
use 5.022;
use strict;
use warnings FATAL => qw ( all );
use Data::Dumper;

my ( $func, @list, $num );

$func = sub {
    print $num // "undef"; print "\n";
};
$num = 101;
$func->();

my $np = \$num;
print Data::Dumper->Dump( [ $np ], [ '*np' ] );

@list = ( 1,2,3 );

foreach $num ( @list ) {
    print Data::Dumper->Dump( [ $num ], [ '*num' ] );
    print Data::Dumper->Dump( [ $np ], [ '*np' ] );
    my $np2 = \$num;
    print Data::Dumper->Dump( [ $np2 ], [ '*np2' ] );
    print Data::Dumper->Dump( [ $np ], [ '*np' ] );
    $$np++;
    $func->();
}

```

Which outputs this:

```
101
$np = \101;
$num = 1;
$np = \101;
$np2 = \1;
$np = \101;
102
$num = 2;
$np = \102;
$np2 = \2;
$np = \102;
103
$num = 3;
$np = \103;
$np2 = \3;
$np = \103;
104

```

Thus, inside the loop, the symbol `$num` simply does not refer to the memory pointed to by `$np`, but instead at the memory pointed to by `$np2`.