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 $data if present, and its encoding is set to $encoding if present. when $encoding is omitted, it defaults to utf8 if $data is already in utf8, or "" (empty string) otherwise. defaults, but make it fashion. πŸŽ€
  • encoder() β€” is an alias of Encode::Encoder->new(). this one is exported on demand. no cap. πŸ”₯
  • $e->data([$data]) β€” when $data is present, sets the instance data to $data and returns the object itself. otherwise, the current instance data is returned. it's giving two-way street. πŸ¦‹
  • $e->encoding([$encoding]) β€” when $encoding is present, sets the instance encoding to $encoding and 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 name bytes was deliberately picked to avoid namespace tainting β€” this module may be used as a base class, so method names that appear in Encode::Encoding are 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