1Encode::Encoder(3)    User Contributed Perl Documentation   Encode::Encoder(3)
2
3
4

NAME

6       Encode::Encoder -- Object Oriented Encoder
7

SYNOPSIS

9         use Encode::Encoder;
10         # Encode::encode("ISO-8859-1", $data);
11         Encode::Encoder->new($data)->iso_8859_1; # OOP way
12         # shortcut
13         use Encode::Encoder qw(encoder);
14         encoder($data)->iso_8859_1;
15         # you can stack them!
16         encoder($data)->iso_8859_1->base64;  # provided base64() is defined
17         # you can use it as a decoder as well
18         encoder($base64)->bytes('base64')->latin1;
19         # stringified
20         print encoder($data)->utf8->latin1;  # prints the string in latin1
21         # numified
22         encoder("\x{abcd}\x{ef}g")->utf8 == 6; # true. bytes::length($data)
23

ABSTRACT

25       Encode::Encoder allows you to use Encode in an object-oriented style.
26       This is not only more intuitive than a functional approach, but also
27       handier when you want to stack encodings.  Suppose you want your UTF-8
28       string converted to Latin1 then Base64: you can simply say
29
30         my $base64 = encoder($utf8)->latin1->base64;
31
32       instead of
33
34         my $latin1 = encode("latin1", $utf8);
35         my $base64 = encode_base64($utf8);
36
37       or the lazier and more convoluted
38
39         my $base64 = encode_base64(encode("latin1", $utf8));
40

Description

42       Here is how to use this module.
43
44       ·   There are at least two instance variables stored in a hash
45           reference, {data} and {encoding}.
46
47       ·   When there is no method, it takes the method name as the name of
48           the encoding and encodes the instance data with encoding.  If
49           successful, the instance encoding is set accordingly.
50
51       ·   You can retrieve the result via ->data but usually you don't have
52           to because the stringify operator ("") is overridden to do exactly
53           that.
54
55   Predefined Methods
56       This module predefines the methods below:
57
58       $e = Encode::Encoder->new([$data, $encoding]);
59           returns an encoder object.  Its data is initialized with $data if
60           present, and its encoding is set to $encoding if present.
61
62           When $encoding is omitted, it defaults to utf8 if $data is already
63           in utf8 or "" (empty string) otherwise.
64
65       encoder()
66           is an alias of Encode::Encoder->new().  This one is exported on
67           demand.
68
69       $e->data([$data])
70           When $data is present, sets the instance data to $data and returns
71           the object itself.  Otherwise, the current instance data is
72           returned.
73
74       $e->encoding([$encoding])
75           When $encoding is present, sets the instance encoding to $encoding
76           and returns the object itself.  Otherwise, the current instance
77           encoding is returned.
78
79       $e->bytes([$encoding])
80           decodes instance data from $encoding, or the instance encoding if
81           omitted.  If the conversion is successful, the instance encoding
82           will be set to "".
83
84           The name bytes was deliberately picked to avoid namespace tainting
85           -- this module may be used as a base class so method names that
86           appear in Encode::Encoding are avoided.
87
88   Example: base64 transcoder
89       This module is designed to work with Encode::Encoding.  To make the
90       Base64 transcoder example above really work, you could write a module
91       like this:
92
93         package Encode::Base64;
94         use parent 'Encode::Encoding';
95         __PACKAGE__->Define('base64');
96         use MIME::Base64;
97         sub encode{
98             my ($obj, $data) = @_;
99             return encode_base64($data);
100         }
101         sub decode{
102             my ($obj, $data) = @_;
103             return decode_base64($data);
104         }
105         1;
106         __END__
107
108       And your caller module would be something like this:
109
110         use Encode::Encoder;
111         use Encode::Base64;
112
113         # now you can really do the following
114
115         encoder($data)->iso_8859_1->base64;
116         encoder($base64)->bytes('base64')->latin1;
117
118   Operator Overloading
119       This module overloads two operators, stringify ("") and numify (0+).
120
121       Stringify dumps the data inside the object.
122
123       Numify returns the number of bytes in the instance data.
124
125       They come in handy when you want to print or find the size of data.
126

SEE ALSO

128       Encode, Encode::Encoding
129
130
131
132perl v5.32.0                      2020-12-02                Encode::Encoder(3)
Impressum