Debian Codegraph @

upstream/debian-unstable/man3/Encode::Encoder.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (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 .\"
57 .IX Title "Encode::Encoder 3perl"
58 .TH Encode::Encoder 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Encode::Encoder \-\- Object Oriented Encoder
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 10
68 \& use Encode::Encoder;
69 \& # Encode::encode("ISO\-8859\-1", $data);
70 \& Encode::Encoder\->new($data)\->iso_8859_1; # OOP way
71 \& # shortcut
72 \& use Encode::Encoder qw(encoder);
73 \& encoder($data)\->iso_8859_1;
74 \& # you can stack them!
75 \& encoder($data)\->iso_8859_1\->base64; # provided base64() is defined
76 \& # you can use it as a decoder as well
77 \& encoder($base64)\->bytes(\*(Aqbase64\*(Aq)\->latin1;
78 \& # stringified
79 \& print encoder($data)\->utf8\->latin1; # prints the string in latin1
80 \& # numified
81 \& encoder("\ex{abcd}\ex{ef}g")\->utf8 == 6; # true. bytes::length($data)
82 .Ve
83 .SH ABSTRACT
84 .IX Header "ABSTRACT"
85 \&\fBEncode::Encoder\fR allows you to use Encode in an object-oriented
86 style. This is not only more intuitive than a functional approach,
87 but also handier when you want to stack encodings. Suppose you want
88 your UTF\-8 string converted to Latin1 then Base64: you can simply say
89 .PP
90 .Vb 1
91 \& my $base64 = encoder($utf8)\->latin1\->base64;
92 .Ve
93 .PP
94 instead of
95 .PP
96 .Vb 2
97 \& my $latin1 = encode("latin1", $utf8);
98 \& my $base64 = encode_base64($utf8);
99 .Ve
100 .PP
101 or the lazier and more convoluted
102 .PP
103 .Vb 1
104 \& my $base64 = encode_base64(encode("latin1", $utf8));
105 .Ve
106 .SH Description
107 .IX Header "Description"
108 Here is how to use this module.
109 .IP \(bu 4
110 There are at least two instance variables stored in a hash reference,
111 {data} and {encoding}.
112 .IP \(bu 4
113 When there is no method, it takes the method name as the name of the
114 encoding and encodes the instance \fIdata\fR with \fIencoding\fR. If successful,
115 the instance \fIencoding\fR is set accordingly.
116 .IP \(bu 4
117 You can retrieve the result via \->data but usually you don't have to
118 because the stringify operator ("") is overridden to do exactly that.
119 .SS "Predefined Methods"
120 .IX Subsection "Predefined Methods"
121 This module predefines the methods below:
122 .ie n .IP "$e = Encode::Encoder\->new([$data, $encoding]);" 4
123 .el .IP "\f(CW$e\fR = Encode::Encoder\->new([$data, \f(CW$encoding\fR]);" 4
124 .IX Item "$e = Encode::Encoder->new([$data, $encoding]);"
125 returns an encoder object. Its data is initialized with \f(CW$data\fR if
126 present, and its encoding is set to \f(CW$encoding\fR if present.
127 .Sp
128 When \f(CW$encoding\fR is omitted, it defaults to utf8 if \f(CW$data\fR is already in
129 utf8 or "" (empty string) otherwise.
130 .IP \fBencoder()\fR 4
131 .IX Item "encoder()"
132 is an alias of Encode::Encoder\->\fBnew()\fR. This one is exported on demand.
133 .ie n .IP $e\->data([$data]) 4
134 .el .IP \f(CW$e\fR\->data([$data]) 4
135 .IX Item "$e->data([$data])"
136 When \f(CW$data\fR is present, sets the instance data to \f(CW$data\fR and returns the
137 object itself. Otherwise, the current instance data is returned.
138 .ie n .IP $e\->encoding([$encoding]) 4
139 .el .IP \f(CW$e\fR\->encoding([$encoding]) 4
140 .IX Item "$e->encoding([$encoding])"
141 When \f(CW$encoding\fR is present, sets the instance encoding to \f(CW$encoding\fR and
142 returns the object itself. Otherwise, the current instance encoding is
143 returned.
144 .ie n .IP $e\->bytes([$encoding]) 4
145 .el .IP \f(CW$e\fR\->bytes([$encoding]) 4
146 .IX Item "$e->bytes([$encoding])"
147 decodes instance data from \f(CW$encoding\fR, or the instance encoding if
148 omitted. If the conversion is successful, the instance encoding
149 will be set to "".
150 .Sp
151 The name \fIbytes\fR was deliberately picked to avoid namespace tainting
152 \&\-\- this module may be used as a base class so method names that appear
153 in Encode::Encoding are avoided.
154 .SS "Example: base64 transcoder"
155 .IX Subsection "Example: base64 transcoder"
156 This module is designed to work with Encode::Encoding.
157 To make the Base64 transcoder example above really work, you could
158 write a module like this:
159 .PP
160 .Vb 10
161 \& package Encode::Base64;
162 \& use parent \*(AqEncode::Encoding\*(Aq;
163 \& _\|_PACKAGE_\|_\->Define(\*(Aqbase64\*(Aq);
164 \& use MIME::Base64;
165 \& sub encode{
166 \& my ($obj, $data) = @_;
167 \& return encode_base64($data);
168 \& }
169 \& sub decode{
170 \& my ($obj, $data) = @_;
171 \& return decode_base64($data);
172 \& }
173 \& 1;
174 \& _\|_END_\|_
175 .Ve
176 .PP
177 And your caller module would be something like this:
178 .PP
179 .Vb 2
180 \& use Encode::Encoder;
181 \& use Encode::Base64;
182 \&
183 \& # now you can really do the following
184 \&
185 \& encoder($data)\->iso_8859_1\->base64;
186 \& encoder($base64)\->bytes(\*(Aqbase64\*(Aq)\->latin1;
187 .Ve
188 .SS "Operator Overloading"
189 .IX Subsection "Operator Overloading"
190 This module overloads two operators, stringify ("") and numify (0+).
191 .PP
192 Stringify dumps the data inside the object.
193 .PP
194 Numify returns the number of bytes in the instance data.
195 .PP
196 They come in handy when you want to print or find the size of data.
197 .SH "SEE ALSO"
198 .IX Header "SEE ALSO"
199 Encode,
200 Encode::Encoding