Upgrade to Encode 0.98, from Dan Kogai.
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / Details.pod
CommitLineData
1b2c56c8 1=head1 NAME
2
a63c962f 3Encode::Details - implementation details of Encode.pm
1b2c56c8 4
5=head1 DESCRIPTION
6
7The C<Encode> module provides the interfaces between Perl's strings
8and the rest of the system. Perl strings are sequences of B<characters>.
9
10The repertoire of characters that Perl can represent is at least that
11defined by the Unicode Consortium. On most platforms the ordinal
12values of the characters (as returned by C<ord(ch)>) is the "Unicode
13codepoint" for the character (the exceptions are those platforms where
14the legacy encoding is some variant of EBCDIC rather than a super-set
15of ASCII - see L<perlebcdic>).
16
a63c962f 17Traditionally computer data has been moved around in 8-bit chunks
1b2c56c8 18often called "bytes". These chunks are also known as "octets" in
19networking standards. Perl is widely used to manipulate data of
20many types - not only strings of characters representing human or
21computer languages but also "binary" data being the machines representation
22of numbers, pixels in an image - or just about anything.
23
24When Perl is processing "binary data" the programmer wants Perl to process
25"sequences of bytes". This is not a problem for Perl - as a byte has 256
26possible values it easily fits in Perl's much larger "logical character".
27
28Due to size concerns, each of B<CJK> (Chinese, Japanese & Korean) modules
29are not loaded in memory until the first time they're used. Although you
30don't have to C<use> the corresponding B<Encode::>(B<TW>|B<CN>|B<JP>|B<KR>)
31modules first, be aware that those encodings will not be in C<%encodings>
32until their module is loaded (either implicitly through using encodings
33contained in the same module, or via an explicit C<use>).
34
35=head2 TERMINOLOGY
36
37=over 4
38
39=item *
40
41I<character>: a character in the range 0..(2**32-1) (or more).
42(What Perl's strings are made of.)
43
44=item *
45
46I<byte>: a character in the range 0..255
47(A special case of a Perl character.)
48
49=item *
50
51I<octet>: 8 bits of data, with ordinal values 0..255
52(Term for bytes passed to or from a non-Perl context, e.g. disk file.)
53
54=back
55
56The marker [INTERNAL] marks Internal Implementation Details, in
57general meant only for those who think they know what they are doing,
58and such details may change in future releases.
59
60=head1 ENCODINGS
61
62=head2 Characteristics of an Encoding
63
64An encoding has a "repertoire" of characters that it can represent,
65and for each representable character there is at least one sequence of
66octets that represents it.
67
68=head2 Types of Encodings
69
70Encodings can be divided into the following types:
71
72=over 4
73
74=item * Fixed length 8-bit (or less) encodings.
75
76Each character is a single octet so may have a repertoire of up to
77256 characters. ASCII and iso-8859-* are typical examples.
78
79=item * Fixed length 16-bit encodings
80
81Each character is two octets so may have a repertoire of up to
8265 536 characters. Unicode's UCS-2 is an example. Also used for
83encodings for East Asian languages.
84
85=item * Fixed length 32-bit encodings.
86
87Not really very "encoded" encodings. The Unicode code points
88are just represented as 4-octet integers. None the less because
89different architectures use different representations of integers
a63c962f 90(so called "endian") there at least two distinct encodings.
1b2c56c8 91
92=item * Multi-byte encodings
93
94The number of octets needed to represent a character varies.
95UTF-8 is a particularly complex but regular case of a multi-byte
96encoding. Several East Asian countries use a multi-byte encoding
97where 1-octet is used to cover western roman characters and Asian
98characters get 2-octets.
99(UTF-16 is strictly a multi-byte encoding taking either 2 or 4 octets
100to represent a Unicode code point.)
101
102=item * "Escape" encodings.
103
104These encodings embed "escape sequences" into the octet sequence
105which describe how the following octets are to be interpreted.
106The iso-2022-* family is typical. Following the escape sequence
107octets are encoded by an "embedded" encoding (which will be one
108of the above types) until another escape sequence switches to
109a different "embedded" encoding.
110
111These schemes are very flexible and can handle mixed languages but are
112very complex to process (and have state). No escape encodings are
113implemented for Perl yet.
114
115=back
116
117=head2 Specifying Encodings
118
119Encodings can be specified to the API described below in two ways:
120
121=over 4
122
123=item 1. By name
124
125Encoding names are strings with characters taken from a restricted
126repertoire. See L</"Encoding Names">.
127
128=item 2. As an object
129
130Encoding objects are returned by C<find_encoding($name, [$skip_external])>.
131If the second parameter is true, Encode will refrain from loading external
132modules for CJK encodings.
133
134=back
135
136=head2 Encoding Names
137
138Encoding names are case insensitive. White space in names is ignored.
139In addition an encoding may have aliases. Each encoding has one
140"canonical" name. The "canonical" name is chosen from the names of
141the encoding by picking the first in the following sequence:
142
143=over 4
144
145=item * The MIME name as defined in IETF RFCs.
146
147=item * The name in the IANA registry.
148
149=item * The name used by the organization that defined it.
150
151=back
152
153Because of all the alias issues, and because in the general case
154encodings have state C<Encode> uses the encoding object internally
155once an operation is in progress.
156
157As of Perl 5.8.0, at least the following encodings are recognized
158(the => marks aliases):
159
160 ASCII
161
162 US-ASCII => ASCII
163
164The Unicode:
165
166 UTF-8
167 UTF-16
168 UCS-2
169
170 ISO 10646-1 => UCS-2
171
172The ISO 8859 and KOI:
173
174 ISO 8859-1 ISO 8859-6 ISO 8859-11 KOI8-F
175 ISO 8859-2 ISO 8859-7 (12 doesn't exist) KOI8-R
176 ISO 8859-3 ISO 8859-8 ISO 8859-13 KOI8-U
177 ISO 8859-4 ISO 8859-9 ISO 8859-14
178 ISO 8859-5 ISO 8859-10 ISO 8859-15
179 ISO 8859-16
180
181 Latin1 => 8859-1 Latin6 => 8859-10
182 Latin2 => 8859-2 Latin7 => 8859-13
183 Latin3 => 8859-3 Latin8 => 8859-14
184 Latin4 => 8859-4 Latin9 => 8859-15
185 Latin5 => 8859-9 Latin10 => 8859-16
186
187 Cyrillic => 8859-5
188 Arabic => 8859-6
189 Greek => 8859-7
190 Hebrew => 8859-8
191 Thai => 8859-11
192 TIS620 => 8859-11
193
194The CJKV: Chinese, Japanese, Korean, Vietnamese:
195
196 ISO 2022 ISO 2022 JP-1 JIS 0201 GB 1988 Big5 EUC-CN
197 ISO 2022 CN ISO 2022 JP-2 JIS 0208 GB 2312 HZ EUC-JP
198 ISO 2022 JP ISO 2022 KR JIS 0210 GB 12345 CNS 11643 EUC-JP-0212
199 Shift-JIS GBK Big5-HKSCS EUC-KR
200 VISCII ISO-IR-165
201
202(Due to size concerns, additional Chinese encodings including C<GB 18030>,
203C<EUC-TW> and C<BIG5PLUS> are distributed separately on CPAN, under the name
204L<Encode::HanExtra>.)
205
206The PC codepages:
207
208 CP37 CP852 CP861 CP866 CP949 CP1251 CP1256
209 CP424 CP855 CP862 CP869 CP950 CP1252 CP1257
210 CP737 CP856 CP863 CP874 CP1006 CP1253 CP1258
211 CP775 CP857 CP864 CP932 CP1047 CP1254
212 CP850 CP860 CP865 CP936 CP1250 CP1255
213
214 WinLatin1 => CP1252
215 WinLatin2 => CP1250
216 WinCyrillic => CP1251
217 WinGreek => CP1253
218 WinTurkiskh => CP1254
219 WinHebrew => CP1255
220 WinArabic => CP1256
221 WinBaltic => CP1257
222 WinVietnamese => CP1258
223
224(All the CPI<NNN...> are available also as IBMI<NNN...>.)
225
226The Mac codepages:
227
228 MacCentralEuropean MacJapanese
229 MacCroatian MacRoman
230 MacCyrillic MacRomanian
231 MacDingbats MacSami
232 MacGreek MacThai
233 MacIcelandic MacTurkish
234 MacUkraine
235
236Miscellaneous:
237
238 7bit-greek IR-197
239 7bit-kana NeXTstep
240 7bit-latin1 POSIX-BC
241 DingBats Roman8
242 GSM 0338 Symbol
243
244=head2 Encoding Classification
245
246Encodings
247
248 US-ASCII UTF-8 KOI8-R ISO-8859-*
249 ISO-2022-CN ISO-2022-JP ISO-2022-KR Big5
250 EUC-CN EUC-JP EUC-KR
251
252are L<http://www.iana.org/assignments/character-sets>-registered
253as preferred MIME names and may probably be used over the Internet.
254So is
255
256 Shift_JIS
257
258but despite its wide spread it bears the label of being
259Microsft proprietary.
260
261 UTF-16 KOI8-U ISO-2022-JP-2
262
a63c962f 263are IANA-registered preferred MIME names but probably should
1b2c56c8 264be avoided as encoding for web pages due to lack of browser
265support.
266
267
268 ISO-2022 (http://www.ecma.ch/ecma1/STAND/ECMA-035.HTM)
269 ISO-2022-JP-1 (http://www.faqs.org/rfcs/rfc2237.html)
270 ISO-IR-165 (http://www.faqs.org/rfcs/rfc1345.html)
271 GBK
272 VISCII
273 GB 12345 (only plains 1 and 2 available)
274 GB 18030
275 CNS 11643
276
277are totally valid encodings but not registered at IANA.
278
279 BIG5PLUS
280 EUC-JP-0212 (Encode::lib::Encode::Tcl::Extended)
281
282are a bit proprietary
283
284You may probably get some info on CJK encodings at
285
286 brief description for most of the mentioned CJK encodings
287 http://www.debian.org.ru/doc/manuals/intro-i18n/ch-codes.html
288
289 several years old, but still useful
290 http://www.oreilly.com/people/authors/lunde/cjk_inf.html
291
292 and some in-depth reading for the heroes :-)
293 http://www.ecma.ch/ecma1/STAND/ECMA-035.HTM (eq ISO-2022)
294 http://www.faqs.org/rfcs/rfc1345.txt
295
296
297=head1 PERL ENCODING API
298
299=head2 Generic Encoding Interface
300
301=over 4
302
303=item *
304
305 $bytes = encode(ENCODING, $string[, CHECK])
306
307Encodes string from Perl's internal form into I<ENCODING> and returns
308a sequence of octets. For CHECK see L</"Handling Malformed Data">.
309
310For example to convert (internally UTF-8 encoded) Unicode data
311to octets:
312
313 $octets = encode("utf8", $unicode);
314
315=item *
316
317 $string = decode(ENCODING, $bytes[, CHECK])
318
319Decode sequence of octets assumed to be in I<ENCODING> into Perl's
320internal form and returns the resulting string. For CHECK see
321L</"Handling Malformed Data">.
322
323For example to convert ISO-8859-1 data to UTF-8:
324
325 $utf8 = decode("latin1", $latin1);
326
327=item *
328
329 from_to($string, FROM_ENCODING, TO_ENCODING[, CHECK])
330
331Convert B<in-place> the data between two encodings. How did the data
332in $string originally get to be in FROM_ENCODING? Either using
333encode() or through PerlIO: See L</"Encoding and IO">. For CHECK
334see L</"Handling Malformed Data">.
335
336For example to convert ISO-8859-1 data to UTF-8:
337
338 from_to($data, "iso-8859-1", "utf-8");
339
340and to convert it back:
341
342 from_to($data, "utf-8", "iso-8859-1");
343
344Note that because the conversion happens in place, the data to be
345converted cannot be a string constant, it must be a scalar variable.
346
347=back
348
349=head2 Handling Malformed Data
350
351If CHECK is not set, C<undef> is returned. If the data is supposed to
352be UTF-8, an optional lexical warning (category utf8) is given. If
353CHECK is true but not a code reference, dies.
354
355It would desirable to have a way to indicate that transform should use
356the encodings "replacement character" - no such mechanism is defined yet.
357
358It is also planned to allow I<CHECK> to be a code reference.
359
360This is not yet implemented as there are design issues with what its
361arguments should be and how it returns its results.
362
363=over 4
364
365=item Scheme 1
366
367Passed remaining fragment of string being processed.
368Modifies it in place to remove bytes/characters it can understand
369and returns a string used to represent them.
370e.g.
371
372 sub fixup {
373 my $ch = substr($_[0],0,1,'');
374 return sprintf("\x{%02X}",ord($ch);
375 }
376
377This scheme is close to how underlying C code for Encode works, but gives
378the fixup routine very little context.
379
380=item Scheme 2
381
382Passed original string, and an index into it of the problem area, and
383output string so far. Appends what it will to output string and
384returns new index into original string. For example:
385
386 sub fixup {
387 # my ($s,$i,$d) = @_;
388 my $ch = substr($_[0],$_[1],1);
389 $_[2] .= sprintf("\x{%02X}",ord($ch);
390 return $_[1]+1;
391 }
392
393This scheme gives maximal control to the fixup routine but is more
394complicated to code, and may need internals of Encode to be tweaked to
395keep original string intact.
396
397=item Other Schemes
398
399Hybrids of above.
400
401Multiple return values rather than in-place modifications.
402
403Index into the string could be pos($str) allowing s/\G...//.
404
405=back
406
407=head2 UTF-8 / utf8
408
409The Unicode consortium defines the UTF-8 standard as a way of encoding
a63c962f 410the entire Unicode repertoire as sequences of octets. This encoding is
411expected to become very widespread. Perl can use this form internally
1b2c56c8 412to represent strings, so conversions to and from this form are
413particularly efficient (as octets in memory do not have to change,
414just the meta-data that tells Perl how to treat them).
415
416=over 4
417
a63c962f 418=item $bytes = encode_utf8($string);
1b2c56c8 419
420The characters that comprise string are encoded in Perl's superset of UTF-8
421and the resulting octets returned as a sequence of bytes. All possible
422characters have a UTF-8 representation so this function cannot fail.
423
a63c962f 424=item $string = decode_utf8($bytes [,CHECK]);
1b2c56c8 425
426The sequence of octets represented by $bytes is decoded from UTF-8
427into a sequence of logical characters. Not all sequences of octets
428form valid UTF-8 encodings, so it is possible for this call to fail.
429For CHECK see L</"Handling Malformed Data">.
430
431=back
432
433=head2 Other Encodings of Unicode
434
435UTF-16 is similar to UCS-2, 16 bit or 2-byte chunks. UCS-2 can only
436represent 0..0xFFFF, while UTF-16 has a I<surrogate pair> scheme which
437allows it to cover the whole Unicode range.
438
439Surrogates are code points set aside to encode the 0x01000..0x10FFFF
440range of Unicode code points in pairs of 16-bit units. The I<high
441surrogates> are the range 0xD800..0xDBFF, and the I<low surrogates>
442are the range 0xDC00..0xDFFFF. The surrogate encoding is
443
444 $hi = ($uni - 0x10000) / 0x400 + 0xD800;
445 $lo = ($uni - 0x10000) % 0x400 + 0xDC00;
446
447and the decoding is
448
449 $uni = 0x10000 + ($hi - 0xD8000) * 0x400 + ($lo - 0xDC00);
450
451Encode implements big-endian UCS-2 aliased to "iso-10646-1" as that
452happens to be the name used by that representation when used with X11
453fonts.
454
455UTF-32 or UCS-4 is 32-bit or 4-byte chunks. Perl's logical characters
456can be considered as being in this form without encoding. An encoding
457to transfer strings in this form (e.g. to write them to a file) would
458need to
459
460 pack('L*', unpack('U*', $string)); # native
461 or
462 pack('V*', unpack('U*', $string)); # little-endian
463 or
464 pack('N*', unpack('U*', $string)); # big-endian
465
466depending on the endianness required.
467
468No UTF-32 encodings are implemented yet.
469
470Both UCS-2 and UCS-4 style encodings can have "byte order marks" by
471representing the code point 0xFFFE as the very first thing in a file.
472
473=head2 Listing available encodings
474
475 use Encode qw(encodings);
476 @list = encodings();
477
478Returns a list of the canonical names of the available encodings.
479
480=head2 Defining Aliases
481
482 use Encode qw(define_alias);
483 define_alias( newName => ENCODING);
484
485Allows newName to be used as am alias for ENCODING. ENCODING may be
486either the name of an encoding or and encoding object (as above).
487
488Currently I<newName> can be specified in the following ways:
489
490=over 4
491
492=item As a simple string.
493
494=item As a qr// compiled regular expression, e.g.:
495
496 define_alias( qr/^iso8859-(\d+)$/i => '"iso-8859-$1"' );
497
498In this case if I<ENCODING> is not a reference it is C<eval>-ed to
a63c962f 499allow C<$1> etc. to be substituted. The example is one way to names as
1b2c56c8 500used in X11 font names to alias the MIME names for the iso-8859-*
501family. Note the double quote inside the single quote. If you are
a63c962f 502using regex here, you have to do so or it won't work in this case.
1b2c56c8 503
504=item As a code reference, e.g.:
505
506 define_alias( sub { return /^iso8859-(\d+)$/i ? "iso-8859-$1" : undef } , '');
507
508In this case C<$_> will be set to the name that is being looked up and
509I<ENCODING> is passed to the sub as its first argument. The example
510is another way to names as used in X11 font names to alias the MIME
511names for the iso-8859-* family.
512
513=back
514
515=head2 Defining Encodings
516
517 use Encode qw(define_alias);
518 define_encoding( $object, 'canonicalName' [,alias...]);
519
520Causes I<canonicalName> to be associated with I<$object>. The object
521should provide the interface described in L</"IMPLEMENTATION CLASSES">
522below. If more than two arguments are provided then additional
523arguments are taken as aliases for I<$object> as for C<define_alias>.
524
525=head1 Encoding and IO
526
527It is very common to want to do encoding transformations when
528reading or writing files, network connections, pipes etc.
529If Perl is configured to use the new 'perlio' IO system then
530C<Encode> provides a "layer" (See L<perliol>) which can transform
531data as it is read or written.
532
533Here is how the blind poet would modernise the encoding:
534
535 use Encode;
536 open(my $iliad,'<:encoding(iso-8859-7)','iliad.greek');
537 open(my $utf8,'>:utf8','iliad.utf8');
538 my @epic = <$iliad>;
539 print $utf8 @epic;
540 close($utf8);
541 close($illiad);
542
543In addition the new IO system can also be configured to read/write
544UTF-8 encoded characters (as noted above this is efficient):
545
546 open(my $fh,'>:utf8','anything');
547 print $fh "Any \x{0021} string \N{SMILEY FACE}\n";
548
549Either of the above forms of "layer" specifications can be made the default
550for a lexical scope with the C<use open ...> pragma. See L<open>.
551
552Once a handle is open is layers can be altered using C<binmode>.
553
554Without any such configuration, or if Perl itself is built using
555system's own IO, then write operations assume that file handle accepts
556only I<bytes> and will C<die> if a character larger than 255 is
557written to the handle. When reading, each octet from the handle
558becomes a byte-in-a-character. Note that this default is the same
559behaviour as bytes-only languages (including Perl before v5.6) would
560have, and is sufficient to handle native 8-bit encodings
561e.g. iso-8859-1, EBCDIC etc. and any legacy mechanisms for handling
562other encodings and binary data.
563
564In other cases it is the programs responsibility to transform
565characters into bytes using the API above before doing writes, and to
566transform the bytes read from a handle into characters before doing
567"character operations" (e.g. C<lc>, C</\W+/>, ...).
568
569You can also use PerlIO to convert larger amounts of data you don't
570want to bring into memory. For example to convert between ISO-8859-1
571(Latin 1) and UTF-8 (or UTF-EBCDIC in EBCDIC machines):
572
573 open(F, "<:encoding(iso-8859-1)", "data.txt") or die $!;
574 open(G, ">:utf8", "data.utf") or die $!;
575 while (<F>) { print G }
576
577 # Could also do "print G <F>" but that would pull
578 # the whole file into memory just to write it out again.
579
580More examples:
581
582 open(my $f, "<:encoding(cp1252)")
583 open(my $g, ">:encoding(iso-8859-2)")
584 open(my $h, ">:encoding(latin9)") # iso-8859-15
585
586See L<PerlIO> for more information.
587
588See also L<encoding> for how to change the default encoding of the
589data in your script.
590
591=head1 Encoding How to ...
592
593To do:
594
595=over 4
596
597=item * IO with mixed content (faking iso-2022-*)
598
599Encode::JP implements its own iso-2022 routines, however.
600
601=item * MIME's Content-Length:
602
603=item * UTF-8 strings in binary data.
604
605=item * Perl/Encode wrappers on non-Unicode XS modules.
606
607=back
608
609=head1 Messing with Perl's Internals
610
611The following API uses parts of Perl's internals in the current
612implementation. As such they are efficient, but may change.
613
614=over 4
615
a63c962f 616=item is_utf8(STRING [, CHECK])
1b2c56c8 617
618[INTERNAL] Test whether the UTF-8 flag is turned on in the STRING.
619If CHECK is true, also checks the data in STRING for being well-formed
620UTF-8. Returns true if successful, false otherwise.
621
a63c962f 622=item _utf8_on(STRING)
1b2c56c8 623
624[INTERNAL] Turn on the UTF-8 flag in STRING. The data in STRING is
625B<not> checked for being well-formed UTF-8. Do not use unless you
626B<know> that the STRING is well-formed UTF-8. Returns the previous
627state of the UTF-8 flag (so please don't test the return value as
628I<not> success or failure), or C<undef> if STRING is not a string.
629
a63c962f 630=item _utf8_off(STRING)
1b2c56c8 631
632[INTERNAL] Turn off the UTF-8 flag in STRING. Do not use frivolously.
633Returns the previous state of the UTF-8 flag (so please don't test the
634return value as I<not> success or failure), or C<undef> if STRING is
635not a string.
636
637=back
638
639=head1 IMPLEMENTATION CLASSES
640
641As mentioned above encodings are (in the current implementation at least)
642defined by objects. The mapping of encoding name to object is via the
643C<%encodings> hash.
644
645The values of the hash can currently be either strings or objects.
646The string form may go away in the future. The string form occurs
647when C<encodings()> has scanned C<@INC> for loadable encodings but has
648not actually loaded the encoding in question. This is because the
649current "loading" process is all Perl and a bit slow.
650
651Once an encoding is loaded then value of the hash is object which
652implements the encoding. The object should provide the following
653interface:
654
655=over 4
656
657=item -E<gt>name
658
659Should return the string representing the canonical name of the encoding.
660
661=item -E<gt>new_sequence
662
663This is a placeholder for encodings with state. It should return an
664object which implements this interface, all current implementations
665return the original object.
666
667=item -E<gt>encode($string,$check)
668
669Should return the octet sequence representing I<$string>. If I<$check>
670is true it should modify I<$string> in place to remove the converted
671part (i.e. the whole string unless there is an error). If an error
672occurs it should return the octet sequence for the fragment of string
673that has been converted, and modify $string in-place to remove the
674converted part leaving it starting with the problem fragment.
675
676If check is is false then C<encode> should make a "best effort" to
677convert the string - for example by using a replacement character.
678
679=item -E<gt>decode($octets,$check)
680
681Should return the string that I<$octets> represents. If I<$check> is
682true it should modify I<$octets> in place to remove the converted part
683(i.e. the whole sequence unless there is an error). If an error
684occurs it should return the fragment of string that has been
685converted, and modify $octets in-place to remove the converted part
686leaving it starting with the problem fragment.
687
688If check is is false then C<decode> should make a "best effort" to
689convert the string - for example by using Unicode's "\x{FFFD}" as a
690replacement character.
691
692=back
693
694It should be noted that the check behaviour is different from the
695outer public API. The logic is that the "unchecked" case is useful
696when encoding is part of a stream which may be reporting errors
697(e.g. STDERR). In such cases it is desirable to get everything
698through somehow without causing additional errors which obscure the
699original one. Also the encoding is best placed to know what the
700correct replacement character is, so if that is the desired behaviour
701then letting low level code do it is the most efficient.
702
703In contrast if check is true, the scheme above allows the encoding to
704do as much as it can and tell layer above how much that was. What is
705lacking at present is a mechanism to report what went wrong. The most
706likely interface will be an additional method call to the object, or
707perhaps (to avoid forcing per-stream objects on otherwise stateless
708encodings) and additional parameter.
709
710It is also highly desirable that encoding classes inherit from
711C<Encode::Encoding> as a base class. This allows that class to define
712additional behaviour for all encoding objects. For example built in
713Unicode, UCS-2 and UTF-8 classes use :
714
715 package Encode::MyEncoding;
716 use base qw(Encode::Encoding);
717
718 __PACKAGE__->Define(qw(myCanonical myAlias));
719
720To create an object with bless {Name => ...},$class, and call
721define_encoding. They inherit their C<name> method from
722C<Encode::Encoding>.
723
724=head2 Compiled Encodings
725
726F<Encode.xs> provides a class C<Encode::XS> which provides the
727interface described above. It calls a generic octet-sequence to
728octet-sequence "engine" that is driven by tables (defined in
729F<encengine.c>). The same engine is used for both encode and
730decode. C<Encode:XS>'s C<encode> forces Perl's characters to their
731UTF-8 form and then treats them as just another multibyte
732encoding. C<Encode:XS>'s C<decode> transforms the sequence and then
733turns the UTF-8-ness flag as that is the form that the tables are
734defined to produce. For details of the engine see the comments in
735F<encengine.c>.
736
737The tables are produced by the Perl script F<compile> (the name needs
738to change so we can eventually install it somewhere). F<compile> can
739currently read two formats:
740
741=over 4
742
743=item *.enc
744
745This is a coined format used by Tcl. It is documented in
746Encode/EncodeFormat.pod.
747
748=item *.ucm
749
750This is the semi-standard format used by IBM's ICU package.
751
752=back
753
754F<compile> can write the following forms:
755
756=over 4
757
758=item *.ucm
759
760See above - the F<Encode/*.ucm> files provided with the distribution have
761been created from the original Tcl .enc files using this approach.
762
763=item *.c
764
765Produces tables as C data structures - this is used to build in encodings
766into F<Encode.so>/F<Encode.dll>.
767
768=item *.xs
769
770In theory this allows encodings to be stand-alone loadable Perl
771extensions. The process has not yet been tested. The plan is to use
772this approach for large East Asian encodings.
773
774=back
775
776The set of encodings built-in to F<Encode.so>/F<Encode.dll> is
777determined by F<Makefile.PL>. The current set is as follows:
778
779=over 4
780
781=item ascii and iso-8859-*
782
783That is all the common 8-bit "western" encodings.
784
785=item IBM-1047 and two other variants of EBCDIC.
786
787These are the same variants that are supported by EBCDIC Perl as
788"native" encodings. They are included to prove "reversibility" of
789some constructs in EBCDIC Perl.
790
791=item symbol and dingbats as used by Tk on X11.
792
793(The reason Encode got started was to support Perl/Tk.)
794
795=back
796
797That set is rather ad hoc and has been driven by the needs of the
798tests rather than the needs of typical applications. It is likely
799to be rationalized.
800
801=head1 SEE ALSO
802
803L<perlunicode>, L<perlebcdic>, L<perlfunc/open>, L<PerlIO>, L<encoding>,
804L<utf8>, the Perl Unicode Mailing List E<lt>perl-unicode@perl.orgE<gt>
805
1b2c56c8 806=cut