Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/2perl/Encode

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  | will give a
.\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
.\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Encode 3"
.TH Encode 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
Encode \- character encodings
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\&    use Encode;
.Ve
.Sh "Table of Contents"
.IX Subsection "Table of Contents"
Encode consists of a collection of modules whose details are too big
to fit in one document.  This \s-1POD\s0 itself explains the top-level APIs
and general topics at a glance.  For other topics and more details,
see the PODs below:
.PP
.Vb 10
\&  Name                          Description
\&  --------------------------------------------------------
\&  Encode::Alias         Alias definitions to encodings
\&  Encode::Encoding      Encode Implementation Base Class
\&  Encode::Supported     List of Supported Encodings
\&  Encode::CN            Simplified Chinese Encodings
\&  Encode::JP            Japanese Encodings
\&  Encode::KR            Korean Encodings
\&  Encode::TW            Traditional Chinese Encodings
\&  --------------------------------------------------------
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
The \f(CW\*(C`Encode\*(C'\fR module provides the interfaces between Perl's strings
and the rest of the system.  Perl strings are sequences of
\&\fBcharacters\fR.
.PP
The repertoire of characters that Perl can represent is at least that
defined by the Unicode Consortium. On most platforms the ordinal
values of the characters (as returned by \f(CW\*(C`ord(ch)\*(C'\fR) is the \*(L"Unicode
codepoint\*(R" for the character (the exceptions are those platforms where
the legacy encoding is some variant of \s-1EBCDIC\s0 rather than a super-set
of \s-1ASCII\s0 \- see perlebcdic).
.PP
Traditionally, computer data has been moved around in 8\-bit chunks
often called \*(L"bytes\*(R". These chunks are also known as \*(L"octets\*(R" in
networking standards. Perl is widely used to manipulate data of many
types \- not only strings of characters representing human or computer
languages but also \*(L"binary\*(R" data being the machine's representation of
numbers, pixels in an image \- or just about anything.
.PP
When Perl is processing \*(L"binary data\*(R", the programmer wants Perl to
process \*(L"sequences of bytes\*(R". This is not a problem for Perl \- as a
byte has 256 possible values, it easily fits in Perl's much larger
\&\*(L"logical character\*(R".
.Sh "\s-1TERMINOLOGY\s0"
.IX Subsection "TERMINOLOGY"
.IP "\(bu" 2
\&\fIcharacter\fR: a character in the range 0..(2**32\-1) (or more).
(What Perl's strings are made of.)
.IP "\(bu" 2
\&\fIbyte\fR: a character in the range 0..255
(A special case of a Perl character.)
.IP "\(bu" 2
\&\fIoctet\fR: 8 bits of data, with ordinal values 0..255
(Term for bytes passed to or from a non-Perl context, e.g. a disk file.)
.SH "PERL ENCODING API"
.IX Header "PERL ENCODING API"
.ie n .IP "$octets  = encode(\s-1ENCODING\s0, $string [, \s-1CHECK\s0])" 2
.el .IP "$octets  = encode(\s-1ENCODING\s0, \f(CW$string\fR [, \s-1CHECK\s0])" 2
.IX Item "$octets  = encode(ENCODING, $string [, CHECK])"
Encodes a string from Perl's internal form into \fI\s-1ENCODING\s0\fR and returns
a sequence of octets.  \s-1ENCODING\s0 can be either a canonical name or
an alias.  For encoding names and aliases, see \*(L"Defining Aliases\*(R".
For \s-1CHECK\s0, see \*(L"Handling Malformed Data\*(R".
.Sp
For example, to convert a string from Perl's internal format to
iso\-8859\-1 (also known as Latin1),
.Sp
.Vb 1
\&  $octets = encode("iso-8859-1", $string);
.Ve
.Sp
\&\fB\s-1CAVEAT\s0\fR: When you run \f(CW\*(C`$octets = encode("utf8", $string)\*(C'\fR, then \f(CW$octets\fR
\&\fBmay not be equal to\fR \f(CW$string\fR.  Though they both contain the same data, the utf8 flag
for \f(CW$octets\fR is \fBalways\fR off.  When you encode anything, utf8 flag of
the result is always off, even when it contains completely valid utf8
string. See \*(L"The \s-1UTF\-8\s0 flag\*(R" below.
.Sp
encode($valid_encoding, undef) is harmless but warns you for 
\&\f(CW\*(C`Use of uninitialized value in subroutine entry\*(C'\fR. 
encode($valid_encoding, '') is harmless and warnless.
.ie n .IP "$string = decode(\s-1ENCODING\s0, $octets [, \s-1CHECK\s0])" 2
.el .IP "$string = decode(\s-1ENCODING\s0, \f(CW$octets\fR [, \s-1CHECK\s0])" 2
.IX Item "$string = decode(ENCODING, $octets [, CHECK])"
Decodes a sequence of octets assumed to be in \fI\s-1ENCODING\s0\fR into Perl's
internal form and returns the resulting string.  As in \fIencode()\fR,
\&\s-1ENCODING\s0 can be either a canonical name or an alias. For encoding names
and aliases, see \*(L"Defining Aliases\*(R".  For \s-1CHECK\s0, see
\&\*(L"Handling Malformed Data\*(R".
.Sp
For example, to convert \s-1ISO\-8859\-1\s0 data to a string in Perl's internal format:
.Sp
.Vb 1
\&  $string = decode("iso-8859-1", $octets);
.Ve
.Sp
\&\fB\s-1CAVEAT\s0\fR: When you run \f(CW\*(C`$string = decode("utf8", $octets)\*(C'\fR, then \f(CW$string\fR
\&\fBmay not be equal to\fR \f(CW$octets\fR.  Though they both contain the same data,
the utf8 flag for \f(CW$string\fR is on unless \f(CW$octets\fR entirely consists of
\&\s-1ASCII\s0 data (or \s-1EBCDIC\s0 on \s-1EBCDIC\s0 machines).  See \*(L"The \s-1UTF\-8\s0 flag\*(R"
below.
.Sp
decode($valid_encoding, undef) is harmless but warns you for 
\&\f(CW\*(C`Use of uninitialized value in subroutine entry\*(C'\fR. 
decode($valid_encoding, '') is harmless and warnless.
.IP "[$length =] from_to($octets, \s-1FROM_ENC\s0, \s-1TO_ENC\s0 [, \s-1CHECK\s0])" 2
.IX Item "[$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])"
Converts \fBin-place\fR data between two encodings. The data in \f(CW$octets\fR
must be encoded as octets and not as characters in Perl's internal
format. For example, to convert \s-1ISO\-8859\-1\s0 data to Microsoft's \s-1CP1250\s0 encoding:
.Sp
.Vb 1
\&  from_to($octets, "iso-8859-1", "cp1250");
.Ve
.Sp
and to convert it back:
.Sp
.Vb 1
\&  from_to($octets, "cp1250", "iso-8859-1");
.Ve
.Sp
Note that because the conversion happens in place, the data to be
converted cannot be a string constant; it must be a scalar variable.
.Sp
\&\fIfrom_to()\fR returns the length of the converted string in octets on success, undef
otherwise.
.Sp
\&\fB\s-1CAVEAT\s0\fR: The following operations look the same but are not quite so;
.Sp
.Vb 2
\&  from_to($data, "iso-8859-1", "utf8"); #1
\&  $data = decode("iso-8859-1", $data);  #2
.Ve
.Sp
Both #1 and #2 make \f(CW$data\fR consist of a completely valid \s-1UTF\-8\s0 string
but only #2 turns utf8 flag on.  #1 is equivalent to
.Sp
.Vb 1
\&  $data = encode("utf8", decode("iso-8859-1", $data));
.Ve
.Sp
See \*(L"The \s-1UTF\-8\s0 flag\*(R" below.
.IP "$octets = encode_utf8($string);" 2
.IX Item "$octets = encode_utf8($string);"
Equivalent to \f(CW\*(C`$octets = encode("utf8", $string);\*(C'\fR The characters
that comprise \f(CW$string\fR are encoded in Perl's internal format and the
result is returned as a sequence of octets. All possible
characters have a \s-1UTF\-8\s0 representation so this function cannot fail.
.IP "$string = decode_utf8($octets [, \s-1CHECK\s0]);" 2
.IX Item "$string = decode_utf8($octets [, CHECK]);"
equivalent to \f(CW\*(C`$string = decode("utf8", $octets [, CHECK])\*(C'\fR.
The sequence of octets represented by
\&\f(CW$octets\fR is decoded from \s-1UTF\-8\s0 into a sequence of logical
characters. Not all sequences of octets form valid \s-1UTF\-8\s0 encodings, so
it is possible for this call to fail.  For \s-1CHECK\s0, see
\&\*(L"Handling Malformed Data\*(R".
.Sh "Listing available encodings"
.IX Subsection "Listing available encodings"
.Vb 2
\&  use Encode;
\&  @list = Encode->encodings();
.Ve
.PP
Returns a list of the canonical names of the available encodings that
are loaded.  To get a list of all available encodings including the
ones that are not loaded yet, say
.PP
.Vb 1
\&  @all_encodings = Encode->encodings(":all");
.Ve
.PP
Or you can give the name of a specific module.
.PP
.Vb 1
\&  @with_jp = Encode->encodings("Encode::JP");
.Ve
.PP
When \*(L"::\*(R" is not in the name, \*(L"Encode::\*(R" is assumed.
.PP
.Vb 1
\&  @ebcdic = Encode->encodings("EBCDIC");
.Ve
.PP
To find out in detail which encodings are supported by this package,
see Encode::Supported.
.Sh "Defining Aliases"
.IX Subsection "Defining Aliases"
To add a new alias to a given encoding, use:
.PP
.Vb 3
\&  use Encode;
\&  use Encode::Alias;
\&  define_alias(newName => ENCODING);
.Ve
.PP
After that, newName can be used as an alias for \s-1ENCODING\s0.
\&\s-1ENCODING\s0 may be either the name of an encoding or an
\&\fIencoding object\fR
.PP
But before you do so, make sure the alias is nonexistent with
\&\f(CW\*(C`resolve_alias()\*(C'\fR, which returns the canonical name thereof.
i.e.
.PP
.Vb 3
\&  Encode::resolve_alias("latin1") eq "iso-8859-1" # true
\&  Encode::resolve_alias("iso-8859-12")   # false; nonexistent
\&  Encode::resolve_alias($name) eq $name  # true if $name is canonical
.Ve
.PP
\&\fIresolve_alias()\fR does not need \f(CW\*(C`use Encode::Alias\*(C'\fR; it can be
exported via \f(CW\*(C`use Encode qw(resolve_alias)\*(C'\fR.
.PP
See Encode::Alias for details.
.SH "Encoding via PerlIO"
.IX Header "Encoding via PerlIO"
If your perl supports \fIPerlIO\fR (which is the default), you can use a PerlIO layer to decode
and encode directly via a filehandle.  The following two examples
are totally identical in their functionality.
.PP
.Vb 4
\&  # via PerlIO
\&  open my $in,  "<:encoding(shiftjis)", $infile  or die;
\&  open my $out, ">:encoding(euc-jp)",   $outfile or die;
\&  while(<$in>){ print $out $_; }
.Ve
.PP
.Vb 7
\&  # via from_to
\&  open my $in,  "<", $infile  or die;
\&  open my $out, ">", $outfile or die;
\&  while(<$in>){
\&    from_to($_, "shiftjis", "euc-jp", 1);
\&    print $out $_;
\&  }
.Ve
.PP
Unfortunately, it may be that encodings are PerlIO\-savvy.  You can check
if your encoding is supported by PerlIO by calling the \f(CW\*(C`perlio_ok\*(C'\fR
method.
.PP
.Vb 2
\&  Encode::perlio_ok("hz");             # False
\&  find_encoding("euc-cn")->perlio_ok;  # True where PerlIO is available
.Ve
.PP
.Vb 2
\&  use Encode qw(perlio_ok);            # exported upon request
\&  perlio_ok("euc-jp")
.Ve
.PP
Fortunately, all encodings that come with Encode core are PerlIO-savvy
except for hz and ISO\-2022\-kr.  For gory details, see Encode::Encoding and Encode::PerlIO.
.SH "Handling Malformed Data"
.IX Header "Handling Malformed Data"
.RS 2
The \fI\s-1CHECK\s0\fR argument is used as follows.  When you omit it,
the behaviour is the same as if you had passed a value of 0 for
\&\fI\s-1CHECK\s0\fR.
.RE
.IP "\fI\s-1CHECK\s0\fR = Encode::FB_DEFAULT ( == 0)" 2
.IX Item "CHECK = Encode::FB_DEFAULT ( == 0)"
If \fI\s-1CHECK\s0\fR is 0, (en|de)code will put a \fIsubstitution character\fR
in place of a malformed character.  For UCM-based encodings,
<subchar> will be used.  For Unicode, the code point \f(CW0xFFFD\fR is used.
If the data is supposed to be \s-1UTF\-8\s0, an optional lexical warning
(category utf8) is given.
.IP "\fI\s-1CHECK\s0\fR = Encode::FB_CROAK ( == 1)" 2
.IX Item "CHECK = Encode::FB_CROAK ( == 1)"
If \fI\s-1CHECK\s0\fR is 1, methods will die on error immediately with an error
message.  Therefore, when \fI\s-1CHECK\s0\fR is set to 1,  you should trap the
fatal error with eval{} unless you really want to let it die on error.
.IP "\fI\s-1CHECK\s0\fR = Encode::FB_QUIET" 2
.IX Item "CHECK = Encode::FB_QUIET"
If \fI\s-1CHECK\s0\fR is set to Encode::FB_QUIET, (en|de)code will immediately
return the portion of the data that has been processed so far when
an error occurs. The data argument will be overwritten with
everything after that point (that is, the unprocessed part of data).
This is handy when you have to call decode repeatedly in the case
where your source data may contain partial multi-byte character
sequences, for example because you are reading with a fixed-width
buffer. Here is some sample code that does exactly this:
.Sp
.Vb 7
\&  my $data = ''; my $utf8 = '';
\&  while(defined(read $fh, $buffer, 256)){
\&    # buffer may end in a partial character so we append
\&    $data .= $buffer;
\&    $utf8 .= decode($encoding, $data, Encode::FB_QUIET);
\&    # $data now contains the unprocessed partial character
\&  }
.Ve
.IP "\fI\s-1CHECK\s0\fR = Encode::FB_WARN" 2
.IX Item "CHECK = Encode::FB_WARN"
This is the same as above, except that it warns on error.  Handy when
you are debugging the mode above.
.IP "perlqq mode (\fI\s-1CHECK\s0\fR = Encode::FB_PERLQQ)" 2
.IX Item "perlqq mode (CHECK = Encode::FB_PERLQQ)"
.PD 0
.IP "\s-1HTML\s0 charref mode (\fI\s-1CHECK\s0\fR = Encode::FB_HTMLCREF)" 2
.IX Item "HTML charref mode (CHECK = Encode::FB_HTMLCREF)"
.IP "\s-1XML\s0 charref mode (\fI\s-1CHECK\s0\fR = Encode::FB_XMLCREF)" 2
.IX Item "XML charref mode (CHECK = Encode::FB_XMLCREF)"
.PD
For encodings that are implemented by Encode::XS, \s-1CHECK\s0 ==
Encode::FB_PERLQQ turns (en|de)code into \f(CW\*(C`perlqq\*(C'\fR fallback mode.
.Sp
When you decode, \f(CW\*(C`\ex\f(CI\s-1HH\s0\f(CW\*(C'\fR will be inserted for a malformed character,
where \fI\s-1HH\s0\fR is the hex representation of the octet  that could not be
decoded to utf8.  And when you encode, \f(CW\*(C`\ex{\f(CI\s-1HHHH\s0\f(CW}\*(C'\fR will be inserted,
where \fI\s-1HHHH\s0\fR is the Unicode \s-1ID\s0 of the character that cannot be found
in the character repertoire of the encoding.
.Sp
\&\s-1HTML/XML\s0 character reference modes are about the same, in place of
\&\f(CW\*(C`\ex{\f(CI\s-1HHHH\s0\f(CW}\*(C'\fR, \s-1HTML\s0 uses \f(CW\*(C`&#\f(CI\s-1NNNN\s0\f(CW\*(C'\fR; where \fI\s-1NNNN\s0\fR is a decimal digit and
\&\s-1XML\s0 uses \f(CW\*(C`&#x\f(CI\s-1HHHH\s0\f(CW\*(C'\fR; where \fI\s-1HHHH\s0\fR is the hexadecimal digit.
.IP "The bitmask" 2
.IX Item "The bitmask"
These modes are actually set via a bitmask.  Here is how the \s-1FB_XX\s0
constants are laid out.  You can import the \s-1FB_XX\s0 constants via
\&\f(CW\*(C`use Encode qw(:fallbacks)\*(C'\fR; you can import the generic bitmask
constants via \f(CW\*(C`use Encode qw(:fallback_all)\*(C'\fR.
.Sp
.Vb 8
\&                     FB_DEFAULT FB_CROAK FB_QUIET FB_WARN  FB_PERLQQ
\& DIE_ON_ERR    0x0001             X
\& WARN_ON_ERR   0x0002                               X
\& RETURN_ON_ERR 0x0004                      X        X
\& LEAVE_SRC     0x0008
\& PERLQQ        0x0100                                        X
\& HTMLCREF      0x0200
\& XMLCREF       0x0400
.Ve
.Sh "Unimplemented fallback schemes"
.IX Subsection "Unimplemented fallback schemes"
In the future, you will be able to use a code reference to a callback
function for the value of \fI\s-1CHECK\s0\fR but its \s-1API\s0 is still undecided.
.Sp
The fallback scheme does not work on \s-1EBCDIC\s0 platforms.
.SH "Defining Encodings"
.IX Header "Defining Encodings"
To define a new encoding, use:
.Sp
.Vb 2
\&    use Encode qw(define_encoding);
\&    define_encoding($object, 'canonicalName' [, alias...]);
.Ve
.Sp
\&\fIcanonicalName\fR will be associated with \fI$object\fR.  The object
should provide the interface described in Encode::Encoding.
If more than two arguments are provided then additional
arguments are taken as aliases for \fI$object\fR.
.Sp
See Encode::Encoding for more details.
.SH "The UTF\-8 flag"
.IX Header "The UTF-8 flag"
Before the introduction of utf8 support in perl, The \f(CW\*(C`eq\*(C'\fR operator
just compared the strings represented by two scalars. Beginning with
perl 5.8, \f(CW\*(C`eq\*(C'\fR compares two strings with simultaneous consideration
of \fIthe utf8 flag\fR. To explain why we made it so, I will quote page
402 of \f(CW\*(C`Programming Perl, 3rd ed.\*(C'\fR
.RS 2
.IP "Goal #1:" 2
.IX Item "Goal #1:"
Old byte-oriented programs should not spontaneously break on the old
byte-oriented data they used to work on.
.IP "Goal #2:" 2
.IX Item "Goal #2:"
Old byte-oriented programs should magically start working on the new
character-oriented data when appropriate.
.IP "Goal #3:" 2
.IX Item "Goal #3:"
Programs should run just as fast in the new character-oriented mode
as in the old byte-oriented mode.
.IP "Goal #4:" 2
.IX Item "Goal #4:"
Perl should remain one language, rather than forking into a
byte-oriented Perl and a character-oriented Perl.
.RE
.RS 2
.Sp
Back when \f(CW\*(C`Programming Perl, 3rd ed.\*(C'\fR was written, not even Perl 5.6.0
was born and many features documented in the book remained
unimplemented for a long time.  Perl 5.8 corrected this and the introduction
of the \s-1UTF\-8\s0 flag is one of them.  You can think of this perl notion as of a
byte-oriented mode (utf8 flag off) and a character-oriented mode (utf8
flag on).
.Sp
Here is how Encode takes care of the utf8 flag.
.IP "\(bu" 2
When you encode, the resulting utf8 flag is always off.
.IP "\(bu" 2
When you decode, the resulting utf8 flag is on unless you can
unambiguously represent data.  Here is the definition of
dis\-ambiguity.
.Sp
After \f(CW\*(C`$utf8 = decode('foo', $octet);\*(C'\fR,
.Sp
.Vb 6
\&  When $octet is...   The utf8 flag in $utf8 is
\&  ---------------------------------------------
\&  In ASCII only (or EBCDIC only)            OFF
\&  In ISO-8859-1                              ON
\&  In any other Encoding                      ON
\&  ---------------------------------------------
.Ve
.Sp
As you see, there is one exception, In \s-1ASCII\s0.  That way you can assue
Goal #1.  And with Encode Goal #2 is assumed but you still have to be
careful in such cases mentioned in \fB\s-1CAVEAT\s0\fR paragraphs.
.Sp
This utf8 flag is not visible in perl scripts, exactly for the same
reason you cannot (or you \fIdon't have to\fR) see if a scalar contains a
string, integer, or floating point number.   But you can still peek
and poke these if you will.  See the section below.
.RE
.RS 2
.Sh "Messing with Perl's Internals"
.IX Subsection "Messing with Perl's Internals"
The following \s-1API\s0 uses parts of Perl's internals in the current
implementation.  As such, they are efficient but may change.
.IP "is_utf8(\s-1STRING\s0 [, \s-1CHECK\s0])" 2
.IX Item "is_utf8(STRING [, CHECK])"
[\s-1INTERNAL\s0] Tests whether the \s-1UTF\-8\s0 flag is turned on in the \s-1STRING\s0.
If \s-1CHECK\s0 is true, also checks the data in \s-1STRING\s0 for being well-formed
\&\s-1UTF\-8\s0.  Returns true if successful, false otherwise.
.IP "_utf8_on(\s-1STRING\s0)" 2
.IX Item "_utf8_on(STRING)"
[\s-1INTERNAL\s0] Turns on the \s-1UTF\-8\s0 flag in \s-1STRING\s0.  The data in \s-1STRING\s0 is
\&\fBnot\fR checked for being well-formed \s-1UTF\-8\s0.  Do not use unless you
\&\fBknow\fR that the \s-1STRING\s0 is well-formed \s-1UTF\-8\s0.  Returns the previous
state of the \s-1UTF\-8\s0 flag (so please don't treat the return value as
indicating success or failure), or \f(CW\*(C`undef\*(C'\fR if \s-1STRING\s0 is not a string.
.IP "_utf8_off(\s-1STRING\s0)" 2
.IX Item "_utf8_off(STRING)"
[\s-1INTERNAL\s0] Turns off the \s-1UTF\-8\s0 flag in \s-1STRING\s0.  Do not use frivolously.
Returns the previous state of the \s-1UTF\-8\s0 flag (so please don't treat the
return value as indicating success or failure), or \f(CW\*(C`undef\*(C'\fR if \s-1STRING\s0 is
not a string.
.RE
.RS 2
.SH "SEE ALSO"
.IX Header "SEE ALSO"
Encode::Encoding,
Encode::Supported,
Encode::PerlIO,
encoding,
perlebcdic,
\&\*(L"open\*(R" in perlfunc,
perlunicode,
utf8,
the Perl Unicode Mailing List <perl\-unicode@perl.org>
.SH "MAINTAINER"
.IX Header "MAINTAINER"
This project was originated by Nick Ing-Simmons and later maintained
by Dan Kogai <dankogai@dan.co.jp>.  See \s-1AUTHORS\s0 for a full
list of people involved.  For any questions, use
<perl\-unicode@perl.org> so we can all share.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.