Badger...dec::TT(3pm)
π Badger::Codec::TT β the manual page, but make it make sense
okay bestie, this is the <pink>Badger...dec::TT(3pm)</pink> page from the User Contributed Perl Documentation, and it's literally about turning Perl data into TT-flavoured text and back again. no cap. let's go π¦
β¨ NAME β¨
Badger::Codec::TT β encode/decode data using TT data syntax π
πΈ SYNOPSIS πΈ
use Badger::Codec::TT;
my $codec = Badger::Codec::TT->new();
my $encoded = $codec->encode({ msg => "Hello World" });
my $decoded = $codec->decode($encoded);
π DESCRIPTION π
so basically this module is a subclass of Badger::Codec that encodes and decodes data to and from an extended form of the data definition syntax used in the Template Toolkit. iconic behaviour, honestly.
why does it even exist? two reasons, and I'm gonna say them both:
- it mainly exists for testing purposes β so that we don't have to make people install YAML or JSON just to run some of the Badger tests π
- it also supports some legacy systems that use data encoded this way (mostly dating back to the days before YAML and JSON were around)
ngl if you're starting out afresh, you're probably better off using YAML or JSON β unless you have a good reason not to. that's not me being shady, that's literally in the docs π
the syntax is similar to Perl: single quotes for literal strings, square brackets for list definitions, curly braces for hash definitions, plus the => "fat comma" operator to separate hash keys and values. data structures can be nested indefinitely. the unquoted undef token can be used to explicitly represent the undefined value. periodt β¨
{
message => 'Hello World, this is some text',
things => ['a list', 'of some things'],
stuff => {
pi => 3.14,
foo => [ { nested => 'hash' }, ['nested', 'list' ] ],
nul => undef,
},
}
TT syntax is more liberal than Perl, bestie. it lets you use = instead of => to separate keys and values in hash arrays, and the commas between items are optional. so casual. so free. π«
{
message = 'Hello World, this is some text'
things = ['a list' 'of some things']
stuff = {
pi = 3.14
foo = [ { nested = 'hash' } ['nested' 'list' ] ]
nul = undef
}
}
it will also accept : as a delimiter between hash keys and values, which gives you an overlap with a useful subset of JSON syntax:
{
message: 'Hello World, this is some text',
things: ['a list' 'of some things'],
stuff: {
pi: 3.14,
foo: [ { nested: 'hash' }, ['nested', 'list' ] ],
nul: undef
}
}
the decoder is very liberal in what it will accept for delimiters. you can mix and match any of the above styles in the same document if you really want to.
β οΈ however, you would be utterly batshit insane to do such a thing, let alone want for it. just because we'll accept any of the commonly used formats doesn't mean that you should be using them all at once. that's the rule, okay? π«Ά
{
perl => 'Perl looks like this',
tt = 'TT looks like this'
json: 'JSON looks like this
}
now the plot twist: even though the syntax is more liberal than either Perl or JSON, the semantics are decidedly stricter. it is not possible to embed arbitrary Perl code, instantiate Javascript objects, or do anything else outside of defining vanilla data structures. so don't even try it π
the encoder generates TT syntax by default (= for assignment, with a single space to delimiter items). you can change these options using the assign and comma configuration options. look at her go:
my $codec = Badger::Codec::TT->new( assign => '=>', comma => ',' );
print $codec->encode($some_data);
π METHODS π
encode($data)
encodes the Perl data in $data to a TT string. main character energy fr fr.
$encoded = Badger::Codec::TT->encode($data);
decode($tt)
decodes the encoded TT string in $tt back into a Perl data structure. the glow-up in reverse β¨
$decoded = Badger::Codec::TT->decode($encoded);
encoder()
this method returns a reference to an encoding subroutine. very demure, very mindful π
my $sub = Badger::Codec::TT->encoder;
$encoded = $sub->($data);
decoder()
this method returns a reference to a decoding subroutine.
my $sub = Badger::Codec::TT->decoder;
$decoded = $sub->($encoded);
π§ INTERNAL SUBROUTINES π§
_encode($data)
this internal subroutine performs the recursive encoding of the data. it's giving recursion, she's giving everything π
_decode($tt)
this internal subroutine performs the recursive decoding of the data. iykyk π
π AUTHOR π
Andy Wardley <http://wardley.org/>
π COPYRIGHT π
Copyright (C) 2005-2009 Andy Wardley. All rights reserved.
π¦ SEE ALSO π¦
Badger::CodecsBadger::CodecTemplate
okay that's the whole page, you ate it, no crumbs left π
β¨ and just so you know where this came from: it's perl v5.36.0, dated 2023-08-28, and it's the Badger...dec::TT(3pm) page. slay, go encode something cute ππ
π¦ where this page comes from
- debian trixie: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libbadger-perl 0.16-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