Re: Dumping all vars ?
Tim Allwine <[email protected]>
| Newsgroups | gmane.comp.lang.perl.modules.template-toolkit |
|---|---|
| Message-ID | <[email protected]> |
On 4/18/12 4:32 PM, Patrick Rynhart wrote:
> Is it possible to dump all variables available from within a TT2 template (or - alternatively - enumerate all variables
> which are available ?)
>
> I've had a look at this:
>
> http://template-toolkit.org/docs/modules/Template/Plugin/Dumper.html
>
> but you need to know the variables previously.
>
> (I'm wanting to list anything which is available from the upstream perl script - not variables defined locally in the
> TT2 script.)
This is old but it works.
use Dumpvalue;
use IO::Scalar;
use Template;
my $d = Dumpvalue->new();
my $vars = {};
$vars->{settings} = 'xyz';
for (1..10) {
push(@{$vars->{list}},$_);
}
$vars->{names}{first_name} = 'Bob';
$vars->{names}{last_name} = 'Smith';
my $dump_str;
my $io = IO::Scalar->new(\$dump_str);
my $oio = select($io);
$d->dumpValue(\$vars);
$vars->{dump_str} = $dump_str;
select($oio);
my $tt = Template->new();
$tt->process(\*DATA,$vars);
__DATA__
Dumped vars:
[% dump_str %]
Here is the output:
Dumped vars:
-> HASH(0x100803068)
'list' => ARRAY(0x100811a50)
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
'names' => HASH(0x1008119c0)
'first_name' => 'Bob'
'last_name' => 'Smith'
'settings' => 'xyz'
You could do the same thing with Data::Dumper but I like Dumpvalue.
-Tim