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

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 "encoding 3"
.TH encoding 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
encoding \- allows you to write your script in non\-ascii or non\-utf8
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 2
\&  use encoding "greek";  # Perl like Greek to you?
\&  use encoding "euc-jp"; # Jperl!
.Ve
.PP
.Vb 1
\&  # or you can even do this if your shell supports your native encoding
.Ve
.PP
.Vb 2
\&  perl -Mencoding=latin2 -e '...' # Feeling centrally European?
\&  perl -Mencoding=euc-kr -e '...' # Or Korean?
.Ve
.PP
.Vb 1
\&  # more control
.Ve
.PP
.Vb 2
\&  # A simple euc-cn => utf-8 converter
\&  use encoding "euc-cn", STDOUT => "utf8";  while(<>){print};
.Ve
.PP
.Vb 2
\&  # "no encoding;" supported (but not scoped!)
\&  no encoding;
.Ve
.PP
.Vb 4
\&  # an alternate way, Filter
\&  use encoding "euc-jp", Filter=>1;
\&  use utf8;
\&  # now you can use kanji identifiers -- in euc-jp!
.Ve
.SH "ABSTRACT"
.IX Header "ABSTRACT"
Let's start with a bit of history: Perl 5.6.0 introduced Unicode
support.  You could apply \f(CW\*(C`substr()\*(C'\fR and regexes even to complex \s-1CJK\s0
characters \*(-- so long as the script was written in \s-1UTF\-8\s0.  But back
then, text editors that supported \s-1UTF\-8\s0 were still rare and many users
instead chose to write scripts in legacy encodings, giving up a whole
new feature of Perl 5.6.
.PP
Rewind to the future: starting from perl 5.8.0 with the \fBencoding\fR
pragma, you can write your script in any encoding you like (so long
as the \f(CW\*(C`Encode\*(C'\fR module supports it) and still enjoy Unicode support.
You can write code in EUC-JP as follows:
.PP
.Vb 3
\&  my $Rakuda = "\exF1\exD1\exF1\exCC"; # Camel in Kanji
\&               #<-char-><-char->   # 4 octets
\&  s/\ebCamel\eb/$Rakuda/;
.Ve
.PP
And with \f(CW\*(C`use encoding "euc\-jp"\*(C'\fR in effect, it is the same thing as
the code in \s-1UTF\-8:\s0
.PP
.Vb 2
\&  my $Rakuda = "\ex{99F1}\ex{99DD}"; # two Unicode Characters
\&  s/\ebCamel\eb/$Rakuda/;
.Ve
.PP
The \fBencoding\fR pragma also modifies the filehandle disciplines of
\&\s-1STDIN\s0, \s-1STDOUT\s0, and \s-1STDERR\s0 to the specified encoding.  Therefore,
.PP
.Vb 5
\&  use encoding "euc-jp";
\&  my $message = "Camel is the symbol of perl.\en";
\&  my $Rakuda = "\exF1\exD1\exF1\exCC"; # Camel in Kanji
\&  $message =~ s/\ebCamel\eb/$Rakuda/;
\&  print $message;
.Ve
.PP
Will print \*(L"\exF1\exD1\exF1\exCC is the symbol of perl.\en\*(R",
not \*(L"\ex{99F1}\ex{99DD} is the symbol of perl.\en\*(R".
.PP
You can override this by giving extra arguments; see below.
.SH "USAGE"
.IX Header "USAGE"
.IP "use encoding [\fI\s-1ENCNAME\s0\fR] ;" 4
.IX Item "use encoding [ENCNAME] ;"
Sets the script encoding to \fI\s-1ENCNAME\s0\fR. Filehandle disciplines of
\&\s-1STDIN\s0 and \s-1STDOUT\s0 are set to ":encoding(\fI\s-1ENCNAME\s0\fR)".  Note that \s-1STDERR\s0
will not be changed.
.Sp
If no encoding is specified, the environment variable \s-1PERL_ENCODING\s0
is consulted.  If no encoding can be found, the error \f(CW\*(C`Unknown encoding
\&'\f(CI\s-1ENCNAME\s0\f(CW'\*(C'\fR will be thrown.
.Sp
Note that non-STD file handles remain unaffected.  Use \f(CW\*(C`use open\*(C'\fR or
\&\f(CW\*(C`binmode\*(C'\fR to change disciplines of those.
.IP "use encoding \fI\s-1ENCNAME\s0\fR [ \s-1STDIN\s0 => \fI\s-1ENCNAME_IN\s0\fR ...] ;" 4
.IX Item "use encoding ENCNAME [ STDIN => ENCNAME_IN ...] ;"
You can also individually set encodings of \s-1STDIN\s0 and \s-1STDOUT\s0 via the
\&\f(CW\*(C`STDIN => \f(CI\s-1ENCNAME\s0\f(CW\*(C'\fR form.  In this case, you cannot omit the
first \fI\s-1ENCNAME\s0\fR.  \f(CW\*(C`STDIN => undef\*(C'\fR turns the \s-1IO\s0 transcoding
completely off.
.IP "no encoding;" 4
.IX Item "no encoding;"
Unsets the script encoding. The disciplines of \s-1STDIN\s0, \s-1STDOUT\s0 are
reset to \*(L":raw\*(R" (the default unprocessed raw stream of bytes).
.SH "CAVEATS"
.IX Header "CAVEATS"
.Sh "\s-1NOT\s0 \s-1SCOPED\s0"
.IX Subsection "NOT SCOPED"
The pragma is a per script, not a per block lexical.  Only the last
\&\f(CW\*(C`use encoding\*(C'\fR or \f(CW\*(C`no encoding\*(C'\fR matters, and it affects 
\&\fBthe whole script\fR.  However, the <no encoding> pragma is supported and 
\&\fBuse encoding\fR can appear as many times as you want in a given script. 
The multiple use of this pragma is discouraged.
.PP
Because of this nature, the use of this pragma inside the module is
strongly discouraged (because the influence of this pragma lasts not
only for the module but the script that uses).  But if you have to,
make sure you say \f(CW\*(C`no encoding\*(C'\fR at the end of the module so you
contain the influence of the pragma within the module.
.Sh "\s-1DO\s0 \s-1NOT\s0 \s-1MIX\s0 \s-1MULTIPLE\s0 \s-1ENCODINGS\s0"
.IX Subsection "DO NOT MIX MULTIPLE ENCODINGS"
Notice that only literals (string or regular expression) having only
legacy code points are affected: if you mix data like this
.PP
.Vb 1
\&        \exDF\ex{100}
.Ve
.PP
the data is assumed to be in (Latin 1 and) Unicode, not in your native
encoding.  In other words, this will match in \*(L"greek\*(R":
.PP
.Vb 1
\&        "\exDF" =~ /\ex{3af}/
.Ve
.PP
but this will not
.PP
.Vb 1
\&        "\exDF\ex{100}" =~ /\ex{3af}\ex{100}/
.Ve
.PP
since the \f(CW\*(C`\exDF\*(C'\fR (\s-1ISO\s0 8859\-7 \s-1GREEK\s0 \s-1SMALL\s0 \s-1LETTER\s0 \s-1IOTA\s0 \s-1WITH\s0 \s-1TONOS\s0) on
the left will \fBnot\fR be upgraded to \f(CW\*(C`\ex{3af}\*(C'\fR (Unicode \s-1GREEK\s0 \s-1SMALL\s0
\&\s-1LETTER\s0 \s-1IOTA\s0 \s-1WITH\s0 \s-1TONOS\s0) because of the \f(CW\*(C`\ex{100}\*(C'\fR on the left.  You
should not be mixing your legacy data and Unicode in the same string.
.PP
This pragma also affects encoding of the 0x80..0xFF code point range:
normally characters in that range are left as eight-bit bytes (unless
they are combined with characters with code points 0x100 or larger,
in which case all characters need to become \s-1UTF\-8\s0 encoded), but if
the \f(CW\*(C`encoding\*(C'\fR pragma is present, even the 0x80..0xFF range always
gets \s-1UTF\-8\s0 encoded.
.PP
After all, the best thing about this pragma is that you don't have to
resort to \ex{....} just to spell your name in a native encoding.
So feel free to put your strings in your encoding in quotes and
regexes.
.SH "Non-ASCII Identifiers and Filter option"
.IX Header "Non-ASCII Identifiers and Filter option"
The magic of \f(CW\*(C`use encoding\*(C'\fR is not applied to the names of
identifiers.  In order to make \f(CW\*(C`${"\ex{4eba}"}++\*(C'\fR ($human++, where human
is a single Han ideograph) work, you still need to write your script
in \s-1UTF\-8\s0 or use a source filter.
.PP
In other words, the same restriction as with Jperl applies.
.PP
If you dare to experiment, however, you can try the Filter option.
.IP "use encoding \fI\s-1ENCNAME\s0\fR Filter=>1;" 4
.IX Item "use encoding ENCNAME Filter=>1;"
This turns the encoding pragma into a source filter.  While the default
approach just decodes interpolated literals (in \fIqq()\fR and \fIqr()\fR), this
will apply a source filter to the entire source code.  In this case,
\&\s-1STDIN\s0 and \s-1STDOUT\s0 remain untouched.
.PP
What does this mean?  Your source code behaves as if it is written in
\&\s-1UTF\-8\s0.  So even if your editor only supports Shift_JIS, for example,
you can still try examples in Chapter 15 of \f(CW\*(C`Programming Perl, 3rd
Ed.\*(C'\fR.  For instance, you can use \s-1UTF\-8\s0 identifiers.
.PP
This option is significantly slower and (as of this writing) non-ASCII
identifiers are not very stable \s-1WITHOUT\s0 this option and with the
source code written in \s-1UTF\-8\s0.
.PP
To make your script in legacy encoding work with minimum effort,
do not use Filter=>1.
.SH "EXAMPLE \- Greekperl"
.IX Header "EXAMPLE - Greekperl"
.Vb 1
\&    use encoding "iso 8859-7";
.Ve
.PP
.Vb 1
\&    # \exDF in ISO 8859-7 (Greek) is \ex{3af} in Unicode.
.Ve
.PP
.Vb 2
\&    $a = "\exDF";
\&    $b = "\ex{100}";
.Ve
.PP
.Vb 1
\&    printf "%#x\en", ord($a); # will print 0x3af, not 0xdf
.Ve
.PP
.Vb 1
\&    $c = $a . $b;
.Ve
.PP
.Vb 1
\&    # $c will be "\ex{3af}\ex{100}", not "\ex{df}\ex{100}".
.Ve
.PP
.Vb 1
\&    # chr() is affected, and ...
.Ve
.PP
.Vb 1
\&    print "mega\en"  if ord(chr(0xdf)) == 0x3af;
.Ve
.PP
.Vb 1
\&    # ... ord() is affected by the encoding pragma ...
.Ve
.PP
.Vb 1
\&    print "tera\en" if ord(pack("C", 0xdf)) == 0x3af;
.Ve
.PP
.Vb 1
\&    # ... as are eq and cmp ...
.Ve
.PP
.Vb 2
\&    print "peta\en" if "\ex{3af}" eq  pack("C", 0xdf);
\&    print "exa\en"  if "\ex{3af}" cmp pack("C", 0xdf) == 0;
.Ve
.PP
.Vb 2
\&    # ... but pack/unpack C are not affected, in case you still
\&    # want to go back to your native encoding
.Ve
.PP
.Vb 1
\&    print "zetta\en" if unpack("C", (pack("C", 0xdf))) == 0xdf;
.Ve
.SH "KNOWN PROBLEMS"
.IX Header "KNOWN PROBLEMS"
For native multibyte encodings (either fixed or variable length),
the current implementation of the regular expressions may introduce
recoding errors for regular expression literals longer than 127 bytes.
.PP
The encoding pragma is not supported on \s-1EBCDIC\s0 platforms.
(Porters who are willing and able to remove this limitation are
welcome.)
.SH "SEE ALSO"
.IX Header "SEE ALSO"
perlunicode, Encode, open, Filter::Util::Call,
.PP
Ch. 15 of \f(CW\*(C`Programming Perl (3rd Edition)\*(C'\fR
by Larry Wall, Tom Christiansen, Jon Orwant;
O'Reilly & Associates; \s-1ISBN\s0 0\-596\-00027\-8

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].