HTML::...rmatter(3pm)
π NAME π
HTML::Template::Compiled::Formatter β bestie, it's an HTC subclass for using a formatter π
Like, it's HTML::Template::Compiled but it gets a glow-up: you can hand it a formatter and it'll style your objects before they hit the template. Iconic behavior, no cap. β¨
πΈ SYNOPSIS πΈ
okay so here's the whole vibe, copy-paste ready, literally exactly this:
my $formatter = {
'HTC::Class1' => {
fullname => sub {
$_[0]->first . ' ' . $_[0]->last
},
first => HTC::Class1->can('first'),
last => HTC::Class1->can('last'),
},
};
my $htc = HTML::Template::Compiled::Formatter->new(
path => 't/templates',
filename => 'formatter.htc',
debug => 0,
);
my $obj = bless ({ first => 'Abi', last => 'Gail'}, 'HTC::Class1');
$htc->param(
test => 23,
obj => $obj,
);
local $HTML::Template::Compiled::Formatter::formatter = $formatter;
my $out = $htc->output;
Let's break down the slay, because there's a lot going on here and it's all delicious π«Ά
- You build a
$formatterhashref. Each key is a class name β here it's'HTC::Class1'β and the value is another hashref mapping names to subs. π - Inside that inner hashref you get
fullname, which is a freshsubthat glues$_[0]->firstand$_[0]->lasttogether with a space in between. Custom formatter moment π - You also get
firstandlast, which are justHTC::Class1->can('first')andHTC::Class1->can('last')β i.e. you're borrowing the class's own methods as formatters, no extra work, we love efficiency. β - Then you spin up your
HTML::Template::Compiled::Formatter->new(...)withpath => 't/templates',filename => 'formatter.htc'anddebug => 0. Three args, three vibes, we move. π¦ - Next you
blessan anon hashref{ first => 'Abi', last => 'Gail'}into'HTC::Class1'and stash it in$obj. Abi Gail, the main character. π - Then
$htc->param(...)getstest => 23andobj => $obj. So your object is now feeding the template. Gorgeous. π - And here's the spicy bit:
local $HTML::Template::Compiled::Formatter::formatter = $formatter;β you're localizing that package variable so your formatter only applies for this scope. Dynamic scope realness, lowkey genius, and when the scope ends it reverts. Periodt. π - Finally
my $out = $htc->output;and boom, you've got output that's been formatted. Ate. π₯
ngl bestie, thelocalon$HTML::Template::Compiled::Formatter::formatteris doing heavy lifting here β it's a package variable, so if you don't localize it you're changing it for everyone. Guard your energy (and your globals). π
okay that's the whole page, she's short but she's mighty π if you made it this far you're officially a Unix girlie, welcome to the club, we have stickers π
this page came from the User Contributed Perl Documentation section of the manual, and it's tagging along with perl v5.36.0, dated 2022-11-19 β so go forth, format your objects, and be iconic β¨π π¦
π¦ where this page comes from
- debian bookworm: libhtml-template-compiled-perl 1.003-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libhtml-template-compiled-perl 1.003-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libhtml-template-compiled-perl 1.003-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libhtml-template-compiled-perl 1.003-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libhtml-template-compiled-perl 1.003-3 Β· 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