1ENC2XS(1) Perl Programmers Reference Guide ENC2XS(1)
2
3
4
6 enc2xs -- Perl Encode Module Generator
7
9 enc2xs -[options]
10 enc2xs -M ModName mapfiles...
11 enc2xs -C
12
14 enc2xs builds a Perl extension for use by Encode from either Unicode
15 Character Mapping files (.ucm) or Tcl Encoding Files (.enc). Besides
16 being used internally during the build process of the Encode module,
17 you can use enc2xs to add your own encoding to perl. No knowledge of
18 XS is necessary.
19
21 If you want to know as little about Perl as possible but need to add a
22 new encoding, just read this chapter and forget the rest.
23
24 0. Have a .ucm file ready. You can get it from somewhere or you can
25 write your own from scratch or you can grab one from the Encode
26 distribution and customize it. For the UCM format, see the next
27 Chapter. In the example below, I'll call my theoretical encoding
28 myascii, defined in my.ucm. "$" is a shell prompt.
29
30 $ ls -F
31 my.ucm
32
33 1. Issue a command as follows;
34
35 $ enc2xs -M My my.ucm
36 generating Makefile.PL
37 generating My.pm
38 generating README
39 generating Changes
40
41 Now take a look at your current directory. It should look like
42 this.
43
44 $ ls -F
45 Makefile.PL My.pm my.ucm t/
46
47 The following files were created.
48
49 Makefile.PL - MakeMaker script
50 My.pm - Encode submodule
51 t/My.t - test file
52
53 1.1.
54 If you want *.ucm installed together with the modules, do as
55 follows;
56
57 $ mkdir Encode
58 $ mv *.ucm Encode
59 $ enc2xs -M My Encode/*ucm
60
61 2. Edit the files generated. You don't have to if you have no time
62 AND no intention to give it to someone else. But it is a good idea
63 to edit the pod and to add more tests.
64
65 3. Now issue a command all Perl Mongers love:
66
67 $ perl Makefile.PL
68 Writing Makefile for Encode::My
69
70 4. Now all you have to do is make.
71
72 $ make
73 cp My.pm blib/lib/Encode/My.pm
74 /usr/local/bin/perl /usr/local/bin/enc2xs -Q -O \
75 -o encode_t.c -f encode_t.fnm
76 Reading myascii (myascii)
77 Writing compiled form
78 128 bytes in string tables
79 384 bytes (75%) saved spotting duplicates
80 1 bytes (0.775%) saved using substrings
81 ....
82 chmod 644 blib/arch/auto/Encode/My/My.bs
83 $
84
85 The time it takes varies depending on how fast your machine is and
86 how large your encoding is. Unless you are working on something
87 big like euc-tw, it won't take too long.
88
89 5. You can "make install" already but you should test first.
90
91 $ make test
92 PERL_DL_NONLAZY=1 /usr/local/bin/perl -Iblib/arch -Iblib/lib \
93 -e 'use Test::Harness qw(&runtests $verbose); \
94 $verbose=0; runtests @ARGV;' t/*.t
95 t/My....ok
96 All tests successful.
97 Files=1, Tests=2, 0 wallclock secs
98 ( 0.09 cusr + 0.01 csys = 0.09 CPU)
99
100 6. If you are content with the test result, just "make install"
101
102 7. If you want to add your encoding to Encode's demand-loading list
103 (so you don't have to "use Encode::YourEncoding"), run
104
105 enc2xs -C
106
107 to update Encode::ConfigLocal, a module that controls local
108 settings. After that, "use Encode;" is enough to load your
109 encodings on demand.
110
112 Encode uses the Unicode Character Map (UCM) format for source character
113 mappings. This format is used by IBM's ICU package and was adopted by
114 Nick Ing-Simmons for use with the Encode module. Since UCM is more
115 flexible than Tcl's Encoding Map and far more user-friendly, this is
116 the recommended format for Encode now.
117
118 A UCM file looks like this.
119
120 #
121 # Comments
122 #
123 <code_set_name> "US-ascii" # Required
124 <code_set_alias> "ascii" # Optional
125 <mb_cur_min> 1 # Required; usually 1
126 <mb_cur_max> 1 # Max. # of bytes/char
127 <subchar> \x3F # Substitution char
128 #
129 CHARMAP
130 <U0000> \x00 |0 # <control>
131 <U0001> \x01 |0 # <control>
132 <U0002> \x02 |0 # <control>
133 ....
134 <U007C> \x7C |0 # VERTICAL LINE
135 <U007D> \x7D |0 # RIGHT CURLY BRACKET
136 <U007E> \x7E |0 # TILDE
137 <U007F> \x7F |0 # <control>
138 END CHARMAP
139
140 · Anything that follows "#" is treated as a comment.
141
142 · The header section continues until a line containing the word
143 CHARMAP. This section has a form of <keyword> value, one pair per
144 line. Strings used as values must be quoted. Barewords are treated
145 as numbers. \xXX represents a byte.
146
147 Most of the keywords are self-explanatory. subchar means
148 substitution character, not subcharacter. When you decode a
149 Unicode sequence to this encoding but no matching character is
150 found, the byte sequence defined here will be used. For most
151 cases, the value here is \x3F; in ASCII, this is a question mark.
152
153 · CHARMAP starts the character map section. Each line has a form as
154 follows:
155
156 <UXXXX> \xXX.. |0 # comment
157 ^ ^ ^
158 | | +- Fallback flag
159 | +-------- Encoded byte sequence
160 +-------------- Unicode Character ID in hex
161
162 The format is roughly the same as a header section except for the
163 fallback flag: | followed by 0..3. The meaning of the possible
164 values is as follows:
165
166 |0 Round trip safe. A character decoded to Unicode encodes back
167 to the same byte sequence. Most characters have this flag.
168
169 |1 Fallback for unicode -> encoding. When seen, enc2xs adds this
170 character for the encode map only.
171
172 |2 Skip sub-char mapping should there be no code point.
173
174 |3 Fallback for encoding -> unicode. When seen, enc2xs adds this
175 character for the decode map only.
176
177 · And finally, END OF CHARMAP ends the section.
178
179 When you are manually creating a UCM file, you should copy ascii.ucm or
180 an existing encoding which is close to yours, rather than write your
181 own from scratch.
182
183 When you do so, make sure you leave at least U0000 to U0020 as is,
184 unless your environment is EBCDIC.
185
186 CAVEAT: not all features in UCM are implemented. For example,
187 icu:state is not used. Because of that, you need to write a perl
188 module if you want to support algorithmical encodings, notably the
189 ISO-2022 series. Such modules include Encode::JP::2022_JP,
190 Encode::KR::2022_KR, and Encode::TW::HZ.
191
192 Coping with duplicate mappings
193 When you create a map, you SHOULD make your mappings round-trip safe.
194 That is, "encode('your-encoding', decode('your-encoding', $data)) eq
195 $data" stands for all characters that are marked as "|0". Here is how
196 to make sure:
197
198 · Sort your map in Unicode order.
199
200 · When you have a duplicate entry, mark either one with '|1' or '|3'.
201
202 · And make sure the '|1' or '|3' entry FOLLOWS the '|0' entry.
203
204 Here is an example from big5-eten.
205
206 <U2550> \xF9\xF9 |0
207 <U2550> \xA2\xA4 |3
208
209 Internally Encoding -> Unicode and Unicode -> Encoding Map looks like
210 this;
211
212 E to U U to E
213 --------------------------------------
214 \xF9\xF9 => U2550 U2550 => \xF9\xF9
215 \xA2\xA4 => U2550
216
217 So it is round-trip safe for \xF9\xF9. But if the line above is upside
218 down, here is what happens.
219
220 E to U U to E
221 --------------------------------------
222 \xA2\xA4 => U2550 U2550 => \xF9\xF9
223 (\xF9\xF9 => U2550 is now overwritten!)
224
225 The Encode package comes with ucmlint, a crude but sufficient utility
226 to check the integrity of a UCM file. Check under the Encode/bin
227 directory for this.
228
229 When in doubt, you can use ucmsort, yet another utility under
230 Encode/bin directory.
231
233 · ICU Home Page <http://www.icu-project.org/>
234
235 · ICU Character Mapping Tables <http://www.icu-project.org/charset/>
236
237 · ICU:Conversion Data
238 <http://www.icu-project.org/userguide/conversion-data.html>
239
241 Encode, perlmod, perlpod
242
243
244
245perl v5.10.1 2017-03-22 ENC2XS(1)