## Re: [htmltmpl] Patch to fix global_vars and loop problem...

 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: Mathew Robertson - 2004-05-12 22:57:11 ```hmm... I dont consider this to be a bug... here you create a global variable called 'gvar' > \$t->param(gvar =3D> "global variable"); here you are creating a loop specific variable called 'gvar' > { loopvar =3D> "GVar should be set", gvar =3D> "Loop GVar = 1" }, These two 'gvar's are two different variables.=20 In this case > { loopvar =3D> "GVar should be blank again", gvar =3D> undef = }, you are saying that the loop specific 'gvar' is not part of the loop - = ie equivalent to: { loopvar =3D> "GVar should be blank again" } thus requesting the outer global gvar to come into scope. Consider: t->param(gvar =3D> "Global gvar"); my \$gvar_val; my @loop; for.... { if (something) { \$gvar_val =3D "Loop GVar 1"; } elsif (somethign else) { \$gvar_val =3D ""; } my \$tmp; \$tmp->{loopvar} =3D ...; \$tmp->{gvar} =3D \$gvar; push @loop, \$tmp; } t->param(testloop =3D>\@loop); What would you expect the TMPL_LOOP to do in that example?=20 I would have expected that the third iteration to use the global gvar = value. Mathew > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > test.tmpl: > ---------- > Global Variable gvar is: >=20 > Loop: > loopvar: > gvar : > > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > test.pl > ------- >=20 > use strict; > use HTML::Template; >=20 > my \$t =3D new HTML::Template(filename =3D> "test.tmpl", global_vars = =3D> 1,) > || die; >=20 > my \$testloop =3D [ > { loopvar =3D> "GVar should be set", gvar =3D> "Loop GVar = 1" }, > { loopvar =3D> "GVar should be blank", gvar =3D> "" = }, > { loopvar =3D> "GVar should be blank again", gvar =3D> undef = }, > ]; >=20 > \$t->param(gvar =3D> "global variable"); > \$t->param(testloop =3D> \$testloop); > print \$t->output(); > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > When you run test.pl.. you get the following output: > = ------------------------------------------------------------------------ > Global Variable gvar is: global variable >=20 > Loop:=20 > loopvar: GVar should be set=20 > gvar : Loop GVar 1 > =20 > loopvar: GVar should be blank > gvar :=20 > =20 > loopvar: GVar should be blank again > gvar : global variable > = ------------------------------------------------------------------------ >=20 > 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".=20 ```

 [htmltmpl] Patch to fix global_vars and loop problem... From: simran - 2004-05-12 02:47:41 Attachments: html_template_v2_6.patch     test.pl     test.tmpl ```Hi Sam, et al, Here are the details of (what i consider to be a bug anyway) a bug in HTML::Template - its been bought up before, and you have mentioned that it does need fixing, but it annoyed me enough today to produce a patch for it and submit the patch :-) Take the following example: ======================================================================== test.tmpl: ---------- Global Variable gvar is: Loop: loopvar: gvar : ======================================================================== 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". Here's a patch that fixed the problem (using HTML::Template Version 2.6) (it might not be the most efficient.. i've only quickly written something that seems to work... but of course, you might want to check its efficiency etc...) ======================================================================== --- Template.pm.orig 2004-05-12 12:29:01.000000000 +1000 +++ Template.pm 2004-05-12 12:42:58.000000000 +1000 @@ -2464,6 +2464,11 @@ if (defined(\$value_type) and length(\$value_type) and (\$value_type eq 'ARRAY' or ((ref(\$value) !~ /^(CODE)|(HASH)|(SCALAR)\$/) and \$value->isa('ARRAY')))) { (ref(\$param_map->{\$param}) eq 'HTML::Template::LOOP') or croak("HTML::Template::param() : attempt to set parameter '\$param' with an array ref - parameter is not a TMPL_LOOP!"); + foreach my \$row (@\$value) { + foreach my \$key (keys %\$row) { + \$row->{\$key} ||= ""; + } + } \$param_map->{\$param}[HTML::Template::LOOP::PARAM_SET] = [@{\$value}]; } else { (ref(\$param_map->{\$param}) eq 'HTML::Template::VAR') or ======================================================================== simran. ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: Sam Tregar - 2004-05-12 04:14:15 ```On Wed, 12 May 2004, simran wrote: > + foreach my \$row (@\$value) { > + foreach my \$key (keys %\$row) { > + \$row->{\$key} ||= ""; > + } > + } Hmmmm. What happens when \$row->{\$key} is "0"? What happens when undef is set in a loop within a loop? I think the real fix is to find a way to use exists() rather than defined() when looking for the variable setting. That way it doesn't matter what the value of the var is, just whether it's set or not. Maybe you could give that a try? Thanks, -sam ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: simran - 2004-05-12 06:50:38 Attachments: html_template_v2_6.patch     test.pl     test.tmpl ```very good points :-) How is the attached as a patch... simran. On Wed, 2004-05-12 at 14:09, Sam Tregar wrote: > On Wed, 12 May 2004, simran wrote: > > > + foreach my \$row (@\$value) { > > + foreach my \$key (keys %\$row) { > > + \$row->{\$key} ||= ""; > > + } > > + } > > Hmmmm. What happens when \$row->{\$key} is "0"? What happens when > undef is set in a loop within a loop? > > I think the real fix is to find a way to use exists() rather than > defined() when looking for the variable setting. That way it doesn't > matter what the value of the var is, just whether it's set or not. > Maybe you could give that a try? > > Thanks, > -sam > > > ------------------------------------------------------- > This SF.Net email is sponsored by Sleepycat Software > Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to > deliver higher performing products faster, at low TCO. > http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3 > _______________________________________________ > Html-template-users mailing list > Html-template-users@... > https://lists.sourceforge.net/lists/listinfo/html-template-users ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: simran - 2004-05-18 06:07:26 ```Hi Sam, Did you decide on what to do with the submitted patch? simran. On Wed, 2004-05-12 at 16:51, simran wrote: > very good points :-) > > How is the attached as a patch... > > simran. > > > > > On Wed, 2004-05-12 at 14:09, Sam Tregar wrote: > > On Wed, 12 May 2004, simran wrote: > > > > > + foreach my \$row (@\$value) { > > > + foreach my \$key (keys %\$row) { > > > + \$row->{\$key} ||= ""; > > > + } > > > + } > > > > Hmmmm. What happens when \$row->{\$key} is "0"? What happens when > > undef is set in a loop within a loop? > > > > I think the real fix is to find a way to use exists() rather than > > defined() when looking for the variable setting. That way it doesn't > > matter what the value of the var is, just whether it's set or not. > > Maybe you could give that a try? > > > > Thanks, > > -sam > > > > > > ------------------------------------------------------- > > This SF.Net email is sponsored by Sleepycat Software > > Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to > > deliver higher performing products faster, at low TCO. > > http://www.sleepycat.com/telcomwpreg.php?From=osdnemail3 > > _______________________________________________ > > Html-template-users mailing list > > Html-template-users@... > > https://lists.sourceforge.net/lists/listinfo/html-template-users ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: Mathew Robertson - 2004-05-12 22:57:11 ```hmm... I dont consider this to be a bug... here you create a global variable called 'gvar' > \$t->param(gvar =3D> "global variable"); here you are creating a loop specific variable called 'gvar' > { loopvar =3D> "GVar should be set", gvar =3D> "Loop GVar = 1" }, These two 'gvar's are two different variables.=20 In this case > { loopvar =3D> "GVar should be blank again", gvar =3D> undef = }, you are saying that the loop specific 'gvar' is not part of the loop - = ie equivalent to: { loopvar =3D> "GVar should be blank again" } thus requesting the outer global gvar to come into scope. Consider: t->param(gvar =3D> "Global gvar"); my \$gvar_val; my @loop; for.... { if (something) { \$gvar_val =3D "Loop GVar 1"; } elsif (somethign else) { \$gvar_val =3D ""; } my \$tmp; \$tmp->{loopvar} =3D ...; \$tmp->{gvar} =3D \$gvar; push @loop, \$tmp; } t->param(testloop =3D>\@loop); What would you expect the TMPL_LOOP to do in that example?=20 I would have expected that the third iteration to use the global gvar = value. Mathew > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > test.tmpl: > ---------- > Global Variable gvar is: >=20 > Loop: > loopvar: > gvar : > > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > test.pl > ------- >=20 > use strict; > use HTML::Template; >=20 > my \$t =3D new HTML::Template(filename =3D> "test.tmpl", global_vars = =3D> 1,) > || die; >=20 > my \$testloop =3D [ > { loopvar =3D> "GVar should be set", gvar =3D> "Loop GVar = 1" }, > { loopvar =3D> "GVar should be blank", gvar =3D> "" = }, > { loopvar =3D> "GVar should be blank again", gvar =3D> undef = }, > ]; >=20 > \$t->param(gvar =3D> "global variable"); > \$t->param(testloop =3D> \$testloop); > print \$t->output(); > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > When you run test.pl.. you get the following output: > = ------------------------------------------------------------------------ > Global Variable gvar is: global variable >=20 > Loop:=20 > loopvar: GVar should be set=20 > gvar : Loop GVar 1 > =20 > loopvar: GVar should be blank > gvar :=20 > =20 > loopvar: GVar should be blank again > gvar : global variable > = ------------------------------------------------------------------------ >=20 > 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".=20 ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: simran - 2004-05-12 23:54:28 ```> hmm... I dont consider this to be a bug... > > 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" } 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... 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 Another example: -- test.pl -- #!/usr/bin/perl my \$tvar = "this is a test"; \$tvar = undef; print "Tvar=\$tvar\n"; ----------------------------------------- The above prints: Tvar= Now, so in the same context if "undef" should mean... take the outer scope, then perl should have ignored my "\$tvar = undef" line... 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/html-template-users@.../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... simran. > > 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 =>\@loop); > > 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: > > > > Loop: > > loopvar: > > gvar : > > > > ======================================================================== > > 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". ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: Mathew Robertson - 2004-05-13 02:19:12 ```> > here you create a global variable called 'gvar' > > > \$t->param(gvar =3D> "global variable"); > >=20 > > here you are creating a loop specific variable called 'gvar' > > > { loopvar =3D> "GVar should be set", gvar =3D> "Loop = GVar 1" }, > >=20 > > These two 'gvar's are two different variables.=20 > >=20 > > In this case > > > { loopvar =3D> "GVar should be blank again", gvar =3D> undef = }, > >=20 > > you are saying that the loop specific 'gvar' is not part of the loop = - ie equivalent to: > > { loopvar =3D> "GVar should be blank again" } >=20 > here is where i think we differ...=20 >=20 > 1. \$href =3D { loopvar =3D> "GVar should be blank again", gvar =3D> = undef } > to me (and in general perl programming is VERY different from:=20 > 2. \$href =3D { loopvar =3D> "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)?". =20 > 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" =20 > 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...=20 >=20 > (1) perl > print "Keys: ". join(',', keys %\$href) . "\n"; > (1) output> Keys: gvar,loopvar >=20 > (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: >=20 > -- test.pl -- > #!/usr/bin/perl > my \$tvar =3D "this is a test"; > \$tvar =3D undef; > print "Tvar=3D\$tvar\n"; > ----------------------------------------- > The above prints: Tvar=3D 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 =3D undef" line...=20 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 :-)=20 > It has infact been flagged before as well... its partially on a thread > at: > = http://www.mail-archive.com/html-template-users@.../msg= 00728.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...=20 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 >=20 >=20 >=20 >=20 >=20 > >=20 > > thus requesting the outer global gvar to come into scope. > >=20 > > Consider: > >=20 > > t->param(gvar =3D> "Global gvar"); > > my \$gvar_val; > > my @loop; > > for.... { > > if (something) { > > \$gvar_val =3D "Loop GVar 1"; > > } elsif (somethign else) { > > \$gvar_val =3D ""; > > } > > my \$tmp; > > \$tmp->{loopvar} =3D ...; > > \$tmp->{gvar} =3D \$gvar; > > push @loop, \$tmp; > > } > > t->param(testloop =3D>\@loop); > >=20 > > What would you expect the TMPL_LOOP to do in that example?=20 > > I would have expected that the third iteration to use the global = gvar value. > >=20 > > Mathew > >=20 > >=20 > > > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > test.tmpl: > > > ---------- > > > Global Variable gvar is: > > >=20 > > > Loop: > > > loopvar: > > > gvar : > > > > > > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > test.pl > > > ------- > > >=20 > > > use strict; > > > use HTML::Template; > > >=20 > > > my \$t =3D new HTML::Template(filename =3D> "test.tmpl", = global_vars =3D> 1,) > > > || die; > > >=20 > > > my \$testloop =3D [ > > > { loopvar =3D> "GVar should be set", gvar =3D> "Loop = GVar 1" }, > > > { loopvar =3D> "GVar should be blank", gvar =3D> "" = }, > > > { loopvar =3D> "GVar should be blank again", gvar =3D> undef = }, > > > ]; > > >=20 > > > \$t->param(gvar =3D> "global variable"); > > > \$t->param(testloop =3D> \$testloop); > > > print \$t->output(); > > > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > >=20 > > > When you run test.pl.. you get the following output: > > > = ------------------------------------------------------------------------ > > > Global Variable gvar is: global variable > > >=20 > > > Loop:=20 > > > loopvar: GVar should be set=20 > > > gvar : Loop GVar 1 > > > =20 > > > loopvar: GVar should be blank > > > gvar :=20 > > > =20 > > > loopvar: GVar should be blank again > > > gvar : global variable > > > = ------------------------------------------------------------------------ > > >=20 > > > 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".=20 >=20 >=20 > ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: simran - 2004-05-13 02:40:45 ```[...snip...] > > 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; i do normally always use strict :-)... and putting them (use warnings, strict) into the test.pl file above produces the same result... i do take your other points though... and i do believe that inside a loop a "row" or data should be treated as a hash... as that's what is actually is as well... but that is my opinion... would be interesting to see what Sam things... as i guess it will be his decision in the end :-) Personally though.. i have found that they way i use HTML::Template, i found found the current behaviour to be contrary to my expectations... (and hence a bug in my eyes) > > ... 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/html-template-users@.../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 =>\@loop); > > > > > > 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: > > > > > > > > Loop: > > > > loopvar: > > > > gvar : > > > > > > > > ======================================================================== > > > > 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 > Html-template-users@... > https://lists.sourceforge.net/lists/listinfo/html-template-users ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: petersm - 2004-05-13 14:29:24 ```Mathew Robertson wrote: > 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)?". > According to the H::T docs ... "For the computer-science geeks among you, a introduces a new scope much like a perl subroutine call." So the scope (when global vars is turned on) is that it should behave like a variable, not a hash (since it is a TMPL_VAR not a TMPL_KEY right :). For instance, the following simple script.. #!/usr/bin/perl use strict; our \$var = 'a test'; &test(); sub test { my \$var = 'another test'; \$var = undef; print "This is \$var\n"; } produces this output 'This is' Even with 'use strict' (and even with 'use warnings' STDOUT still gets this output even if STDERR get's the warning). So, in my opinion he is right. This is a bug. And I even think sam admitted it was and that somehow what was needed was to check 'exists' and not truthfulness. > 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; Even if you 'use strict' and 'use warnings' you get the right output (like I said before, on STDOUT even if you do get a 'warning' on STDERR) Michael Peters Venzia ```
 Re: [htmltmpl] Patch to fix global_vars and loop problem... From: Mathew Robertson - 2004-05-13 22:49:54 ```good example -> given this , I would agreee that it is a bug. ... and yes you are correct, the stderr message is generated by 'use = warnings' not 'use strict'. Mathew > > Relating this to TMPL_LOOP's... the question becomes, "Is a variable = inside=20 > > 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=20 > > TMPL_LOOP variables (as compared to Perl's use of 'strict'ness)?". > > =20 >=20 > According to the H::T docs ... > "For the computer-science geeks among you, a introduces a = new > scope much like a perl subroutine call." > So the scope (when global vars is turned on) is that it should behave = like a > variable, not a hash (since it is a TMPL_VAR not a TMPL_KEY right :). = For > instance, the following simple script.. >=20 > #!/usr/bin/perl > use strict; > = =20 > = =20 > our \$var =3D 'a test'; > &test(); > = =20 > = =20 > sub test > { > my \$var =3D 'another test'; > \$var =3D undef; > print "This is \$var\n"; > } >=20 > produces this output > 'This is' >=20 > Even with 'use strict' (and even with 'use warnings' STDOUT still gets = this > output even if STDERR get's the warning). >=20 > So, in my opinion he is right. This is a bug. And I even think sam = admitted it > was and that somehow what was needed was to check 'exists' and not = truthfulness. >=20 >=20 > > Since we are talking about TMPL_LOOP variables, not Perl hash keys, = it gets > back to the question(s) posed above. > >=20 > > > Another example: > > >=20 > > > -- test.pl -- > > > #!/usr/bin/perl > > > my \$tvar =3D "this is a test"; > > > \$tvar =3D undef; > > > print "Tvar=3D\$tvar\n"; > > > ----------------------------------------- > > > The above prints: Tvar=3D > >=20 > > true - but only if you dont do: > >=20 > > use strict; > > use warnings; >=20 > Even if you 'use strict' and 'use warnings' you get the right output = (like I > said before, on STDOUT even if you do get a 'warning' on STDERR) >=20 >=20 > Michael Peters > Venzia >=20 >=20 > ------------------------------------------------------- > 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=3D2562&alloc_id=3D6184&op=3Dclick > _______________________________________________ > Html-template-users mailing list > Html-template-users@... > https://lists.sourceforge.net/lists/listinfo/html-template-users > ```