Debian Codegraph @

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