Encode::Encoder(3perl)
π NAME π
Encode::Encoder β the Object Oriented Encoder, bestie. That's it, that's the whole name, slay.
β¨ SYNOPSIS β¨
Okay so here's the whole vibe in code, no cap:
use Encode::Encoder;
# Encode::encode("ISO-8859-1", $data);
Encode::Encoder->new($data)->iso_8859_1; # OOP way
# shortcut
use Encode::Encoder qw(encoder);
encoder($data)->iso_8859_1;
# you can stack them!
encoder($data)->iso_8859_1->base64; # provided base64() is defined
# you can use it as a decoder as well
encoder($base64)->bytes('base64')->latin1;
# stringified
print encoder($data)->utf8->latin1; # prints the string in latin1
# numified
encoder("\x{abcd}\x{ef}g")->utf8 == 6; # true. bytes::length($data)
Stack them. Chain them. Decode with them. Print them. Count them. It's giving main character energy fr fr π
π¦ ABSTRACT π¦
Encode::Encoder lets you use Encode in an object-oriented style. And like β that's not only more intuitive than the functional approach, it's also handier when you wanna stack encodings on top of each other. Imagine your UTF-8 string needs to go to Latin1 and then Base64? You literally just say:
my $base64 = encoder($utf8)->latin1->base64;
instead of the long way:
my $latin1 = encode("latin1", $utf8);
my $base64 = encode_base64($utf8);
or the even lazier, more convoluted way:
my $base64 = encode_base64(encode("latin1", $utf8));
one line. one vibe. iconic. β¨
πΈ Description πΈ
Here's how you actually use this module, bestie:
- There are at least two instance variables stored in a hash reference:
{data}and{encoding}. Those two carry the whole situation. - When there's no method matching what you called, it takes that method name and treats it as the name of the encoding, then encodes the instance data with it. And if that works? The instance encoding gets set accordingly. Cute, right? π
- You can get the result back via
->data, but usually you don't even have to, because the stringify operator ("") is overridden to do exactly that. So it just... becomes the string. Obsessed.
π Predefined Methods π
This module predefines the methods below, so these are the ones already loaded in the group chat:
$e = Encode::Encoder->new([$data, $encoding])β returns an encoder object, periodt. Its data gets initialized with$dataif it's there, and its encoding gets set to$encodingif that's there. When$encodingis omitted, it defaults toutf8if$datais already in utf8, or""(empty string) otherwise.encoder()β this is an alias ofEncode::Encoder->new(). It's exported on demand, so ask for it and you shall receive π«Ά$e->data([$data])β when$datais present, it sets the instance data to$dataand returns the object itself. Otherwise, it returns the current instance data. That's it, that's the method β¨$e->encoding([$encoding])β when$encodingis present, it sets the instance encoding to$encodingand returns the object itself. Otherwise, it returns the current instance encoding. Same energy, different field π$e->bytes([$encoding])β decodes instance data from$encoding, or from the instance encoding if you left it out. And if the conversion goes through successfully, the instance encoding will be set to"".
Btw, the name bytes was deliberately picked to avoid namespace tainting β this module may be used as a base class, so method names that show up in Encode::Encoding are avoided. iykyk π§
π Example: base64 transcoder π
This module is designed to work with Encode::Encoding. So to make that Base64 transcoder example up top actually work, you could write a module like this:
package Encode::Base64;
use parent 'Encode::Encoding';
__PACKAGE__->Define('base64');
use MIME::Base64;
sub encode{
my ($obj, $data) = @_;
return encode_base64($data);
}
sub decode{
my ($obj, $data) = @_;
return decode_base64($data);
}
1;
__END__
And your caller module would be something like this:
use Encode::Encoder;
use Encode::Base64;
# now you can really do the following
encoder($data)->iso_8859_1->base64;
encoder($base64)->bytes('base64')->latin1;
Look at that chaining. Chef's kiss. Ate and left no crumbs. π
β Operator Overloading β
This module overloads two operators, and I need you to know both of them:
- stringify (
"") - numify (
0+)
Stringify dumps the data inside the object.
Numify returns the number of bytes in the instance data.
They come in handy when you wanna print stuff or find the size of data. So lowkey essential, highkey useful π
π SEE ALSO π
EncodeEncode::Encoding
Okay bestie that's the whole page!! This one's from Perl version v5.44.0, dated 2026-10-17, and it lives in the manual as Encode::Encoder(3perl). Go forth and stack encodings, you absolute icon πβ¨ππ¦
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND 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