Bob Showalter wrote:
> ...
> But foreach loops are funny. Try this:
>
> use strict;
>
> our $x = "Hello";
Aargh! That should be "my". To much confusion in my cutting/pasting.
>
> printx();
> for $x (1..3) {
> print "$x\n";
> printx();
> }
> printx();
>
> sub printx { print "$x\n"; }
>
> prints:
> Hello
> Hello
> Hello
> Hello
> Hello
>
> Hmm, now try it with "our":
>
> use strict;
>
> our $x = "Hello";
This "our" is correct.
>
> printx();
> for $x (1..3) {
> printx();
> }
> printx();
>
> sub printx { print "$x\n"; }
>
> prints:
> Hello
> 1
> 2
> 3
> Hello
>
> The loop variable can either be a global (default) or a lexical
> scoped to the loop. It can't reuse a lexical in an outer scope. Also,
> when a global is used, it is localized to the loop.
>
> So, the OP's problem can be addressed by:
>
> 1. Using the global variable.
>
> 2. Passing the variable to the function.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]