Encode::Encoder(3pm)
π Encode::Encoder β the Object Oriented Encoder, bestie π
okay so this is Encode::Encoder, straight out of the Perl Programmers Reference Guide, and it's literally the glow-up version of Encode. Same facts, way better vibes. let's go β¨
πΈ SYNOPSIS πΈ
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)
like, look at that. chaining encodings like it's nothing. main character energy fr fr π₯
π¦ ABSTRACT π¦
Encode::Encoder lets you use Encode in an object-oriented style. and honestly? it's not only more intuitive than the functional approach, it's also so much handier when you wanna stack encodings on top of each other. π«Ά
say you want your UTF-8 string converted to Latin1 and then Base64. you can simply say:
my $base64 = encoder($utf8)->latin1->base64;
instead of this whole ordeal:
my $latin1 = encode("latin1", $utf8);
my $base64 = encode_base64($utf8);
or the lazier and more convoluted version:
my $base64 = encode_base64(encode("latin1", $utf8));
we love a concise queen π
β¨ Description β¨
here's how you actually use this module, no gatekeeping:
- there are at least two instance variables stored in a hash reference:
{data}and{encoding}. - when there's no method for what you're doing, it takes the method name as the name of the encoding and encodes the instance data with that encoding. if it's successful, the instance encoding is set accordingly. sneaky but iconic π
- you can retrieve the result via
->data, but usually you don't have to, because the stringify operator ("") is overridden to do exactly that. it just knows. π
π Predefined Methods π
this module predefines the methods below:
$e = Encode::Encoder->new([$data, $encoding]);β returns an encoder object. its data is initialized with$dataif present, and its encoding is set to$encodingif present. when$encodingis omitted, it defaults toutf8if$datais already in utf8, or""(empty string) otherwise. defaults, but make it fashion. πencoder()β is an alias ofEncode::Encoder->new(). this one is exported on demand. no cap. π₯$e->data([$data])β when$datais present, sets the instance data to$dataand returns the object itself. otherwise, the current instance data is returned. it's giving two-way street. π¦$e->encoding([$encoding])β when$encodingis present, sets the instance encoding to$encodingand returns the object itself. otherwise, the current instance encoding is returned. same energy, different field. β$e->bytes([$encoding])β decodes instance data from$encoding, or the instance encoding if omitted. if the conversion is successful, the instance encoding will be set to"". the namebyteswas deliberately picked to avoid namespace tainting β this module may be used as a base class, so method names that appear inEncode::Encodingare avoided. strategic naming, we stan π§
ngl, that last bullet is important if you're building on top of this thing. the name isn't random, it's on purpose. π«Ά
πΈ Example: base64 transcoder πΈ
this module is designed to work with Encode::Encoding. to make the Base64 transcoder example above really 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;
like?? look at it go. the stacking works. ππ
π Operator Overloading π
this module overloads two operators: stringify ("") and 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 want to print, or find the size of, your data. little operators, big slay. β¨
π SEE ALSO π
Encode, Encode::Encoding β go read those next, iykyk π
ok that's the whole page, bestie. no detail left behind, no option unmentioned. you're welcome. π β¨
with love from the perl v5.42.3 era, dated 2026-11-03 β this doc ate and left no crumbs ππ«Ά
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms