> > here you create a global variable called 'gvar'
> > > $t->param(gvar     => "global variable");
> > 
> > here you are creating a loop specific variable called 'gvar'
> > >     { loopvar => "GVar should be set",         gvar => "Loop GVar 1" },
> > 
> > These two 'gvar's are two different variables. 
> > 
> > In this case
> > >     { loopvar => "GVar should be blank again", gvar => undef         },
> > 
> > you are saying that the loop specific 'gvar' is not part of the loop - ie 
> > equivalent to:
> >        { loopvar => "GVar should be blank again" }
> 
> here is where i think we differ... 
> 
> 1. $href = { loopvar => "GVar should be blank again", gvar => undef }
> to me (and in general perl programming is VERY different from: 
> 2. $href = { loopvar => "GVar should be blank again" }

umm... not quite... Perl treats 'unknown' and 'undef' as the same thing if "use 
strict" is in effect (which it always should be) - and this is a compile-time test. 
(Aside:  I think 'exists' on a plain scalar / array_ref would solve this problem - but 
Perl doesn't have that capability).

Perl will try to lookup 'gvar' in its symbol table.  If the value doesn't exist in the 
symbol table, then any use of the 'gvar' gets flagged as a warning - this is a 
run-time test.

Hash'es on the other hand, always get autovivified, thus 'exists' makes sense.

Relating this to TMPL_LOOP's... the question becomes, "Is a variable inside a loop 
considered to be equivalent to a hash key, or equivalent to a variable?", 
alternatively it could be asked "What is the 'strict'ness of TMPL_LOOP variables (as 
compared to Perl's use of 'strict'ness)?".
 
> In (1), you are explicitely saying that you want "gvar" to be undefined.
> In (2), you are not saying that... you are effectively saying that you
> don't care what "gvar" is...

no - in (2) I said nothing about gvar - I didn't say "I dont care"
 
> To see how perl treats them differently, here is an example of a very
> common function (keys) that returns all the keys in a hash... 
> 
> (1) perl  > print "Keys: ". join(',', keys %$href) . "\n";
> (1) output> Keys: gvar,loopvar
> 
> (2) perl  > print "Keys: ". join(',', keys %$href) . "\n";
> (2) output> Keys: loopvar

Since we are talking about TMPL_LOOP variables, not Perl hash keys, it gets back to 
the question(s) posed above.

> Another example:
> 
> -- test.pl --
> #!/usr/bin/perl
> my $tvar = "this is a test";
> $tvar = undef;
> print "Tvar=$tvar\n";
> -----------------------------------------
> The above prints: Tvar=

true - but only if you dont do:

use strict;
use warnings;

... and I cant find a single reference suggesting to not use the 'use's.


> Now, so in the same context if "undef" should mean... take the outer
> scope, then perl should have ignored my "$tvar = undef" line... 

No - setting a value to 'undef' means - "remove me from the symbol table" - it doesnt 
mean, "make my value to be 'undef'" (although you get this behaviour if 'use strict' 
is not in effect).


> I think its definitely a bug :-) 
> It has infact been flagged before as well... its partially on a thread
> at:
> http://www.mail-archive.com/[EMAIL PROTECTED]/msg00728.html
> although that thread digressed a bit... but this topic has been bought
> up a few times... so maybe we can find more threads about it in the
> history of the list... 

fair enough - although, that doesn't constitute a reason to modify the behaviour.  A 
change to the code, should require us to agree on the answer to the question(s) posed 
above.

Mathew


> 
> 
> 
> 
> 
> > 
> > thus requesting the outer global gvar to come into scope.
> > 
> > Consider:
> > 
> > t->param(gvar => "Global gvar");
> > my $gvar_val;
> > my @loop;
> > for.... {
> >   if (something) {
> >     $gvar_val = "Loop GVar 1";
> >   } elsif (somethign else) {
> >     $gvar_val = "";
> >   }
> >   my $tmp;
> >   $tmp->{loopvar} = ...;
> >   $tmp->{gvar} = $gvar;
> >   push @loop, $tmp;
> > }
> > t->param(testloop =>[EMAIL PROTECTED]);
> > 
> > What would you expect the TMPL_LOOP to do in that example? 
> > I would have expected that the third iteration to use the global gvar value.
> > 
> > Mathew
> > 
> > 
> > > ========================================================================
> > > test.tmpl:
> > > ----------
> > > Global Variable gvar is: <tmpl_var name=gvar>
> > > 
> > > Loop: <tmpl_loop name=testloop>
> > >         loopvar: <tmpl_var name=loopvar>
> > >         gvar   : <tmpl_var name=gvar>
> > >       </tmpl_loop>
> > > ========================================================================
> > > test.pl
> > > -------
> > > 
> > > use strict;
> > > use HTML::Template;
> > > 
> > > my $t = new HTML::Template(filename => "test.tmpl", global_vars => 1,)
> > > || die;
> > > 
> > > my $testloop = [
> > >     { loopvar => "GVar should be set",         gvar => "Loop GVar 1" },
> > >     { loopvar => "GVar should be blank",       gvar => ""            },
> > >     { loopvar => "GVar should be blank again", gvar => undef         },
> > > ];
> > > 
> > > $t->param(gvar     => "global variable");
> > > $t->param(testloop => $testloop);
> > > print $t->output();
> > > ========================================================================
> > > 
> > > When you run test.pl.. you get the following output:
> > > ------------------------------------------------------------------------
> > > Global Variable gvar is: global variable
> > > 
> > > Loop: 
> > >         loopvar: GVar should be set 
> > >         gvar   : Loop GVar 1
> > >       
> > >         loopvar: GVar should be blank
> > >         gvar   : 
> > >       
> > >         loopvar: GVar should be blank again
> > >         gvar   : global variable
> > > ------------------------------------------------------------------------
> > > 
> > > Now, the issue is that "gvar" in the third iteration of the loop,
> > > "should have been blank"... i explicitely set it to "undef" in the
> > > code.. however, since global_vars was on, it ignored my "undef". 
> 
> 
>


-------------------------------------------------------
This SF.Net email is sponsored by: SourceForge.net Broadband
Sign-up now for SourceForge Broadband and get the fastest
6.0/768 connection for only $19.95/mo for the first 3 months!
http://ads.osdn.com/?ad_id%62&alloc_ida84&op=click
_______________________________________________
Html-template-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/html-template-users

Reply via email to