Debian Codegraph @

upstream/mageia-cauldron/man3pm/Encode::Encoder.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Encode::Encoder 3pm"
61 .TH Encode::Encoder 3pm 2023-12-22 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Encode::Encoder \-\- Object Oriented Encoder
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 10
71 \& use Encode::Encoder;
72 \& # Encode::encode("ISO\-8859\-1", $data);
73 \& Encode::Encoder\->new($data)\->iso_8859_1; # OOP way
74 \& # shortcut
75 \& use Encode::Encoder qw(encoder);
76 \& encoder($data)\->iso_8859_1;
77 \& # you can stack them!
78 \& encoder($data)\->iso_8859_1\->base64; # provided base64() is defined
79 \& # you can use it as a decoder as well
80 \& encoder($base64)\->bytes(\*(Aqbase64\*(Aq)\->latin1;
81 \& # stringified
82 \& print encoder($data)\->utf8\->latin1; # prints the string in latin1
83 \& # numified
84 \& encoder("\ex{abcd}\ex{ef}g")\->utf8 == 6; # true. bytes::length($data)
85 .Ve
86 .SH ABSTRACT
87 .IX Header "ABSTRACT"
88 \&\fBEncode::Encoder\fR allows you to use Encode in an object\-oriented
89 style. This is not only more intuitive than a functional approach,
90 but also handier when you want to stack encodings. Suppose you want
91 your UTF\-8 string converted to Latin1 then Base64: you can simply say
92 .PP
93 .Vb 1
94 \& my $base64 = encoder($utf8)\->latin1\->base64;
95 .Ve
96 .PP
97 instead of
98 .PP
99 .Vb 2
100 \& my $latin1 = encode("latin1", $utf8);
101 \& my $base64 = encode_base64($utf8);
102 .Ve
103 .PP
104 or the lazier and more convoluted
105 .PP
106 .Vb 1
107 \& my $base64 = encode_base64(encode("latin1", $utf8));
108 .Ve
109 .SH Description
110 .IX Header "Description"
111 Here is how to use this module.
112 .IP \(bu 4
113 There are at least two instance variables stored in a hash reference,
114 {data} and {encoding}.
115 .IP \(bu 4
116 When there is no method, it takes the method name as the name of the
117 encoding and encodes the instance \fIdata\fR with \fIencoding\fR. If successful,
118 the instance \fIencoding\fR is set accordingly.
119 .IP \(bu 4
120 You can retrieve the result via \->data but usually you don\*(Aqt have to
121 because the stringify operator ("") is overridden to do exactly that.
122 .SS "Predefined Methods"
123 .IX Subsection "Predefined Methods"
124 This module predefines the methods below:
125 .ie n .IP "$e = Encode::Encoder\->new([$data, $encoding]);" 4
126 .el .IP "\f(CW$e\fR = Encode::Encoder\->new([$data, \f(CW$encoding\fR]);" 4
127 .IX Item "$e = Encode::Encoder->new([$data, $encoding]);"
128 returns an encoder object. Its data is initialized with \f(CW$data\fR if
129 present, and its encoding is set to \f(CW$encoding\fR if present.
130 .Sp
131 When \f(CW$encoding\fR is omitted, it defaults to utf8 if \f(CW$data\fR is already in
132 utf8 or "" (empty string) otherwise.
133 .IP \fBencoder()\fR 4
134 .IX Item "encoder()"
135 is an alias of Encode::Encoder\->\fBnew()\fR. This one is exported on demand.
136 .ie n .IP $e\->data([$data]) 4
137 .el .IP \f(CW$e\fR\->data([$data]) 4
138 .IX Item "$e->data([$data])"
139 When \f(CW$data\fR is present, sets the instance data to \f(CW$data\fR and returns the
140 object itself. Otherwise, the current instance data is returned.
141 .ie n .IP $e\->encoding([$encoding]) 4
142 .el .IP \f(CW$e\fR\->encoding([$encoding]) 4
143 .IX Item "$e->encoding([$encoding])"
144 When \f(CW$encoding\fR is present, sets the instance encoding to \f(CW$encoding\fR and
145 returns the object itself. Otherwise, the current instance encoding is
146 returned.
147 .ie n .IP $e\->bytes([$encoding]) 4
148 .el .IP \f(CW$e\fR\->bytes([$encoding]) 4
149 .IX Item "$e->bytes([$encoding])"
150 decodes instance data from \f(CW$encoding\fR, or the instance encoding if
151 omitted. If the conversion is successful, the instance encoding
152 will be set to "".
153 .Sp
154 The name \fIbytes\fR was deliberately picked to avoid namespace tainting
155 \&\-\- this module may be used as a base class so method names that appear
156 in Encode::Encoding are avoided.
157 .SS "Example: base64 transcoder"
158 .IX Subsection "Example: base64 transcoder"
159 This module is designed to work with Encode::Encoding.
160 To make the Base64 transcoder example above really work, you could
161 write a module like this:
162 .PP
163 .Vb 10
164 \& package Encode::Base64;
165 \& use parent \*(AqEncode::Encoding\*(Aq;
166 \& _\|_PACKAGE_\|_\->Define(\*(Aqbase64\*(Aq);
167 \& use MIME::Base64;
168 \& sub encode{
169 \& my ($obj, $data) = @_;
170 \& return encode_base64($data);
171 \& }
172 \& sub decode{
173 \& my ($obj, $data) = @_;
174 \& return decode_base64($data);
175 \& }
176 \& 1;
177 \& _\|_END_\|_
178 .Ve
179 .PP
180 And your caller module would be something like this:
181 .PP
182 .Vb 2
183 \& use Encode::Encoder;
184 \& use Encode::Base64;
185 \&
186 \& # now you can really do the following
187 \&
188 \& encoder($data)\->iso_8859_1\->base64;
189 \& encoder($base64)\->bytes(\*(Aqbase64\*(Aq)\->latin1;
190 .Ve
191 .SS "Operator Overloading"
192 .IX Subsection "Operator Overloading"
193 This module overloads two operators, stringify ("") and numify (0+).
194 .PP
195 Stringify dumps the data inside the object.
196 .PP
197 Numify returns the number of bytes in the instance data.
198 .PP
199 They come in handy when you want to print or find the size of data.
200 .SH "SEE ALSO"
201 .IX Header "SEE ALSO"
202 Encode,
203 Encode::Encoding