Template::Stash(3)
π NAME π
okay bestie, this is the Template::Stash(3) page β Template::Stash, literally Magical storage for template variables. No cap, it's the object class that holds variable values while the template processor is doing its runtime thing. buckle up π
β¨ SYNOPSIS β¨
use Template::Stash;
my $stash = Template::Stash->new(\%vars);
# get variable values
$value = $stash->get($variable);
$value = $stash->get(\@compound);
# set variable value
$stash->set($variable, $value);
$stash->set(\@compound, $value);
# default variable value
$stash->set($variable, $value, 1);
$stash->set(\@compound, $value, 1);
# set variable values en masse
$stash->update(\%new_vars)
# methods for (de-)localising variables
$stash = $stash->clone(\%new_vars);
$stash = $stash->declone();
πΈ DESCRIPTION πΈ
Real talk: Template::Stash defines an object class that stores variable values for runtime use by the template processor. Inside, the values live in a hash reference (and yeah, that hash ref is blessed to make the object), and you get at them with the get() and set() methods.
Variables can point at hash arrays, lists, subroutines and objects, not just simple values. The stash does the right magic automatically when it deals with variables β calling code or object methods, indexing into lists, hashes, etc. It's giving βI got you, bestieβ energy.
It also has clone() and declone() methods. The template processor uses them to make temporary copies of the stash so changes made to variables can be localised. periodt. π
π PUBLIC METHODS π
π
new(\%params)
The new() constructor method creates and returns a reference to a new Template::Stash object. Iconic.
my $stash = Template::Stash->new();
You can pass in a hash reference stuffed with variables and values to initialise the stash.
my $stash = Template::Stash->new({ var1 => 'value1',
var2 => 'value2' });
π¦ get($variable)
get() grabs the variable whose name you put in the first parameter.
$value = $stash->get('var1');
For dotted compound variables, pass the variable elements as a list reference. Every node in the variable takes up two entries: first the name of the variable element, then either a reference to a list of arguments for that element or 0 if there are none.
[% foo.bar(10).baz(20) %]
$stash->get([ 'foo', 0, 'bar', [ 10 ], 'baz', [ 20 ] ]);
π set($variable, $value, $default)
set() takes the variable name in the first parameter and pins the value from the second parameter onto it.
$stash->set('var1', 'value1');
If that third parameter evaluates to a true value, the variable only gets set when it didn't already have a true value before. lowkey a default-value guard.
$stash->set('var2', 'default_value', 1);
Dotted compound variables work the same way as they do for get() above.
[% foo.bar = 30 %]
$stash->set([ 'foo', 0, 'bar', 0 ], 30);
There's a magical variable called "IMPORT" you can specify, and its value should be a hash reference. The stuff in that hash array gets copied β i.e. imported β into the current namespace.
# foo.bar = baz, foo.wiz = waz
$stash->set('foo', { 'bar' => 'baz', 'wiz' => 'waz' });
# import 'foo' into main namespace: bar = baz, wiz = waz
$stash->set('IMPORT', $stash->get('foo'));
π update($variables)
Use this one to set or update a bunch of variables in one go.
$stash->update({
foo => 10,
bar => 20,
});
π₯ getref($variable)
This undocumented feature hands back a closure you can call to get a variable's value. It's how variable references get implemented so they can be evaluated lazily.
[% x = \foo.bar.baz %] # x is a reference to foo.bar.baz
[% x %] # evalautes foo.bar.baz
bestie, it literally says βundocumented featureβ in the original β I'm just passing the tea along π«
π§ clone(\%params)
clone() creates and returns a fresh Template::Stash object that's a localised copy of the parent stash. You can update variables in the cloned stash all you want; when declone() is called, you get the original stash back with all its members intact and exactly in the state they were in before clone() was called.
For convenience, you can pass a hash of parameters into clone(). It gets used to update any simple variable β meaning ones that don't contain namespace elements, like foo and bar, but not foo.bar β while the stash is being cloned. If you're adding or updating complex variables, call the set() method after calling clone(). That correctly resolves and/or creates any namespace hashes you need.
A cloned stash keeps a reference to the stash it was copied from in its _PARENT member.
π declone()
declone() returns the _PARENT reference, and that's how you restore a stash's state as described above.
π define_vmethod($type, $name, $code)
Use this to define new virtual methods. The first argument should be either "scalar" or "item" to define a scalar virtual method, "hash" for hash virtual methods, or either "array" or "list" for list virtual methods. The second argument is the name of the new method. The third argument is a reference to a subroutine that implements the method. The data item the virtual method gets called on is passed to the subroutine as its first argument.
$stash->define_vmethod(
item => ucfirst => sub {
my $text = shift;
return ucfirst $text
}
);
π οΈ INTERNAL METHODS π οΈ
β dotop($root, $item, \@args, $lvalue)
This is the core "dot" operation method. It evaluates elements of variables against their root.
π undefined($ident, $args)
This method gets called when get() hits an undefined value. If the STRICT option is in effect, it throws an exception indicating the use of an undefined value. Otherwise it silently returns an empty string.
You can redefine it in a subclass if you want alternate handling for undefined values.
π AUTHOR π
Andy Wardley <abw@wardley.org> <http://wardley.org/>
π COPYRIGHT π
Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π SEE ALSO π
Template, Template::Context
okay that's the whole page, bestie β sign-off time π: this one came from perl v5.42.0, dated 2024-06-23. stay magical π«Άβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Template-Toolkit 3.102.0-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Template-Toolkit 3.102.0-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Template-Toolkit 3.102.0-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Template-Toolkit 3.102.0-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms