1Encode::Encoder(3pm)   Perl Programmers Reference Guide   Encode::Encoder(3pm)
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 refer‐
45           ence, {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 suc‐
49           cessful, 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
57       This module predefines the methods below:
58
59       $e = Encode::Encoder->new([$data, $encoding]);
60           returns an encoder object.  Its data is initialized with $data if
61           present, and its encoding is set to $encoding if present.
62
63           When $encoding is omitted, it defaults to utf8 if $data is already
64           in utf8 or "" (empty string) otherwise.
65
66       encoder()
67           is an alias of Encode::Encoder->new().  This one is exported on
68           demand.
69
70       $e->data([$data])
71           When $data is present, sets the instance data to $data and returns
72           the object itself.  Otherwise, the current instance data is
73           returned.
74
75       $e->encoding([$encoding])
76           When $encoding is present, sets the instance encoding to $encoding
77           and returns the object itself.  Otherwise, the current instance
78           encoding is returned.
79
80       $e->bytes([$encoding])
81           decodes instance data from $encoding, or the instance encoding if
82           omitted.  If the conversion is successful, the instance encoding
83           will be set to "".
84
85           The name bytes was deliberately picked to avoid namespace tainting
86           -- this module may be used as a base class so method names that
87           appear in Encode::Encoding are avoided.
88
89       Example: base64 transcoder
90
91       This module is designed to work with Encode::Encoding.  To make the
92       Base64 transcoder example above really work, you could write a module
93       like this:
94
95         package Encode::Base64;
96         use base 'Encode::Encoding';
97         __PACKAGE__->Define('base64');
98         use MIME::Base64;
99         sub encode{
100             my ($obj, $data) = @_;
101             return encode_base64($data);
102         }
103         sub decode{
104             my ($obj, $data) = @_;
105             return decode_base64($data);
106         }
107         1;
108         __END__
109
110       And your caller module would be something like this:
111
112         use Encode::Encoder;
113         use Encode::Base64;
114
115         # now you can really do the following
116
117         encoder($data)->iso_8859_1->base64;
118         encoder($base64)->bytes('base64')->latin1;
119
120       Operator Overloading
121
122       This module overloads two operators, stringify ("") and numify (0+).
123
124       Stringify dumps the data inside the object.
125
126       Numify returns the number of bytes in the instance data.
127
128       They come in handy when you want to print or find the size of data.
129

SEE ALSO

131       Encode, Encode::Encoding
132
133
134
135perl v5.8.8                       2001-09-21              Encode::Encoder(3pm)
Impressum