in reply to Problem of context?
You have declared @test2 inside an if block inside the first while loop. As soon as execution leaves the if block the array goes out of scope. See Coping with Scoping for more details.
Update: Example - compare these two:
use strict; my $foo = 0; for (1..5) { $foo += $_; } print $foo;
and
use strict; for (1..5) { my $foo = 0; $foo += $_; } print $foo;
In each case the scope of $foo ends at the end of the block enclosing its declaration. Since the first example has the declaration outside the loop the scope persists to the end of the script. In contrast, the second example gives $foo scope only until the end of the for loop thus making the $foo in the print statement undeclared which will be trapped at compile time.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Problem of context?
by pabla23 (Novice) on Oct 16, 2014 at 09:53 UTC | |
by choroba (Cardinal) on Oct 16, 2014 at 09:55 UTC | |
by biohisham (Priest) on Oct 16, 2014 at 13:44 UTC | |
by pabla23 (Novice) on Oct 16, 2014 at 15:30 UTC | |
by Anonymous Monk on Oct 16, 2014 at 21:35 UTC |