Perl 5 version 28.2
documentation

enc2xs

NAME

enc2xs -- Perl Encode Module Generator

SYNOPSIS

enc2xs -[options]

enc2xs -M ModNamemapfiles...

enc2xs -C

DESCRIPTION

enc2xs builds a Perl extension for use by Encode from either
Unicode Character Mapping files (.ucm) or Tcl Encoding Files (.enc).
Besides being used internally during the build process of the Encode
module, you can use enc2xs to add your own encoding to perl.
No knowledge of XS is necessary.

Quick Guide

If you want to know as little about Perl as possible but need to
add a new encoding, just read this chapter and forget the rest.

Have a .ucm file ready. You can get it from somewhere or you can write
your own from scratch or you can grab one from the Encode distribution
and customize it. For the UCM format, see the next Chapter. In the
example below, I'll call my theoretical encoding myascii, defined
in my.ucm. $
is a shell prompt.

If you want to add your encoding to Encode's demand-loading list
(so you don't have to "use Encode::YourEncoding"), run

enc2xs -C

to update Encode::ConfigLocal, a module that controls local settings.
After that, "use Encode;" is enough to load your encodings on demand.

The Unicode Character Map

Encode uses the Unicode Character Map (UCM) format for source character
mappings. This format is used by IBM's ICU package and was adopted
by Nick Ing-Simmons for use with the Encode module. Since UCM is
more flexible than Tcl's Encoding Map and far more user-friendly,
this is the recommended format for Encode now.

The header section continues until a line containing the word
CHARMAP. This section has a form of <keyword> value, one
pair per line. Strings used as values must be quoted. Barewords are
treated as numbers. \xXX represents a byte.

Most of the keywords are self-explanatory. subchar means
substitution character, not subcharacter. When you decode a Unicode
sequence to this encoding but no matching character is found, the byte
sequence defined here will be used. For most cases, the value here is
\x3F; in ASCII, this is a question mark.

CHARMAP starts the character map section. Each line has a form as
follows:

The format is roughly the same as a header section except for the
fallback flag: | followed by 0..3. The meaning of the possible
values is as follows:

|0

Round trip safe. A character decoded to Unicode encodes back to the
same byte sequence. Most characters have this flag.

|1

Fallback for unicode -> encoding. When seen, enc2xs adds this
character for the encode map only.

|2

Skip sub-char mapping should there be no code point.

|3

Fallback for encoding -> unicode. When seen, enc2xs adds this
character for the decode map only.

And finally, END OF CHARMAP ends the section.

When you are manually creating a UCM file, you should copy ascii.ucm
or an existing encoding which is close to yours, rather than write
your own from scratch.

When you do so, make sure you leave at least U0000 to U0020 as
is, unless your environment is EBCDIC.

CAVEAT: not all features in UCM are implemented. For example,
icu:state is not used. Because of that, you need to write a perl
module if you want to support algorithmical encodings, notably
the ISO-2022 series. Such modules include Encode::JP::2022_JP,
Encode::KR::2022_KR, and Encode::TW::HZ.

Coping with duplicate mappings

When you create a map, you SHOULD make your mappings round-trip safe.
That is, encode('your-encoding',decode('your-encoding',$data))eq$data
stands for all characters that are marked as |0
. Here is
how to make sure: