Encode::Encoder(3)
π NAME π
Encode::Encoder -- Object Oriented Encoder. This is a User Contributed Perl Documentation page, and it's lowkey serving very useful Perl-module energy. π«Ά
π¦ SYNOPSIS π¦
Bestie, here's the whole vibe in code β every line stays exactly as it is:
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)
πΈ ABSTRACT πΈ
Encode::Encoder lets you use Encode in an object-oriented style. And ngl, that's not only more intuitive than a functional approach, it's also handier when you want to stack encodings. Suppose you want your UTF-8 string converted to Latin1 and then Base64: you can simply say
my $base64 = encoder($utf8)->latin1->base64;
instead of
my $latin1 = encode("latin1", $utf8);
my $base64 = encode_base64($utf8);
or the lazier and more convoluted
my $base64 = encode_base64(encode("latin1", $utf8));
π Description π
Here's how to use this module, no cap:
- There are at least two instance variables stored in a hash reference,
{data}and{encoding}. π - When there's no method for what you're asking, 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. β¨
- You can retrieve the result via
->data, but usually you don't have to, because the stringify operator ("") is overridden to do exactly that. Iconic.
π 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. πencoder()β is an alias ofEncode::Encoder->new(). This one is exported on demand. π«$e->data([$data])β when$datais present, sets the instance data to$dataand returns the object itself. Otherwise, the current instance data is returned. π«Ά$e->encoding([$encoding])β when$encodingis present, sets the instance encoding to$encodingand returns the object itself. Otherwise, the current instance encoding is returned. π$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. π
π 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;
π¦ 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 data. Periodt.
π SEE ALSO π
EncodeEncode::Encoding
That's the whole page, bestie! π«Ά This Encode::Encoder(3) page has source/version perl v5.42.0, dated 2024-03-08. Bye and slay! π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Encode 3.210.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Encode 3.210.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Encode 3.210.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Encode 3.210.0-bp160.1.12 Β· Artistic-1.0 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