Subject: | Variable scoping wierdness |
When using a variable to loop through an array, it will mask a variable of the same name within the same scope. This is reasonable. However, at the end of the loop, it will still be masking the variable... an example of this is the code below:
Show quoted text
------ Start of Sample Code -----
#!/usr/bin/perl -w
use strict;
use Template; # version 2.06
use Data::Dumper;
my $tt = Template->new();
my $vars = {
foo => 'bar',
array => [1..4]
};
my $template = <<TEMPLATE;
[%- FOREACH foo = array %]
Hello!
[%- END %]
[% foo %]
TEMPLATE
$tt->process ( \$template, $vars );
----- End of Sample Code -----
I would expect this code to be functionally equivalent to the following perl script:
----- Start of perl Script -----
#!/usr/bin/perl -w
use strict;
my $foo = 'bar';
foreach my $foo (1..4) {
print "Hello!\n";
}
print "$foo\n";
----- End of perl Script -----
However, at the end of the FOREACH loop in the template, the value of 'foo' is 4, not 'bar'. This is counterintuitive, and as far as I can see undocumented. Not a massive bug, but something which should IMHO be addressed... it kept me stumped for quite a while.