Templa...ompile(3pm)

πŸ“¦
version 1.022 Β· 6 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

okay so bestie, welcome to Template::Alloy::Compile β€” it's the Compile role, and what it does is let you take an AST and turn it into actual perl code. It ships as part of the Template::Alloy distribution, under the Perl User Contributed docs (the 3pm girlies know πŸ’…).


🌸 DESCRIPTION 🌸

so here's the tea 🍡 β€” the Template::Alloy::Compile role is the one that takes the AST that the Parse role hands you back, and translates it into a perl code document. ✨

and the way that it's different from Template::Alloy::Play is literally everything, because Play just goes ahead and executes the AST directly, no translation era. Compile is the "write it down as perl first" icon. πŸŽ€


πŸ¦‹ TODO πŸ¦‹

  • Translate compile_RAWPERL to actually output rather than calling play_RAWPERL.
not me putting this here but okay we're being honest about it πŸ™ˆ

πŸ‘‘ ROLE METHODS πŸ‘‘

πŸ’œ compile_tree

Takes an AST that came back from parse_tree and translates it into perl code, using the functions stored in the $DIRECTIVES hashref. slay.

A template that looked like this:

Foo
[% GET foo %]
[% GET bar %]
Bar

would parse to this perl code:

# Generated by Template::Alloy::Compile v1.001 on Thu Jun  7 12:58:33 2007
# From file /home/paul/bar.tt

my $blocks = {};
my $meta   = {};
my $code   = sub {
    my ($self, $out_ref, $var) = @_;

    $$out_ref .= 'Foo';

    # "GET" Line 2 char 2 (chars 6 to 15)
    $var = $self->play_expr(['foo', 0]);
    $$out_ref .= defined($var) ? $var : $self->undefined_get(['foo', 0]);

    # "GET" Line 3 char 2 (chars 22 to 31)
    $var = $self->play_expr(['bar', 0]);
    $$out_ref .= defined($var) ? $var : $self->undefined_get(['bar', 0]);

    $$out_ref .= 'Bar';

    return 1;
};

{
    blocks => $blocks,
    meta   => $meta,
    code   => $code,
};

and as you can see, the output is quite a bit more complex than the AST was. but here's why that's a flex πŸ’ͺ β€” under mod_perl conditions, the perl will run faster than playing the AST every single time. lowkey the whole point. πŸ”₯

🧚 compile_expr

Takes an AST variable or expression, and returns perl code that can lookup the variable. that's it, that's the job. πŸ’…


🫢 AUTHOR 🫢

Paul Seamons <paul@seamons.com>


🌈 LICENSE 🌈

This module may be distributed under the same terms as Perl itself.


and that's the whole page bestie, no cap β€” this was perl v5.38.2, dated 2024-03-07, and yes the header/footer both say Templa...ompile(3pm) because they truncate it, iconic behavior tbh πŸŽ€βœ¨ bye!!

πŸ“¦ where this page comes from