Template2
Template2 copied to clipboard
TRACE_VARS using hash extraction
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.
Looks like it stores as:
# 'VARIABLES' => {
# 'A' => {},
# 'B' => {
# 'C' => {}
# }
# }
I don't necessarily see why you were expecting {A => {B => { C => {} } } }
Just because [ A ] will expand keys in FOREACH scope and B.C will be accessible
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?
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 I'm surprised it does that but it looks like this works now?