Reading from a file while auto-detecting the format:
Force reading as JSON:
Force reading as YAML:
Input comes from standard input by default, no need to specify anything
in this case (although you can be explicit and use filename -):
Input from Text on Command Line
You can specify input text with a shell trick (using <<<) and what
explained before, or using the following options directly:
No Input
Sometimes you don’t need to read an input data structure (e.g. when you
define variable’s values using -d/--define):
Output
Output is sent to standard output by default. You can either
redirect/pipe it, or use option -o to save to a file:
Many options/tricks specify how the output is generated, so read on! One
option is described here, though: if you want to add a newline at the
very end of the input, pass option -n:
whatever is enclosed between a pair of [% and %] is considered
special
if the pair contains a path in the data, the chunk is expanded
with the value of the variable at the specific position in the
input data
if the opening is immediately followed by an equal sign = (with
no spaces), the chunk is interpreted as Perl code and the last
value is substituted for the chunk
otherwise, the chunk is considered Perl code but nothing is
printed by default
whatever is printed to standard output in the Perl code inside the
chunks is expanded in place of the chunk.
A path in the data is something like path.to.1.variable. This
is split upon the the dots, and each section is used to traverse the
input data structure. So, if we had a reference $V to the data
structure, the example path would be resolved to
$V->{path}{to}[1]{variable} (assuming of course that there are hashes
and arrays at the right places).
Text Templates
Templates can be taken from a file, for example file1.tmpl:
Use option -t to load them:
… or from a string on the command line:
We used option -n to add a newline to the output!
Variables
Getting a single variable only has its own shortcut, i.e. the following
are equivalent:
Functions
Getting the output of a function only has its own shortcut, i.e. the
following are equivalent:
This comes handy for calling a few pre-defined functions as explained
below.
Pretty-Printing
Pretty-printing (or converting!) in JSON:
Pretty-printing (or converting!) in YAML:
Generating HTML/XML
Functions urlenc, xmlenc and xmltxt can come handy when generating
HTML/XML - they encode the argument to a representation that is safe for
inclusion in URL or text, respectively. This is urlenc in action:
xmlenc encodes the five reserved characters < > ' " &:
xmltxt is like xmlenc, but it encodes only < and & (which should be
fine in all cases anyway):
Crumbr Mode
Crumbr mode allows you to expand the whole input data structure with one
line per leaf, so that you should be able to easily use grep and
sed.
You can be exact in distinguishing between hash keys and array
indexes:
You might want to get a JSON-compliant representation of each line:
Variables Accessors
Variables accessors are shortcut functions that allow expanding a
variable according to the (expected) type.
Scalar variables can be accessed via V (for Variable, not for
Scalar!):
Array references are de-referenced using A:
Hashes can be dereferenced in three ways: directly with H, only keys
with HK, only values with HV:
Loading Functions
If you want to load Perl functions from modules, as of version 0.7.0
you can with options -M and -l.
The former (-M, alias --module) allows you to load modules and
functions from the command line, and consume those functions from inside
the template:
You can import multiple functions separating their names with a comma:
Alternatively, you can just specify a line that will be used:
In particular, whatever you pass as parameter will be evaled like
follows:
If you want to load modules/functions that don’t lie around in places
perl would normally look into, you can add places to the directory
search list using option -l (alias --lib, alias --include).
Suppose you have module Foo::Bar inside lib/Foo/Bar.pm: