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";
####
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 =>
}
##
##
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 );
##
##
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'
};