Upgrade to Encode 1.26, from Dan Kogai.
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / Encoding.pm
CommitLineData
18586f54 1package Encode::Encoding;
2# Base class for classes which implement encodings
3use strict;
f2a2953c 4our $VERSION = do { my @r = (q$Revision: 1.25 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
18586f54 5
6sub Define
7{
8 my $obj = shift;
9 my $canonical = shift;
10 $obj = bless { Name => $canonical },$obj unless ref $obj;
11 # warn "$canonical => $obj\n";
f2a2953c 12 Encode::define_encoding($obj, $canonical, @_);
18586f54 13}
14
15sub name { shift->{'Name'} }
16
17# Temporary legacy methods
18sub toUnicode { shift->decode(@_) }
19sub fromUnicode { shift->encode(@_) }
20
21sub new_sequence { return $_[0] }
22
284ee456 23sub DESTROY {}
24
18586f54 251;
26__END__
1b2c56c8 27
28=head1 NAME
29
30Encode::Encoding - Encode Implementation Base Class
31
32=head1 SYNOPSIS
33
34 package Encode::MyEncoding;
35 use base qw(Encode::Encoding);
36
37 __PACKAGE__->Define(qw(myCanonical myAlias));
38
5129552c 39=head1 DESCRIPTION
1b2c56c8 40
41As mentioned in L<Encode>, encodings are (in the current
42implementation at least) defined by objects. The mapping of encoding
43name to object is via the C<%encodings> hash.
44
45The values of the hash can currently be either strings or objects.
46The string form may go away in the future. The string form occurs
47when C<encodings()> has scanned C<@INC> for loadable encodings but has
48not actually loaded the encoding in question. This is because the
49current "loading" process is all Perl and a bit slow.
50
51Once an encoding is loaded then value of the hash is object which
52implements the encoding. The object should provide the following
53interface:
54
55=over 4
56
57=item -E<gt>name
58
59Should return the string representing the canonical name of the encoding.
60
61=item -E<gt>new_sequence
62
63This is a placeholder for encodings with state. It should return an
64object which implements this interface, all current implementations
65return the original object.
66
67=item -E<gt>encode($string,$check)
68
69Should return the octet sequence representing I<$string>. If I<$check>
70is true it should modify I<$string> in place to remove the converted
71part (i.e. the whole string unless there is an error). If an error
72occurs it should return the octet sequence for the fragment of string
73that has been converted, and modify $string in-place to remove the
74converted part leaving it starting with the problem fragment.
75
76If check is is false then C<encode> should make a "best effort" to
77convert the string - for example by using a replacement character.
78
79=item -E<gt>decode($octets,$check)
80
81Should return the string that I<$octets> represents. If I<$check> is
82true it should modify I<$octets> in place to remove the converted part
83(i.e. the whole sequence unless there is an error). If an error
84occurs it should return the fragment of string that has been
85converted, and modify $octets in-place to remove the converted part
86leaving it starting with the problem fragment.
87
88If check is is false then C<decode> should make a "best effort" to
89convert the string - for example by using Unicode's "\x{FFFD}" as a
90replacement character.
91
92=back
93
94It should be noted that the check behaviour is different from the
95outer public API. The logic is that the "unchecked" case is useful
96when encoding is part of a stream which may be reporting errors
97(e.g. STDERR). In such cases it is desirable to get everything
98through somehow without causing additional errors which obscure the
99original one. Also the encoding is best placed to know what the
100correct replacement character is, so if that is the desired behaviour
101then letting low level code do it is the most efficient.
102
103In contrast if check is true, the scheme above allows the encoding to
104do as much as it can and tell layer above how much that was. What is
105lacking at present is a mechanism to report what went wrong. The most
106likely interface will be an additional method call to the object, or
107perhaps (to avoid forcing per-stream objects on otherwise stateless
108encodings) and additional parameter.
109
110It is also highly desirable that encoding classes inherit from
111C<Encode::Encoding> as a base class. This allows that class to define
112additional behaviour for all encoding objects. For example built in
113Unicode, UCS-2 and UTF-8 classes use :
114
115 package Encode::MyEncoding;
116 use base qw(Encode::Encoding);
117
118 __PACKAGE__->Define(qw(myCanonical myAlias));
119
120To create an object with bless {Name => ...},$class, and call
121define_encoding. They inherit their C<name> method from
122C<Encode::Encoding>.
123
124=head2 Compiled Encodings
125
67d7b5ef 126For the sake of speed and efficiency, Most of the encodings are now
127supported via I<Compiled Form> that are XS modules generated from UCM
128files. Encode provides enc2xs tool to achieve that. Please see
129L<enc2xs> for more details.
1b2c56c8 130
67d7b5ef 131=head1 SEE ALSO
1b2c56c8 132
67d7b5ef 133L<perlmod>, L<enc2xs>
1b2c56c8 134
f2a2953c 135=for future
136
137
138=over 4
139
140=item Scheme 1
141
142Passed remaining fragment of string being processed.
143Modifies it in place to remove bytes/characters it can understand
144and returns a string used to represent them.
145e.g.
146
147 sub fixup {
148 my $ch = substr($_[0],0,1,'');
149 return sprintf("\x{%02X}",ord($ch);
150 }
151
152This scheme is close to how underlying C code for Encode works, but gives
153the fixup routine very little context.
154
155=item Scheme 2
156
157Passed original string, and an index into it of the problem area, and
158output string so far. Appends what it will to output string and
159returns new index into original string. For example:
160
161 sub fixup {
162 # my ($s,$i,$d) = @_;
163 my $ch = substr($_[0],$_[1],1);
164 $_[2] .= sprintf("\x{%02X}",ord($ch);
165 return $_[1]+1;
166 }
167
168This scheme gives maximal control to the fixup routine but is more
169complicated to code, and may need internals of Encode to be tweaked to
170keep original string intact.
171
172=item Other Schemes
173
174Hybrids of above.
175
176Multiple return values rather than in-place modifications.
177
178Index into the string could be C<pos($str)> allowing C<s/\G...//>.
179
180=back
181
1b2c56c8 182=cut