flowdy has asked for the wisdom of the Perl Monks concerning the following question:

Template::Toolkit 2.24 behaves weird when I store an aggregate item to a variable. The result of defined is not quite consistent. Look:
tpage <<EOF [% group= ["test string"]; g = group.1 %] [% IF g.defined != group.1.defined %]Seems like a bug![% END %] EOF Seems like a bug!

defined appears to behave like exists($ttstash{var}). The doc is inconsistent in that they tell about a value, not about a variable. The variable is assigned to, defined in this sense, but it is assigned an undefined value.

Isn't that a bug to report?

Replies are listed 'Best First'.
Re: Template::Toolkit: defined on aggregate item vs. defined on variable
by choroba (Cardinal) on Aug 05, 2016 at 12:35 UTC
    Weird indeed. Same in 2.26.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Template::Toolkit: defined on aggregate item vs. defined on variable
by beech (Parson) on Aug 05, 2016 at 17:46 UTC

    arrays start with 0, so if you add another element, .1 is defined, so I think its not a bug

    $ tpage tt2-defined-bug.txt group 1 g 1 group.0 1 group.1 1 no bug $ cat tt2-defined-bug.txt [%# tpage --compile_dir=goner tt2-defined-bug.txt -%] [% group= ["one","two"]; g = group.1 %] group [% IF group.defined %] 1 [% ELSE %] 0 [% END %] g [% IF g.defined %] 1 [% ELSE %] 0 [% END %] group.0 [% IF group.0.defined %] 1 [% ELSE %] 0 [% END %] group.1 [% IF group.1.defined %] 1 [% ELSE %] 0 [% END %] [% IF g.defined != group.1.defined %]Seems like a bug! [% ELSE %]no bu +g[% END %]
      > so if you add another element, .1 is defined

      The question is why g.defined != group.1.defined when the array has one element only.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        Because arrays start at 0? So group.1 is not defined? And the condition is true?

        if( 1 ne "") { print "Seems like a bug! BUT IT IS NOT BUG\n"; }

      In general you're right. In this case, I just posted a simplified example to pm. In my real-word situation I have to tell apart defined or undefined second part of a tuple in view logic. This is where the shown weirdness taught me better not to give long.paths.through.data.structures simple names to use more than once. Or at least not to include the tail component in the assignment.