Debian Codegraph @

upstream/mageia-cauldron/man3pm/Encode::Encoding.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::Encoding 3pm"
61 .TH Encode::Encoding 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::Encoding \- Encode Implementation Base Class
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 2
71 \& package Encode::MyEncoding;
72 \& use parent qw(Encode::Encoding);
73 \&
74 \& _\|_PACKAGE_\|_\->Define(qw(myCanonical myAlias));
75 .Ve
76 .SH DESCRIPTION
77 .IX Header "DESCRIPTION"
78 As mentioned in Encode, encodings are (in the current
79 implementation at least) defined as objects. The mapping of encoding
80 name to object is via the \f(CW%Encode::Encoding\fR hash. Though you can
81 directly manipulate this hash, it is strongly encouraged to use this
82 base class module and add \fBencode()\fR and \fBdecode()\fR methods.
83 .SS "Methods you should implement"
84 .IX Subsection "Methods you should implement"
85 You are strongly encouraged to implement methods below, at least
86 either \fBencode()\fR or \fBdecode()\fR.
87 .IP "\->encode($string [,$check])" 4
88 .IX Item "->encode($string [,$check])"
89 MUST return the octet sequence representing \fR\f(CI$string\fR\fI\fR.
90 .RS 4
91 .IP \(bu 2
92 If \fR\f(CI$check\fR\fI\fR is true, it SHOULD modify \fI\fR\f(CI$string\fR\fI\fR in place to remove
93 the converted part (i.e. the whole string unless there is an error).
94 If \fBperlio_ok()\fR is true, SHOULD becomes MUST.
95 .IP \(bu 2
96 If an error occurs, it SHOULD return the octet sequence for the
97 fragment of string that has been converted and modify \f(CW$string\fR in\-place
98 to remove the converted part leaving it starting with the problem
99 fragment. If \fBperlio_ok()\fR is true, SHOULD becomes MUST.
100 .IP \(bu 2
101 If \fR\f(CI$check\fR\fI\fR is false then \f(CW\*(C`encode\*(C'\fR MUST make a "best effort" to
102 convert the string \- for example, by using a replacement character.
103 .RE
104 .RS 4
105 .RE
106 .IP "\->decode($octets [,$check])" 4
107 .IX Item "->decode($octets [,$check])"
108 MUST return the string that \fR\f(CI$octets\fR\fI\fR represents.
109 .RS 4
110 .IP \(bu 2
111 If \fR\f(CI$check\fR\fI\fR is true, it SHOULD modify \fI\fR\f(CI$octets\fR\fI\fR in place to remove
112 the converted part (i.e. the whole sequence unless there is an
113 error). If \fBperlio_ok()\fR is true, SHOULD becomes MUST.
114 .IP \(bu 2
115 If an error occurs, it SHOULD return the fragment of string that has
116 been converted and modify \f(CW$octets\fR in\-place to remove the converted
117 part leaving it starting with the problem fragment. If \fBperlio_ok()\fR is
118 true, SHOULD becomes MUST.
119 .IP \(bu 2
120 If \fR\f(CI$check\fR\fI\fR is false then \f(CW\*(C`decode\*(C'\fR should make a "best effort" to
121 convert the string \- for example by using Unicode\*(Aqs "\ex{FFFD}" as a
122 replacement character.
123 .RE
124 .RS 4
125 .RE
126 .PP
127 If you want your encoding to work with encoding pragma, you should
128 also implement the method below.
129 .ie n .IP "\->cat_decode($destination, $octets, $offset, $terminator [,$check])" 4
130 .el .IP "\->cat_decode($destination, \f(CW$octets\fR, \f(CW$offset\fR, \f(CW$terminator\fR [,$check])" 4
131 .IX Item "->cat_decode($destination, $octets, $offset, $terminator [,$check])"
132 MUST decode \fR\f(CI$octets\fR\fI\fR with \fI\fR\f(CI$offset\fR\fI\fR and concatenate it to \fI\fR\f(CI$destination\fR\fI\fR.
133 Decoding will terminate when \f(CW$terminator\fR (a string) appears in output.
134 \&\fI\fR\f(CI$offset\fR\fI\fR will be modified to the last \f(CW$octets\fR position at end of decode.
135 Returns true if \f(CW$terminator\fR appears output, else returns false.
136 .SS "Other methods defined in Encode::Encodings"
137 .IX Subsection "Other methods defined in Encode::Encodings"
138 You do not have to override methods shown below unless you have to.
139 .IP \->name 4
140 .IX Item "->name"
141 Predefined As:
142 .Sp
143 .Vb 1
144 \& sub name { return shift\->{\*(AqName\*(Aq} }
145 .Ve
146 .Sp
147 MUST return the string representing the canonical name of the encoding.
148 .IP \->mime_name 4
149 .IX Item "->mime_name"
150 Predefined As:
151 .Sp
152 .Vb 3
153 \& sub mime_name{
154 \& return Encode::MIME::Name::get_mime_name(shift\->name);
155 \& }
156 .Ve
157 .Sp
158 MUST return the string representing the IANA charset name of the encoding.
159 .IP \->renew 4
160 .IX Item "->renew"
161 Predefined As:
162 .Sp
163 .Vb 6
164 \& sub renew {
165 \& my $self = shift;
166 \& my $clone = bless { %$self } => ref($self);
167 \& $clone\->{renewed}++;
168 \& return $clone;
169 \& }
170 .Ve
171 .Sp
172 This method reconstructs the encoding object if necessary. If you need
173 to store the state during encoding, this is where you clone your object.
174 .Sp
175 PerlIO ALWAYS calls this method to make sure it has its own private
176 encoding object.
177 .IP \->renewed 4
178 .IX Item "->renewed"
179 Predefined As:
180 .Sp
181 .Vb 1
182 \& sub renewed { $_[0]\->{renewed} || 0 }
183 .Ve
184 .Sp
185 Tells whether the object is renewed (and how many times). Some
186 modules emit \f(CW\*(C`Use of uninitialized value in null operation\*(C'\fR warning
187 unless the value is numeric so return 0 for false.
188 .IP \->\fBperlio_ok()\fR 4
189 .IX Item "->perlio_ok()"
190 Predefined As:
191 .Sp
192 .Vb 3
193 \& sub perlio_ok {
194 \& return eval { require PerlIO::encoding } ? 1 : 0;
195 \& }
196 .Ve
197 .Sp
198 If your encoding does not support PerlIO for some reasons, just;
199 .Sp
200 .Vb 1
201 \& sub perlio_ok { 0 }
202 .Ve
203 .IP \->\fBneeds_lines()\fR 4
204 .IX Item "->needs_lines()"
205 Predefined As:
206 .Sp
207 .Vb 1
208 \& sub needs_lines { 0 };
209 .Ve
210 .Sp
211 If your encoding can work with PerlIO but needs line buffering, you
212 MUST define this method so it returns true. 7bit ISO\-2022 encodings
213 are one example that needs this. When this method is missing, false
214 is assumed.
215 .SS "Example: Encode::ROT13"
216 .IX Subsection "Example: Encode::ROT13"
217 .Vb 3
218 \& package Encode::ROT13;
219 \& use strict;
220 \& use parent qw(Encode::Encoding);
221 \&
222 \& _\|_PACKAGE_\|_\->Define(\*(Aqrot13\*(Aq);
223 \&
224 \& sub encode($$;$){
225 \& my ($obj, $str, $chk) = @_;
226 \& $str =~ tr/A\-Za\-z/N\-ZA\-Mn\-za\-m/;
227 \& $_[1] = \*(Aq\*(Aq if $chk; # this is what in\-place edit means
228 \& return $str;
229 \& }
230 \&
231 \& # Jr pna or ynml yvxr guvf;
232 \& *decode = \e&encode;
233 \&
234 \& 1;
235 .Ve
236 .SH "Why the heck Encode API is different?"
237 .IX Header "Why the heck Encode API is different?"
238 It should be noted that the \fR\f(CI$check\fR\fI\fR behaviour is different from the
239 outer public API. The logic is that the "unchecked" case is useful
240 when the encoding is part of a stream which may be reporting errors
241 (e.g. STDERR). In such cases, it is desirable to get everything
242 through somehow without causing additional errors which obscure the
243 original one. Also, the encoding is best placed to know what the
244 correct replacement character is, so if that is the desired behaviour
245 then letting low level code do it is the most efficient.
246 .PP
247 By contrast, if \fR\f(CI$check\fR\fI\fR is true, the scheme above allows the
248 encoding to do as much as it can and tell the layer above how much
249 that was. What is lacking at present is a mechanism to report what
250 went wrong. The most likely interface will be an additional method
251 call to the object, or perhaps (to avoid forcing per\-stream objects
252 on otherwise stateless encodings) an additional parameter.
253 .PP
254 It is also highly desirable that encoding classes inherit from
255 \&\f(CW\*(C`Encode::Encoding\*(C'\fR as a base class. This allows that class to define
256 additional behaviour for all encoding objects.
257 .PP
258 .Vb 2
259 \& package Encode::MyEncoding;
260 \& use parent qw(Encode::Encoding);
261 \&
262 \& _\|_PACKAGE_\|_\->Define(qw(myCanonical myAlias));
263 .Ve
264 .PP
265 to create an object with \f(CW\*(C`bless {Name => ...}, $class\*(C'\fR, and call
266 define_encoding. They inherit their \f(CW\*(C`name\*(C'\fR method from
267 \&\f(CW\*(C`Encode::Encoding\*(C'\fR.
268 .SS "Compiled Encodings"
269 .IX Subsection "Compiled Encodings"
270 For the sake of speed and efficiency, most of the encodings are now
271 supported via a \fIcompiled form\fR: XS modules generated from UCM
272 files. Encode provides the enc2xs tool to achieve that. Please see
273 enc2xs for more details.
274 .SH "SEE ALSO"
275 .IX Header "SEE ALSO"
276 perlmod, enc2xs