The key is "as late as possible". If you need to retain a value determined in a loop then the variable has to be declared outside the loop:
my $result;
for my $loopVar (...) {
...
$result = ...;
...
}
if (defined $result) {
# use the result
} else {
# result not determined (or undef)
}
Note that $loopVar is magical. It gets aliased to each value in the list that the loop iterates over. In particular It does not retain the value from the last iteration of the loop even if declared outside the loop! Always either use the default variable($_) or explicitly declare the loop variable as shown in the code sample.
Perl is environmentally friendly - it saves trees
|