The documentation says "The Template::Stash module is used to fetch and store template variables.", so I (a recent TT convert) thought your code should work. I did some experimenting:

use strict; use warnings; use Template; use Template::Stash; my $template_string = <<'END_OF_TEMPLATE'; [% x1 = "x1_defined_in_template" -%] Inside the processed template: x1 = [% x1 %] x2 = [% x2 %] x3 = [% x3 %] END_OF_TEMPLATE my $stash = Template::Stash->new(); my $tmpl = Template->new( STASH => $stash ); $stash->set( x1 => 'x1_defined_in_stash_set' ); $stash->set( x2 => 'x2_defined_in_stash_set' ); $stash->set( x3 => 'x3_defined_in_stash_set' ); my $t_vars = { x3 => 'x3_defined_in_process_t_vars' }; # Process and print the template $tmpl->process( \$template_string, $t_vars ) or die $tmpl->error(); print "Post-process, the stash vars are:\n"; print ' x1: ', $stash->get('x1'), "\n"; print ' x2: ', $stash->get('x2'), "\n"; print ' x3: ', $stash->get('x3'), "\n"; print "Note that we could check more easily via _dump:\n"; print $stash->_dump(), "\n";
Output:
Inside the processed template: x1 = x1_defined_in_template x2 = x2_defined_in_stash_set x3 = x3_defined_in_process_t_vars Post-process, the stash vars are: x1: x1_defined_in_stash_set x2: x2_defined_in_stash_set x3: x3_defined_in_stash_set Note that we could check more easily via _dump: [Template::Stash] { x3 => x3_defined_in_stash_set x2 => x2_defined_in_stash_set global => { } inc => CODE(0x18503f8) dec => CODE(0x1850458) x1 => x1_defined_in_stash_set _PARENT => <undef> }

As the Anonymous Monk said, the stash is reverting to its prior state after process(). So, unless you use the stash to communicate *all* variable state, your approach does not look workable.

I also tried the method listed in the "Show All Variables" thread on the TT mailing list, but I could not make it work at all; perhaps the TT code no longer recognizes a bare 'keys' statement as referring to the whole stash.

Since Template::Stash does handle all the variable get&set, you might be able to cache the last get|set for each variable as Template::Stash handles it via subclassing:

use strict; use warnings; package Template::Capturing::Stash; { use base 'Template::Stash'; our %remembering_hash; sub get { die if @_ != 2; my ( $self, $var_name ) = @_; my $var_value = $self->SUPER::get($var_name); #print "! Get of $var_name = $var_value\n"; $remembering_hash{$var_name} = $var_value; return $var_value; } sub set { die if @_ < 3 or @_ > 4; my ( $self, $var_name, $var_value, $default ) = @_; die "Not yet coded to handle default" if defined $default; my $return_value = $self->SUPER::set( $var_name, $var_value ); #print "! Set of $var_name = $var_value\n"; $remembering_hash{$var_name} = $var_value; return $return_value; } } package main; use Template; use Template::Stash; my $template_string = <<'END_OF_TEMPLATE'; [% x1 = "x1_defined_in_template" -%] Inside the processed template: x1 = [% x1 %] x2 = [% x2 %] x3 = [% x3 %] END_OF_TEMPLATE my $stash = Template::Capturing::Stash->new(); my $tmpl = Template->new( STASH => $stash ); $stash->set( x1 => 'x1_defined_in_stash_set' ); $stash->set( x2 => 'x2_defined_in_stash_set' ); $stash->set( x3 => 'x3_defined_in_stash_set' ); my $t_vars = { x3 => 'x3_defined_in_process_t_vars' }; my $out; $tmpl->process( \$template_string, $t_vars, \$out ) or die $tmpl->error(); print $out; #print $stash->_dump(), "\n"; use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper( \%Template::Capturing::Stash::remembering_hash );
Output:
Inside the processed template: x1 = x1_defined_in_template x2 = x2_defined_in_stash_set x3 = x3_defined_in_process_t_vars $VAR1 = { 'component' => '', 'x1' => 'x1_defined_in_template', 'x2' => 'x2_defined_in_stash_set', 'x3' => 'x3_defined_in_process_t_vars' };
While this looks *very* promising, it might be limited to only simple variable access. I did not have time to test it on complex (dotted) variable access, where this technique might break down.

If you figure out a better solution, or get one from another venue, please post it here for future Monks!


In reply to Re: Accessing Template Toolkit Variables by Util
in thread Accessing Template Toolkit Variables by saintmike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.