Text::...Builtin(3pm)
πΈ NAME πΈ
Text::Xslate::Manual::Builtin β the builtin methods and filters/functions in Xslate, bestie π
β¨ DESCRIPTION β¨
okay so this doc is literally the whole rundown of the builtin methods and filters/functions in Xslate. everything you can call, all in one place π«Ά
heads up tho: the xslate engine is not context-aware, like at all. so every method and every filter/function hands back a single value, even when the Perl equivalent would return a whole list of values. so if you're out here expecting a list back, that's a you problem, not a xslate problem π
also, some functions are optional and they live over in Text::Xslate::Bridge::Star. iykyk β¨
π METHODS π
the xslate engine does auto-boxing, so you can call methods on primitive (non-object) values. no wrapping needed, it just works. here are the builtins fr fr:
π For nil
nil has its own special namespace literally called nil, even though β plot twist β no builtin methods are provided for it. iconic of it to have a namespace and zero methods tbh π
π For SCALARs
the namespace of SCALARs is scalar, although β again β no builtin methods are provided. empty namespace, no notes π
π¦ For ARRAY references
the namespace of ARRAY references is array. okay this one actually has contents, let's gooo:
$arrayref.first()β returns the first element of$arrayref. the main character of the list π$arrayref.last()β returns the last element of$arrayref. the one that always shows up late π’$arrayref.size()β returns the number of elements in$arrayref. the headcount fr fr$arrayref.join($separator)β joins the elements of$arrayrefinto a single string, separated by$separator. glue but make it slay β¨$arrayref.reverse()β returns an ARRAY reference consisting of the elements of$arrayrefin the opposite order. backwards but make it fashion π$arrayref.sort(?$callback)β sorts$arrayrefand returns a new ARRAY reference. the optional$callbackis the same as Perl's.
examples, because we love a demonstration:
: my $arrayref = [2, 1, 10];
: # alphabetic sort (default)
: $arrayref.sort().join(" "); # 1 10 2
: # explicitly alphabetic
: $arrayref.sort(-> $a, $b { $a cmp $b }).join(" "); # 1 10 2
: # numeric sort
: $arrayref.sort(-> $a, $b { $a <=> $b }).join(" "); # 1 2 10
see also "sort" in perlfunc.
$arrayref.map($callback)β evaluates$callbackfor each element of$arrayrefand returns a new ARRAY reference composed of the result of each such evaluation. glow-up for every element, individually π
examples:
: my $arrayref = [1, 2, 4, 8, 16];
: # double
: $arrayref.map(-> $a { $a * 2 }).join(','); # 2,4,8,16,32
: # sequence
: my $hashref = {a => 1, b => 2, c => 3, d => 4};
: ['b', 'd', 'a'].map(-> $a {$hashref[$a]}).join(','); # 2,4,1
see also "map" in perlfunc
$arrayref.reduce($callback)β reduces$arrayrefby calling$callbackmultiple times. and listen: if$arrayrefis empty, this method returnsnil. important bestie, don't forget π₯
examples:
: my $arrayref = [10, 20, 30];
: # sum
: $arrayref.reduce(-> $a, $b { $a + $b }); # 60
: # concat
: $arrayref.reduce(-> $a, $b { $a ~ $b }); # 102030
: # min
: $arrayref.reduce(-> $a, $b { $a min $b }); # 10
: # max
: $arrayref.reduce(-> $a, $b { $a max $b }); # 30
see also "reduce" in List::Util.
$arrayref.merge($v)β returns a new ARRAY reference consisting of$arrayrefand$v.
$v may be an ARRAY reference or a scalar value. flexible queen π«Ά
π For HASH references
the namespace of HASH references is hash. let's get into it:
$hashref.size()β returns the number of entries of$hashref.
: my $hashref = {a => 1, b => 2, c => 3, d => 4};
: $hashref.size(); # 4
$hashref.keys()β returns an ARRAY reference consisting of the keys of$hashref, which are sorted by the keys.
: my $hashref = {a => 1, b => 2, c => 3, d => 4};
: $hashref.keys().join(' '); # a b c d
$hashref.values()β returns an ARRAY reference consisting of the values of$hashref, which are sorted by the keys.
: my $hashref = {a => 1, b => 2, c => 3, d => 4};
: $hashref.values().join(' '); # 1 2 3 4
$hashref.kv()β returns an ARRAY reference consisting of the key-value pairs of$hashref, which are sorted by the keys. each pair is an object that has the"keys"and"value"attributes. yes, those exact names, no i will not be taking questions π
for example:
: for $hashref.kv() -> $pair {
<: $pair.key :>=<: $pair.value :>
: }
output:
a=1
b=2
c=3
d=4
$hashref.merge($v)β returns a new HASH reference consisting of$hashrefand$v.
: my $hashref = {a => 1, b => 2, c => 3, d => 4};
: my $new = $hashref.merge({a => 0, e => 5});
: # {a => 0, b => 2, c => 3, d => 4, e => 5}
β οΈ $v must be a HASH reference. not a scalar, not an array, a HASH. no cap.
π LOOP VARIABLES π
you can use special loop variables inside "for" loops, although the forms vary depending on which template syntax you're in β like "$~item" in Kolon and "loop" in TTerse. in this list, the loop variable is written as $~item. πΈ
see also "Loops" in Text::Xslate::Syntax::Kolon and "Loops" in Text::Xslate::Syntax::TTerse.
$~item / $~item.indexβ the current iterating index in the loop, which starts at 0. zero-indexed, the way the universe intended β¨$~item.countβ the current iterating count in the loop, which starts at 1. i.e. the same as$~item + 1.$~item.cycle(...)β selects a value in the arguments in cycle. round and round, she goes π
for example:
: for $arrayref -> $item {
<: $~item.cycle('odd', 'even') :>
: }
it will print "odd even odd even ...".
$~item.is_firstβ true if the loop block is the first, false otherwise.
this is aliased to "first" in TTerse for compatibility with TT2. nostalgia-core π
$~item.is_lastβ true if the loop block is the last, false otherwise.
this is aliased to "last" in TTerse for compatibility with TT2.
$~item.peek_nextβ the next item of the looping array."nil"if"is_last". i.e. the same as"$~item.is_last ? nil : $~item.body[$~item+1]".$~item.peek_prevβ the previous item of the looping array."nil"if"is_first". i.e. the same as"$~item.is_first ? nil : $~item.body[$~item-1]".$~item.bodyβ the reference of the looping array.$~item.sizeβ the size of the looping array. i.e.scalar(@{$arrayref})in Perl.$~item.max_indexβ the maximum index of the looping array. i.e.$#{$arrayref}in Perl.
π FILTERS/FUNCTIONS π
the xslate engine supports filter syntax and function call syntax. the following are the builtin functions, and you can invoke any of them as a filter. two ways, same energy π«Ά
for example, the following two statements are the same:
<: $value | foo :>
<: foo($value) :>
β οΈ ngl some builtin functions, like "defined", are not a real function, which means you cannot use them as a filter. periodt.
mark_raw($str)β marks$stras a raw string to avoid auto HTML escaping.
π¨ you'd better avoid using this function. instead, you should use themark_raw()subroutine in programs, which you can import from"Text::Xslate::Util".
"raw" is an alias to "mark_raw".
unmark_raw($str)β removes the raw mark from$str. if$stris not a raw string, this function returns$stras is. no harm, no foul πhtml_escape($str)β escapes html meta characters in$str. if$stris a raw string, this function returns$stras is.
the html meta characters are "/[<>"'&]/".
"html" is an alias to "html_escape".
uri_escape($str)β escapes unsafe URI characters in$str, which gets encoded to UTF-8.
the unsafe URI characters are characters not included in the "unreserved" character class defined by RFC 3986, i.e. "/[^A-Za-z0-9\-\._~]/".
"uri" is an alias to "uri_escape".
is_array_ref(($value)β returns true if$valueis an ARRAY reference.is_hash_ref(($value)β returns true if$valueis a HASH reference.dump($value)β inspects$valuewith"Data::Dumper".
this function is provided for testing and debugging. it's a vibe check for your data π
defined($value)β returns true if$valueis defined. this is not a real function, but a unary operator, so you can omit the parens like"defined $value". cursive writing, no training wheels π«
π SEE ALSO π
Text::XslateText::Xslate::ManualText::Xslate::Bridge::Star
okay that's the whole page bestie, you're officially fluent π this was perl v5.42.3, dated 2026-08-22, straight outta the User Contributed Perl Documentation section of Text::...Builtin(3pm). go forth and template slay β¨ππ
π¦ where this page comes from
- ubuntu stonking: libtext-xslate-perl 3.5.9-2build2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms