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.
In reply to Re: Tricky scope problem
by kennethk
in thread Tricky scope problem
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |