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.
You are accessing a localized version of the `$num` variable, like Sobrique says. What you intended was to use a reference to the variable `$num`. Which is what I show here:

```
my ( $func, @list, $num );
$func = sub {
    print $$num // "undef"; print "\n";
};

@list = ( 1,2,3 );

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

```

You can also use the global version of the variable, instead of the lexical:

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

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

@list = ( 1,2,3 );

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

```

But this is a silly way to encapsulate this. You should not use global variables inside subroutines. This is not good practice. Instead, pass the value to the sub and access it via the `@_` variable (in this case by the first array index `$_[0]`):

```
my $func = sub {
    print $_[0] // "undef"; print "\n";
};

my @list = ( 1,2,3 );

for ( @list ) {
    $func->($_);
};

```

I also fixed some idiomatic coding style above.