in reply to Re: why need my in a foreach loop?
in thread why need my in a foreach loop?
Here's the difference:
use strict; use warnings; use 5.010; our $foo = 'hello'; sub f { print("$foo\n"); } for my $foo (qw( a b c )) { f(); } --output:-- hello hello hello
In ikegami's example, the my is omitted in the for loop, which causes the for loop to change the global variable $foo--and the subroutine prints the global $foo (i.e. the subroutine is a closure).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: why need my in a foreach loop?
by ikegami (Patriarch) on Nov 28, 2010 at 23:23 UTC | |
by 7stud (Deacon) on Dec 02, 2010 at 03:46 UTC | |
by ikegami (Patriarch) on Dec 02, 2010 at 16:38 UTC |