可以定义一个自定义虚拟方法,该方法返回所提供变量的 ref 类型。粗略的例子:
#!/usr/bin/perl
use strict;
use warnings;
use Template;
use Template::Stash;
$Template::Stash::SCALAR_OPS->{ ttref } = \&ttref;
$Template::Stash::LIST_OPS ->{ ttref } = \&ttref;
$Template::Stash::HASH_OPS ->{ ttref } = \&ttref;
my $t = Template->new( );
$t->process( \*DATA, { vars => [ 1, [ ], { } ] } );
sub ttref
{
return ref $_[0];
}
__DATA__
[% FOREACH var IN vars -%]
ref type of [% var %] is [% var.ttref %]
[% END %]
Run Code Online (Sandbox Code Playgroud)
输出:
ref type of 1 is
ref type of ARRAY(0x9cfbd0) is ARRAY
ref type of HASH(0x9cfc00) is HASH
Run Code Online (Sandbox Code Playgroud)