in reply to Tricky scope problem
And you've made debugging tricky by not providing code that clearly replicates your issue - neither Foo nor Bar are ever called or referenced, your global $gVar is never assigned a value and the flow of your program is unclear. Please read I know what I mean. Why don't you?.
Based on your comments, it looks like you expect to assign $gVar a value as a loop index and then maintain that value when Bar is called within the loop. However, when you use $gVar as the index on your foreach, you actually create a lexically scoped copy (or rather a local alias for the list element). When you then call Bar, it only sees the global. It's discussed (briefly) in Foreach Loops. Perhaps this example will provide clear flow:
my $var = 5; foreach $var (1 .. 3) { print $var; print_var(); } print $var; sub print_var {print $var};
Outputs: 1525355
Update: Cleaned up some unclear language.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tricky scope problem
by lostjimmy (Chaplain) on Apr 11, 2009 at 00:07 UTC | |
by AnomalousMonk (Archbishop) on Apr 11, 2009 at 00:33 UTC | |
by lostjimmy (Chaplain) on Apr 11, 2009 at 15:42 UTC | |
by kennethk (Abbot) on Apr 11, 2009 at 13:19 UTC | |
by lostjimmy (Chaplain) on Apr 11, 2009 at 15:50 UTC |