Skip Menu |

This queue is for tickets about the Template-Toolkit CPAN distribution.

Report information
The Basics
Id: 221
Status: resolved
Priority: 0/
Queue: Template-Toolkit

People
Owner: Nobody in particular
Requestors: grimoire [...] corinne.cpio.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 2.06
Fixed in: (no value)



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.
The Template Toolkit isn't Perl and doesn't share Perl's scoping rules. The behaviour within the FOREACH block is entirely correct for our definition of "correct".
The Template Toolkit isn't Perl and doesn't share Perl's scoping rules. The behaviour within the FOREACH block is entirely correct for our definition of "correct".
Date: Wed, 30 Jan 2002 04:37:32 -0500
From: Alex Page <grimoire [...] corinne.cpio.org>
To: Andy_Wardley via RT <bug-Template-Toolkit [...] rt.cpan.org>
Cc: Alex Page <grimoire [...] corinne.cpio.org>
Subject: Re: [cpan #221] Variable scoping wierdness
On Wed, Jan 30, 2002 at 03:15:35AM -0500, Andy_Wardley via RT wrote: Show quoted text
> The Template Toolkit isn't Perl and doesn't share Perl's scoping > rules. The behaviour within the FOREACH block is entirely correct > for our definition of "correct".
Well, I had a peek through the documentation for FOREACH, and this behaviour isn't mentioned. Given it's counterintuitive for a perl coder, I think that some mention of it should be made... Alex -- KCBpd lWmulvo ECS+ m5 CPEIV B13 Ou Lmb Sc+isIC+ T++ A6LAT H6oe b5 D+ - See http://bob.bob.bofh.org/~giolla/bobcode.html for decoding Website: http://www.cpio.org/~grimoire Writing: http://www.livejournal.com/~diffrentcolours
From: grimoire [...] corinne.cpio.org
Please find attached a diff for docsrc/lib/dir/for which explains the behaviour to the user.
72a73,78 > However, under normal operation, the loop variable remains in scope > after the FOREACH loop has ended (caveat: overwriting any variable > previously in scope). This is useful as the loop variable is secretly > an iterator object (see below) and can be used to analyse the last > entry processed by the loop. >