Template2 icon indicating copy to clipboard operation
Template2 copied to clipboard

TRACE_VARS using hash extraction

Open sten22 opened this issue 10 years ago • 6 comments

Simple test case:

use Template::Parser;
use Test::More;

my $template = Template::Parser
    ->new(TRACE_VARS => 1)
    ->parse('[% FOREACH [ A ] %][% B.C %][% END %]');

is_deeply $template->{VARIABLES}, {A => {B => { C => {} } } };

done_testing;

Output:

not ok 1
#   Failed test at t/auto/template/trace-vars.t line 8.
#     Structures begin differing at:
#          $got->{A}{B} = Does not exist
#     $expected->{A}{B} = HASH(0x7fdd1501b730)
1..1
# Looks like you failed 1 test of 1.

sten22 avatar Oct 06 '15 06:10 sten22

Looks like it stores as:

#   'VARIABLES' => {
#     'A' => {},
#     'B' => {
#       'C' => {}
#     }
#   }

toddr avatar Oct 09 '18 03:10 toddr

I don't necessarily see why you were expecting {A => {B => { C => {} } } }

toddr avatar Oct 09 '18 03:10 toddr

Just because [ A ] will expand keys in FOREACH scope and B.C will be accessible

a-parser avatar Oct 09 '18 04:10 a-parser

foreach is a loop. So you're iterating once with A as the $_; but then in the loop you're not doing $_.B.C You're doing B.C I don't see how A is implied. If it were implied, how would you display B.C if you wanted to?

toddr avatar Oct 09 '18 15:10 toddr

I can't found this in docs right now, but it's works for long time:

perl -e 'use Template; Template->new()->process(\"[% FOREACH [A]; B.C; END %]\n", {A => {B => {C => 42}}});'
42

And it is present in tests: https://github.com/abw/Template2/blob/master/t/foreach.t#L370

a-parser avatar Oct 11 '18 09:10 a-parser

@a-parser I'm surprised it does that but it looks like this works now?

toddr avatar Oct 11 '18 16:10 toddr