1Encode::Encoding(3)   User Contributed Perl Documentation  Encode::Encoding(3)
2
3
4

NAME

6       Encode::Encoding - Encode Implementation Base Class
7

SYNOPSIS

9         package Encode::MyEncoding;
10         use parent qw(Encode::Encoding);
11
12         __PACKAGE__->Define(qw(myCanonical myAlias));
13

DESCRIPTION

15       As mentioned in Encode, encodings are (in the current implementation at
16       least) defined as objects. The mapping of encoding name to object is
17       via the %Encode::Encoding hash.  Though you can directly manipulate
18       this hash, it is strongly encouraged to use this base class module and
19       add encode() and decode() methods.
20
21   Methods you should implement
22       You are strongly encouraged to implement methods below, at least either
23       encode() or decode().
24
25       ->encode($string [,$check])
26           MUST return the octet sequence representing $string.
27
28           · If $check is true, it SHOULD modify $string in place to remove
29             the converted part (i.e.  the whole string unless there is an
30             error).  If perlio_ok() is true, SHOULD becomes MUST.
31
32           · If an error occurs, it SHOULD return the octet sequence for the
33             fragment of string that has been converted and modify $string in-
34             place to remove the converted part leaving it starting with the
35             problem fragment.  If perlio_ok() is true, SHOULD becomes MUST.
36
37           · If $check is false then "encode" MUST  make a "best effort" to
38             convert the string - for example, by using a replacement
39             character.
40
41       ->decode($octets [,$check])
42           MUST return the string that $octets represents.
43
44           · If $check is true, it SHOULD modify $octets in place to remove
45             the converted part (i.e.  the whole sequence unless there is an
46             error).  If perlio_ok() is true, SHOULD becomes MUST.
47
48           · If an error occurs, it SHOULD return the fragment of string that
49             has been converted and modify $octets in-place to remove the
50             converted part leaving it starting with the problem fragment.  If
51             perlio_ok() is true, SHOULD becomes MUST.
52
53           · If $check is false then "decode" should make a "best effort" to
54             convert the string - for example by using Unicode's "\x{FFFD}" as
55             a replacement character.
56
57       If you want your encoding to work with encoding pragma, you should also
58       implement the method below.
59
60       ->cat_decode($destination, $octets, $offset, $terminator [,$check])
61           MUST decode $octets with $offset and concatenate it to
62           $destination.  Decoding will terminate when $terminator (a string)
63           appears in output.  $offset will be modified to the last $octets
64           position at end of decode.  Returns true if $terminator appears
65           output, else returns false.
66
67   Other methods defined in Encode::Encodings
68       You do not have to override methods shown below unless you have to.
69
70       ->name
71           Predefined As:
72
73             sub name  { return shift->{'Name'} }
74
75           MUST return the string representing the canonical name of the
76           encoding.
77
78       ->mime_name
79           Predefined As:
80
81             sub mime_name{
82               return Encode::MIME::Name::get_mime_name(shift->name);
83             }
84
85           MUST return the string representing the IANA charset name of the
86           encoding.
87
88       ->renew
89           Predefined As:
90
91             sub renew {
92               my $self = shift;
93               my $clone = bless { %$self } => ref($self);
94               $clone->{renewed}++;
95               return $clone;
96             }
97
98           This method reconstructs the encoding object if necessary.  If you
99           need to store the state during encoding, this is where you clone
100           your object.
101
102           PerlIO ALWAYS calls this method to make sure it has its own private
103           encoding object.
104
105       ->renewed
106           Predefined As:
107
108             sub renewed { $_[0]->{renewed} || 0 }
109
110           Tells whether the object is renewed (and how many times).  Some
111           modules emit "Use of uninitialized value in null operation" warning
112           unless the value is numeric so return 0 for false.
113
114       ->perlio_ok()
115           Predefined As:
116
117             sub perlio_ok {
118               return eval { require PerlIO::encoding } ? 1 : 0;
119             }
120
121           If your encoding does not support PerlIO for some reasons, just;
122
123            sub perlio_ok { 0 }
124
125       ->needs_lines()
126           Predefined As:
127
128             sub needs_lines { 0 };
129
130           If your encoding can work with PerlIO but needs line buffering, you
131           MUST define this method so it returns true.  7bit ISO-2022
132           encodings are one example that needs this.  When this method is
133           missing, false is assumed.
134
135   Example: Encode::ROT13
136         package Encode::ROT13;
137         use strict;
138         use parent qw(Encode::Encoding);
139
140         __PACKAGE__->Define('rot13');
141
142         sub encode($$;$){
143             my ($obj, $str, $chk) = @_;
144             $str =~ tr/A-Za-z/N-ZA-Mn-za-m/;
145             $_[1] = '' if $chk; # this is what in-place edit means
146             return $str;
147         }
148
149         # Jr pna or ynml yvxr guvf;
150         *decode = \&encode;
151
152         1;
153

Why the heck Encode API is different?

155       It should be noted that the $check behaviour is different from the
156       outer public API. The logic is that the "unchecked" case is useful when
157       the encoding is part of a stream which may be reporting errors (e.g.
158       STDERR).  In such cases, it is desirable to get everything through
159       somehow without causing additional errors which obscure the original
160       one. Also, the encoding is best placed to know what the correct
161       replacement character is, so if that is the desired behaviour then
162       letting low level code do it is the most efficient.
163
164       By contrast, if $check is true, the scheme above allows the encoding to
165       do as much as it can and tell the layer above how much that was. What
166       is lacking at present is a mechanism to report what went wrong. The
167       most likely interface will be an additional method call to the object,
168       or perhaps (to avoid forcing per-stream objects on otherwise stateless
169       encodings) an additional parameter.
170
171       It is also highly desirable that encoding classes inherit from
172       "Encode::Encoding" as a base class. This allows that class to define
173       additional behaviour for all encoding objects.
174
175         package Encode::MyEncoding;
176         use parent qw(Encode::Encoding);
177
178         __PACKAGE__->Define(qw(myCanonical myAlias));
179
180       to create an object with "bless {Name => ...}, $class", and call
181       define_encoding.  They inherit their "name" method from
182       "Encode::Encoding".
183
184   Compiled Encodings
185       For the sake of speed and efficiency, most of the encodings are now
186       supported via a compiled form: XS modules generated from UCM files.
187       Encode provides the enc2xs tool to achieve that.  Please see enc2xs for
188       more details.
189

SEE ALSO

191       perlmod, enc2xs
192
193
194
195perl v5.30.2                      2020-05-04               Encode::Encoding(3)
Impressum