1Encode::Encoding(3pm)  Perl Programmers Reference Guide  Encode::Encoding(3pm)
2
3
4

NAME

6       Encode::Encoding - Encode Implementation Base Class
7

SYNOPSIS

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

Why the heck Encode API is different?

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

SEE ALSO

186       perlmod, enc2xs
187
188
189
190perl v5.8.8                       2001-09-21             Encode::Encoding(3pm)
Impressum