xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/ext/Encode/Encode.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#
2*0Sstevel@tonic-gate# $Id: Encode.pm,v 1.99 2003/12/29 02:47:16 dankogai Exp dankogai $
3*0Sstevel@tonic-gate#
4*0Sstevel@tonic-gatepackage Encode;
5*0Sstevel@tonic-gateuse strict;
6*0Sstevel@tonic-gateour $VERSION = "1.99_01";
7*0Sstevel@tonic-gatesub DEBUG () { 0 }
8*0Sstevel@tonic-gateuse XSLoader ();
9*0Sstevel@tonic-gateXSLoader::load(__PACKAGE__, $VERSION);
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gaterequire Exporter;
12*0Sstevel@tonic-gateuse base qw/Exporter/;
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gate# Public, encouraged API is exported by default
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gateour @EXPORT = qw(
17*0Sstevel@tonic-gate  decode  decode_utf8  encode  encode_utf8
18*0Sstevel@tonic-gate  encodings  find_encoding clone_encoding
19*0Sstevel@tonic-gate);
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gateour @FB_FLAGS  = qw(DIE_ON_ERR WARN_ON_ERR RETURN_ON_ERR LEAVE_SRC
22*0Sstevel@tonic-gate		    PERLQQ HTMLCREF XMLCREF);
23*0Sstevel@tonic-gateour @FB_CONSTS = qw(FB_DEFAULT FB_CROAK FB_QUIET FB_WARN
24*0Sstevel@tonic-gate		    FB_PERLQQ FB_HTMLCREF FB_XMLCREF);
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gateour @EXPORT_OK =
27*0Sstevel@tonic-gate    (
28*0Sstevel@tonic-gate     qw(
29*0Sstevel@tonic-gate       _utf8_off _utf8_on define_encoding from_to is_16bit is_8bit
30*0Sstevel@tonic-gate       is_utf8 perlio_ok resolve_alias utf8_downgrade utf8_upgrade
31*0Sstevel@tonic-gate      ),
32*0Sstevel@tonic-gate     @FB_FLAGS, @FB_CONSTS,
33*0Sstevel@tonic-gate    );
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gateour %EXPORT_TAGS =
36*0Sstevel@tonic-gate    (
37*0Sstevel@tonic-gate     all          =>  [ @EXPORT, @EXPORT_OK ],
38*0Sstevel@tonic-gate     fallbacks    =>  [ @FB_CONSTS ],
39*0Sstevel@tonic-gate     fallback_all =>  [ @FB_CONSTS, @FB_FLAGS ],
40*0Sstevel@tonic-gate    );
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate# Documentation moved after __END__ for speed - NI-S
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gateour $ON_EBCDIC = (ord("A") == 193);
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gateuse Encode::Alias;
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate# Make a %Encoding package variable to allow a certain amount of cheating
49*0Sstevel@tonic-gateour %Encoding;
50*0Sstevel@tonic-gateour %ExtModule;
51*0Sstevel@tonic-gaterequire Encode::Config;
52*0Sstevel@tonic-gateeval { require Encode::ConfigLocal };
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gatesub encodings
55*0Sstevel@tonic-gate{
56*0Sstevel@tonic-gate    my $class = shift;
57*0Sstevel@tonic-gate    my %enc;
58*0Sstevel@tonic-gate    if (@_ and $_[0] eq ":all"){
59*0Sstevel@tonic-gate	%enc = ( %Encoding, %ExtModule );
60*0Sstevel@tonic-gate    }else{
61*0Sstevel@tonic-gate	%enc = %Encoding;
62*0Sstevel@tonic-gate	for my $mod (map {m/::/o ? $_ : "Encode::$_" } @_){
63*0Sstevel@tonic-gate	    DEBUG and warn $mod;
64*0Sstevel@tonic-gate	    for my $enc (keys %ExtModule){
65*0Sstevel@tonic-gate		$ExtModule{$enc} eq $mod and $enc{$enc} = $mod;
66*0Sstevel@tonic-gate	    }
67*0Sstevel@tonic-gate	}
68*0Sstevel@tonic-gate    }
69*0Sstevel@tonic-gate    return
70*0Sstevel@tonic-gate	sort { lc $a cmp lc $b }
71*0Sstevel@tonic-gate             grep {!/^(?:Internal|Unicode|Guess)$/o} keys %enc;
72*0Sstevel@tonic-gate}
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gatesub perlio_ok{
75*0Sstevel@tonic-gate    my $obj = ref($_[0]) ? $_[0] : find_encoding($_[0]);
76*0Sstevel@tonic-gate    $obj->can("perlio_ok") and return $obj->perlio_ok();
77*0Sstevel@tonic-gate    return 0; # safety net
78*0Sstevel@tonic-gate}
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gatesub define_encoding
81*0Sstevel@tonic-gate{
82*0Sstevel@tonic-gate    my $obj  = shift;
83*0Sstevel@tonic-gate    my $name = shift;
84*0Sstevel@tonic-gate    $Encoding{$name} = $obj;
85*0Sstevel@tonic-gate    my $lc = lc($name);
86*0Sstevel@tonic-gate    define_alias($lc => $obj) unless $lc eq $name;
87*0Sstevel@tonic-gate    while (@_){
88*0Sstevel@tonic-gate	my $alias = shift;
89*0Sstevel@tonic-gate	define_alias($alias, $obj);
90*0Sstevel@tonic-gate    }
91*0Sstevel@tonic-gate    return $obj;
92*0Sstevel@tonic-gate}
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gatesub getEncoding
95*0Sstevel@tonic-gate{
96*0Sstevel@tonic-gate    my ($class, $name, $skip_external) = @_;
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate    ref($name) && $name->can('renew') and return $name;
99*0Sstevel@tonic-gate    exists $Encoding{$name} and return $Encoding{$name};
100*0Sstevel@tonic-gate    my $lc = lc $name;
101*0Sstevel@tonic-gate    exists $Encoding{$lc} and return $Encoding{$lc};
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate    my $oc = $class->find_alias($name);
104*0Sstevel@tonic-gate    defined($oc) and return $oc;
105*0Sstevel@tonic-gate    $lc ne $name and $oc = $class->find_alias($lc);
106*0Sstevel@tonic-gate    defined($oc) and return $oc;
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate    unless ($skip_external)
109*0Sstevel@tonic-gate    {
110*0Sstevel@tonic-gate	if (my $mod = $ExtModule{$name} || $ExtModule{$lc}){
111*0Sstevel@tonic-gate	    $mod =~ s,::,/,g ; $mod .= '.pm';
112*0Sstevel@tonic-gate	    eval{ require $mod; };
113*0Sstevel@tonic-gate	    exists $Encoding{$name} and return $Encoding{$name};
114*0Sstevel@tonic-gate	}
115*0Sstevel@tonic-gate    }
116*0Sstevel@tonic-gate    return;
117*0Sstevel@tonic-gate}
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gatesub find_encoding($;$)
120*0Sstevel@tonic-gate{
121*0Sstevel@tonic-gate    my ($name, $skip_external) = @_;
122*0Sstevel@tonic-gate    return __PACKAGE__->getEncoding($name,$skip_external);
123*0Sstevel@tonic-gate}
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gatesub resolve_alias($){
126*0Sstevel@tonic-gate    my $obj = find_encoding(shift);
127*0Sstevel@tonic-gate    defined $obj and return $obj->name;
128*0Sstevel@tonic-gate    return;
129*0Sstevel@tonic-gate}
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gatesub clone_encoding($){
132*0Sstevel@tonic-gate    my $obj = find_encoding(shift);
133*0Sstevel@tonic-gate    ref $obj or return;
134*0Sstevel@tonic-gate    eval { require Storable };
135*0Sstevel@tonic-gate    $@ and return;
136*0Sstevel@tonic-gate    return Storable::dclone($obj);
137*0Sstevel@tonic-gate}
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gatesub encode($$;$)
140*0Sstevel@tonic-gate{
141*0Sstevel@tonic-gate    my ($name, $string, $check) = @_;
142*0Sstevel@tonic-gate    return undef unless defined $string;
143*0Sstevel@tonic-gate    $check ||=0;
144*0Sstevel@tonic-gate    my $enc = find_encoding($name);
145*0Sstevel@tonic-gate    unless(defined $enc){
146*0Sstevel@tonic-gate	require Carp;
147*0Sstevel@tonic-gate	Carp::croak("Unknown encoding '$name'");
148*0Sstevel@tonic-gate    }
149*0Sstevel@tonic-gate    my $octets = $enc->encode($string,$check);
150*0Sstevel@tonic-gate    $_[1] = $string if $check;
151*0Sstevel@tonic-gate    return $octets;
152*0Sstevel@tonic-gate}
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gatesub decode($$;$)
155*0Sstevel@tonic-gate{
156*0Sstevel@tonic-gate    my ($name,$octets,$check) = @_;
157*0Sstevel@tonic-gate    return undef unless defined $octets;
158*0Sstevel@tonic-gate    $check ||=0;
159*0Sstevel@tonic-gate    my $enc = find_encoding($name);
160*0Sstevel@tonic-gate    unless(defined $enc){
161*0Sstevel@tonic-gate	require Carp;
162*0Sstevel@tonic-gate	Carp::croak("Unknown encoding '$name'");
163*0Sstevel@tonic-gate    }
164*0Sstevel@tonic-gate    my $string = $enc->decode($octets,$check);
165*0Sstevel@tonic-gate    $_[1] = $octets if $check;
166*0Sstevel@tonic-gate    return $string;
167*0Sstevel@tonic-gate}
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gatesub from_to($$$;$)
170*0Sstevel@tonic-gate{
171*0Sstevel@tonic-gate    my ($string,$from,$to,$check) = @_;
172*0Sstevel@tonic-gate    return undef unless defined $string;
173*0Sstevel@tonic-gate    $check ||=0;
174*0Sstevel@tonic-gate    my $f = find_encoding($from);
175*0Sstevel@tonic-gate    unless (defined $f){
176*0Sstevel@tonic-gate	require Carp;
177*0Sstevel@tonic-gate	Carp::croak("Unknown encoding '$from'");
178*0Sstevel@tonic-gate    }
179*0Sstevel@tonic-gate    my $t = find_encoding($to);
180*0Sstevel@tonic-gate    unless (defined $t){
181*0Sstevel@tonic-gate	require Carp;
182*0Sstevel@tonic-gate	Carp::croak("Unknown encoding '$to'");
183*0Sstevel@tonic-gate    }
184*0Sstevel@tonic-gate    my $uni = $f->decode($string,$check);
185*0Sstevel@tonic-gate    return undef if ($check && length($string));
186*0Sstevel@tonic-gate    $string =  $t->encode($uni,$check);
187*0Sstevel@tonic-gate    return undef if ($check && length($uni));
188*0Sstevel@tonic-gate    return defined($_[0] = $string) ? length($string) : undef ;
189*0Sstevel@tonic-gate}
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gatesub encode_utf8($)
192*0Sstevel@tonic-gate{
193*0Sstevel@tonic-gate    my ($str) = @_;
194*0Sstevel@tonic-gate    utf8::encode($str);
195*0Sstevel@tonic-gate    return $str;
196*0Sstevel@tonic-gate}
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gatesub decode_utf8($;$)
199*0Sstevel@tonic-gate{
200*0Sstevel@tonic-gate    my ($str, $check) = @_;
201*0Sstevel@tonic-gate    if ($check){
202*0Sstevel@tonic-gate	return decode("utf8", $str, $check);
203*0Sstevel@tonic-gate    }else{
204*0Sstevel@tonic-gate	return undef unless utf8::decode($str);
205*0Sstevel@tonic-gate	return $str;
206*0Sstevel@tonic-gate    }
207*0Sstevel@tonic-gate}
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gatepredefine_encodings(1);
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate#
212*0Sstevel@tonic-gate# This is to restore %Encoding if really needed;
213*0Sstevel@tonic-gate#
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gatesub predefine_encodings{
216*0Sstevel@tonic-gate    use Encode::Encoding;
217*0Sstevel@tonic-gate    no warnings 'redefine';
218*0Sstevel@tonic-gate    my $use_xs = shift;
219*0Sstevel@tonic-gate    if ($ON_EBCDIC) {
220*0Sstevel@tonic-gate	# was in Encode::UTF_EBCDIC
221*0Sstevel@tonic-gate	package Encode::UTF_EBCDIC;
222*0Sstevel@tonic-gate	push @Encode::UTF_EBCDIC::ISA, 'Encode::Encoding';
223*0Sstevel@tonic-gate	*decode = sub{
224*0Sstevel@tonic-gate	    my ($obj,$str,$chk) = @_;
225*0Sstevel@tonic-gate	    my $res = '';
226*0Sstevel@tonic-gate	    for (my $i = 0; $i < length($str); $i++) {
227*0Sstevel@tonic-gate		$res .=
228*0Sstevel@tonic-gate		    chr(utf8::unicode_to_native(ord(substr($str,$i,1))));
229*0Sstevel@tonic-gate	    }
230*0Sstevel@tonic-gate	    $_[1] = '' if $chk;
231*0Sstevel@tonic-gate	    return $res;
232*0Sstevel@tonic-gate	};
233*0Sstevel@tonic-gate	*encode = sub{
234*0Sstevel@tonic-gate	    my ($obj,$str,$chk) = @_;
235*0Sstevel@tonic-gate	    my $res = '';
236*0Sstevel@tonic-gate	    for (my $i = 0; $i < length($str); $i++) {
237*0Sstevel@tonic-gate		$res .=
238*0Sstevel@tonic-gate		    chr(utf8::native_to_unicode(ord(substr($str,$i,1))));
239*0Sstevel@tonic-gate	    }
240*0Sstevel@tonic-gate	    $_[1] = '' if $chk;
241*0Sstevel@tonic-gate	    return $res;
242*0Sstevel@tonic-gate	};
243*0Sstevel@tonic-gate	$Encode::Encoding{Unicode} =
244*0Sstevel@tonic-gate	    bless {Name => "UTF_EBCDIC"} => "Encode::UTF_EBCDIC";
245*0Sstevel@tonic-gate    } else {
246*0Sstevel@tonic-gate	package Encode::Internal;
247*0Sstevel@tonic-gate	push @Encode::Internal::ISA, 'Encode::Encoding';
248*0Sstevel@tonic-gate	*decode = sub{
249*0Sstevel@tonic-gate	    my ($obj,$str,$chk) = @_;
250*0Sstevel@tonic-gate	    utf8::upgrade($str);
251*0Sstevel@tonic-gate	    $_[1] = '' if $chk;
252*0Sstevel@tonic-gate	    return $str;
253*0Sstevel@tonic-gate	};
254*0Sstevel@tonic-gate	*encode = \&decode;
255*0Sstevel@tonic-gate	$Encode::Encoding{Unicode} =
256*0Sstevel@tonic-gate	    bless {Name => "Internal"} => "Encode::Internal";
257*0Sstevel@tonic-gate    }
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate    {
260*0Sstevel@tonic-gate	# was in Encode::utf8
261*0Sstevel@tonic-gate	package Encode::utf8;
262*0Sstevel@tonic-gate	push @Encode::utf8::ISA, 'Encode::Encoding';
263*0Sstevel@tonic-gate	#
264*0Sstevel@tonic-gate	if ($use_xs){
265*0Sstevel@tonic-gate	    Encode::DEBUG and warn __PACKAGE__, " XS on";
266*0Sstevel@tonic-gate	    *decode = \&decode_xs;
267*0Sstevel@tonic-gate	    *encode = \&encode_xs;
268*0Sstevel@tonic-gate	}else{
269*0Sstevel@tonic-gate	    Encode::DEBUG and warn __PACKAGE__, " XS off";
270*0Sstevel@tonic-gate	    *decode = sub{
271*0Sstevel@tonic-gate		my ($obj,$octets,$chk) = @_;
272*0Sstevel@tonic-gate		my $str = Encode::decode_utf8($octets);
273*0Sstevel@tonic-gate		if (defined $str) {
274*0Sstevel@tonic-gate		    $_[1] = '' if $chk;
275*0Sstevel@tonic-gate		    return $str;
276*0Sstevel@tonic-gate		}
277*0Sstevel@tonic-gate		return undef;
278*0Sstevel@tonic-gate	    };
279*0Sstevel@tonic-gate	    *encode = sub {
280*0Sstevel@tonic-gate		my ($obj,$string,$chk) = @_;
281*0Sstevel@tonic-gate		my $octets = Encode::encode_utf8($string);
282*0Sstevel@tonic-gate		$_[1] = '' if $chk;
283*0Sstevel@tonic-gate		return $octets;
284*0Sstevel@tonic-gate	    };
285*0Sstevel@tonic-gate	}
286*0Sstevel@tonic-gate	*cat_decode = sub{ # ($obj, $dst, $src, $pos, $trm, $chk)
287*0Sstevel@tonic-gate	    my ($obj, undef, undef, $pos, $trm) = @_; # currently ignores $chk
288*0Sstevel@tonic-gate	    my ($rdst, $rsrc, $rpos) = \@_[1,2,3];
289*0Sstevel@tonic-gate	    use bytes;
290*0Sstevel@tonic-gate	    if ((my $npos = index($$rsrc, $trm, $pos)) >= 0) {
291*0Sstevel@tonic-gate		$$rdst .= substr($$rsrc, $pos, $npos - $pos + length($trm));
292*0Sstevel@tonic-gate		$$rpos = $npos + length($trm);
293*0Sstevel@tonic-gate		return 1;
294*0Sstevel@tonic-gate	    }
295*0Sstevel@tonic-gate	    $$rdst .= substr($$rsrc, $pos);
296*0Sstevel@tonic-gate	    $$rpos = length($$rsrc);
297*0Sstevel@tonic-gate	    return '';
298*0Sstevel@tonic-gate	};
299*0Sstevel@tonic-gate	$Encode::Encoding{utf8} =
300*0Sstevel@tonic-gate	    bless {Name => "utf8"} => "Encode::utf8";
301*0Sstevel@tonic-gate    }
302*0Sstevel@tonic-gate}
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gate1;
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate__END__
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate=head1 NAME
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gateEncode - character encodings
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate=head1 SYNOPSIS
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate    use Encode;
315*0Sstevel@tonic-gate
316*0Sstevel@tonic-gate=head2 Table of Contents
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gateEncode consists of a collection of modules whose details are too big
319*0Sstevel@tonic-gateto fit in one document.  This POD itself explains the top-level APIs
320*0Sstevel@tonic-gateand general topics at a glance.  For other topics and more details,
321*0Sstevel@tonic-gatesee the PODs below:
322*0Sstevel@tonic-gate
323*0Sstevel@tonic-gate  Name			        Description
324*0Sstevel@tonic-gate  --------------------------------------------------------
325*0Sstevel@tonic-gate  Encode::Alias         Alias definitions to encodings
326*0Sstevel@tonic-gate  Encode::Encoding      Encode Implementation Base Class
327*0Sstevel@tonic-gate  Encode::Supported     List of Supported Encodings
328*0Sstevel@tonic-gate  Encode::CN            Simplified Chinese Encodings
329*0Sstevel@tonic-gate  Encode::JP            Japanese Encodings
330*0Sstevel@tonic-gate  Encode::KR            Korean Encodings
331*0Sstevel@tonic-gate  Encode::TW            Traditional Chinese Encodings
332*0Sstevel@tonic-gate  --------------------------------------------------------
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gate=head1 DESCRIPTION
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gateThe C<Encode> module provides the interfaces between Perl's strings
337*0Sstevel@tonic-gateand the rest of the system.  Perl strings are sequences of
338*0Sstevel@tonic-gateB<characters>.
339*0Sstevel@tonic-gate
340*0Sstevel@tonic-gateThe repertoire of characters that Perl can represent is at least that
341*0Sstevel@tonic-gatedefined by the Unicode Consortium. On most platforms the ordinal
342*0Sstevel@tonic-gatevalues of the characters (as returned by C<ord(ch)>) is the "Unicode
343*0Sstevel@tonic-gatecodepoint" for the character (the exceptions are those platforms where
344*0Sstevel@tonic-gatethe legacy encoding is some variant of EBCDIC rather than a super-set
345*0Sstevel@tonic-gateof ASCII - see L<perlebcdic>).
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gateTraditionally, computer data has been moved around in 8-bit chunks
348*0Sstevel@tonic-gateoften called "bytes". These chunks are also known as "octets" in
349*0Sstevel@tonic-gatenetworking standards. Perl is widely used to manipulate data of many
350*0Sstevel@tonic-gatetypes - not only strings of characters representing human or computer
351*0Sstevel@tonic-gatelanguages but also "binary" data being the machine's representation of
352*0Sstevel@tonic-gatenumbers, pixels in an image - or just about anything.
353*0Sstevel@tonic-gate
354*0Sstevel@tonic-gateWhen Perl is processing "binary data", the programmer wants Perl to
355*0Sstevel@tonic-gateprocess "sequences of bytes". This is not a problem for Perl - as a
356*0Sstevel@tonic-gatebyte has 256 possible values, it easily fits in Perl's much larger
357*0Sstevel@tonic-gate"logical character".
358*0Sstevel@tonic-gate
359*0Sstevel@tonic-gate=head2 TERMINOLOGY
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate=over 2
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate=item *
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gateI<character>: a character in the range 0..(2**32-1) (or more).
366*0Sstevel@tonic-gate(What Perl's strings are made of.)
367*0Sstevel@tonic-gate
368*0Sstevel@tonic-gate=item *
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gateI<byte>: a character in the range 0..255
371*0Sstevel@tonic-gate(A special case of a Perl character.)
372*0Sstevel@tonic-gate
373*0Sstevel@tonic-gate=item *
374*0Sstevel@tonic-gate
375*0Sstevel@tonic-gateI<octet>: 8 bits of data, with ordinal values 0..255
376*0Sstevel@tonic-gate(Term for bytes passed to or from a non-Perl context, e.g. a disk file.)
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gate=back
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate=head1 PERL ENCODING API
381*0Sstevel@tonic-gate
382*0Sstevel@tonic-gate=over 2
383*0Sstevel@tonic-gate
384*0Sstevel@tonic-gate=item $octets  = encode(ENCODING, $string [, CHECK])
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gateEncodes a string from Perl's internal form into I<ENCODING> and returns
387*0Sstevel@tonic-gatea sequence of octets.  ENCODING can be either a canonical name or
388*0Sstevel@tonic-gatean alias.  For encoding names and aliases, see L</"Defining Aliases">.
389*0Sstevel@tonic-gateFor CHECK, see L</"Handling Malformed Data">.
390*0Sstevel@tonic-gate
391*0Sstevel@tonic-gateFor example, to convert a string from Perl's internal format to
392*0Sstevel@tonic-gateiso-8859-1 (also known as Latin1),
393*0Sstevel@tonic-gate
394*0Sstevel@tonic-gate  $octets = encode("iso-8859-1", $string);
395*0Sstevel@tonic-gate
396*0Sstevel@tonic-gateB<CAVEAT>: When you run C<$octets = encode("utf8", $string)>, then $octets
397*0Sstevel@tonic-gateB<may not be equal to> $string.  Though they both contain the same data, the utf8 flag
398*0Sstevel@tonic-gatefor $octets is B<always> off.  When you encode anything, utf8 flag of
399*0Sstevel@tonic-gatethe result is always off, even when it contains completely valid utf8
400*0Sstevel@tonic-gatestring. See L</"The UTF-8 flag"> below.
401*0Sstevel@tonic-gate
402*0Sstevel@tonic-gateencode($valid_encoding, undef) is harmless but warns you for
403*0Sstevel@tonic-gateC<Use of uninitialized value in subroutine entry>.
404*0Sstevel@tonic-gateencode($valid_encoding, '') is harmless and warnless.
405*0Sstevel@tonic-gate
406*0Sstevel@tonic-gate=item $string = decode(ENCODING, $octets [, CHECK])
407*0Sstevel@tonic-gate
408*0Sstevel@tonic-gateDecodes a sequence of octets assumed to be in I<ENCODING> into Perl's
409*0Sstevel@tonic-gateinternal form and returns the resulting string.  As in encode(),
410*0Sstevel@tonic-gateENCODING can be either a canonical name or an alias. For encoding names
411*0Sstevel@tonic-gateand aliases, see L</"Defining Aliases">.  For CHECK, see
412*0Sstevel@tonic-gateL</"Handling Malformed Data">.
413*0Sstevel@tonic-gate
414*0Sstevel@tonic-gateFor example, to convert ISO-8859-1 data to a string in Perl's internal format:
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gate  $string = decode("iso-8859-1", $octets);
417*0Sstevel@tonic-gate
418*0Sstevel@tonic-gateB<CAVEAT>: When you run C<$string = decode("utf8", $octets)>, then $string
419*0Sstevel@tonic-gateB<may not be equal to> $octets.  Though they both contain the same data,
420*0Sstevel@tonic-gatethe utf8 flag for $string is on unless $octets entirely consists of
421*0Sstevel@tonic-gateASCII data (or EBCDIC on EBCDIC machines).  See L</"The UTF-8 flag">
422*0Sstevel@tonic-gatebelow.
423*0Sstevel@tonic-gate
424*0Sstevel@tonic-gatedecode($valid_encoding, undef) is harmless but warns you for
425*0Sstevel@tonic-gateC<Use of uninitialized value in subroutine entry>.
426*0Sstevel@tonic-gatedecode($valid_encoding, '') is harmless and warnless.
427*0Sstevel@tonic-gate
428*0Sstevel@tonic-gate=item [$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])
429*0Sstevel@tonic-gate
430*0Sstevel@tonic-gateConverts B<in-place> data between two encodings. The data in $octets
431*0Sstevel@tonic-gatemust be encoded as octets and not as characters in Perl's internal
432*0Sstevel@tonic-gateformat. For example, to convert ISO-8859-1 data to Microsoft's CP1250 encoding:
433*0Sstevel@tonic-gate
434*0Sstevel@tonic-gate  from_to($octets, "iso-8859-1", "cp1250");
435*0Sstevel@tonic-gate
436*0Sstevel@tonic-gateand to convert it back:
437*0Sstevel@tonic-gate
438*0Sstevel@tonic-gate  from_to($octets, "cp1250", "iso-8859-1");
439*0Sstevel@tonic-gate
440*0Sstevel@tonic-gateNote that because the conversion happens in place, the data to be
441*0Sstevel@tonic-gateconverted cannot be a string constant; it must be a scalar variable.
442*0Sstevel@tonic-gate
443*0Sstevel@tonic-gatefrom_to() returns the length of the converted string in octets on success, undef
444*0Sstevel@tonic-gateotherwise.
445*0Sstevel@tonic-gate
446*0Sstevel@tonic-gateB<CAVEAT>: The following operations look the same but are not quite so;
447*0Sstevel@tonic-gate
448*0Sstevel@tonic-gate  from_to($data, "iso-8859-1", "utf8"); #1
449*0Sstevel@tonic-gate  $data = decode("iso-8859-1", $data);  #2
450*0Sstevel@tonic-gate
451*0Sstevel@tonic-gateBoth #1 and #2 make $data consist of a completely valid UTF-8 string
452*0Sstevel@tonic-gatebut only #2 turns utf8 flag on.  #1 is equivalent to
453*0Sstevel@tonic-gate
454*0Sstevel@tonic-gate  $data = encode("utf8", decode("iso-8859-1", $data));
455*0Sstevel@tonic-gate
456*0Sstevel@tonic-gateSee L</"The UTF-8 flag"> below.
457*0Sstevel@tonic-gate
458*0Sstevel@tonic-gate=item $octets = encode_utf8($string);
459*0Sstevel@tonic-gate
460*0Sstevel@tonic-gateEquivalent to C<$octets = encode("utf8", $string);> The characters
461*0Sstevel@tonic-gatethat comprise $string are encoded in Perl's internal format and the
462*0Sstevel@tonic-gateresult is returned as a sequence of octets. All possible
463*0Sstevel@tonic-gatecharacters have a UTF-8 representation so this function cannot fail.
464*0Sstevel@tonic-gate
465*0Sstevel@tonic-gate
466*0Sstevel@tonic-gate=item $string = decode_utf8($octets [, CHECK]);
467*0Sstevel@tonic-gate
468*0Sstevel@tonic-gateequivalent to C<$string = decode("utf8", $octets [, CHECK])>.
469*0Sstevel@tonic-gateThe sequence of octets represented by
470*0Sstevel@tonic-gate$octets is decoded from UTF-8 into a sequence of logical
471*0Sstevel@tonic-gatecharacters. Not all sequences of octets form valid UTF-8 encodings, so
472*0Sstevel@tonic-gateit is possible for this call to fail.  For CHECK, see
473*0Sstevel@tonic-gateL</"Handling Malformed Data">.
474*0Sstevel@tonic-gate
475*0Sstevel@tonic-gate=back
476*0Sstevel@tonic-gate
477*0Sstevel@tonic-gate=head2 Listing available encodings
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate  use Encode;
480*0Sstevel@tonic-gate  @list = Encode->encodings();
481*0Sstevel@tonic-gate
482*0Sstevel@tonic-gateReturns a list of the canonical names of the available encodings that
483*0Sstevel@tonic-gateare loaded.  To get a list of all available encodings including the
484*0Sstevel@tonic-gateones that are not loaded yet, say
485*0Sstevel@tonic-gate
486*0Sstevel@tonic-gate  @all_encodings = Encode->encodings(":all");
487*0Sstevel@tonic-gate
488*0Sstevel@tonic-gateOr you can give the name of a specific module.
489*0Sstevel@tonic-gate
490*0Sstevel@tonic-gate  @with_jp = Encode->encodings("Encode::JP");
491*0Sstevel@tonic-gate
492*0Sstevel@tonic-gateWhen "::" is not in the name, "Encode::" is assumed.
493*0Sstevel@tonic-gate
494*0Sstevel@tonic-gate  @ebcdic = Encode->encodings("EBCDIC");
495*0Sstevel@tonic-gate
496*0Sstevel@tonic-gateTo find out in detail which encodings are supported by this package,
497*0Sstevel@tonic-gatesee L<Encode::Supported>.
498*0Sstevel@tonic-gate
499*0Sstevel@tonic-gate=head2 Defining Aliases
500*0Sstevel@tonic-gate
501*0Sstevel@tonic-gateTo add a new alias to a given encoding, use:
502*0Sstevel@tonic-gate
503*0Sstevel@tonic-gate  use Encode;
504*0Sstevel@tonic-gate  use Encode::Alias;
505*0Sstevel@tonic-gate  define_alias(newName => ENCODING);
506*0Sstevel@tonic-gate
507*0Sstevel@tonic-gateAfter that, newName can be used as an alias for ENCODING.
508*0Sstevel@tonic-gateENCODING may be either the name of an encoding or an
509*0Sstevel@tonic-gateI<encoding object>
510*0Sstevel@tonic-gate
511*0Sstevel@tonic-gateBut before you do so, make sure the alias is nonexistent with
512*0Sstevel@tonic-gateC<resolve_alias()>, which returns the canonical name thereof.
513*0Sstevel@tonic-gatei.e.
514*0Sstevel@tonic-gate
515*0Sstevel@tonic-gate  Encode::resolve_alias("latin1") eq "iso-8859-1" # true
516*0Sstevel@tonic-gate  Encode::resolve_alias("iso-8859-12")   # false; nonexistent
517*0Sstevel@tonic-gate  Encode::resolve_alias($name) eq $name  # true if $name is canonical
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gateresolve_alias() does not need C<use Encode::Alias>; it can be
520*0Sstevel@tonic-gateexported via C<use Encode qw(resolve_alias)>.
521*0Sstevel@tonic-gate
522*0Sstevel@tonic-gateSee L<Encode::Alias> for details.
523*0Sstevel@tonic-gate
524*0Sstevel@tonic-gate=head1 Encoding via PerlIO
525*0Sstevel@tonic-gate
526*0Sstevel@tonic-gateIf your perl supports I<PerlIO> (which is the default), you can use a PerlIO layer to decode
527*0Sstevel@tonic-gateand encode directly via a filehandle.  The following two examples
528*0Sstevel@tonic-gateare totally identical in their functionality.
529*0Sstevel@tonic-gate
530*0Sstevel@tonic-gate  # via PerlIO
531*0Sstevel@tonic-gate  open my $in,  "<:encoding(shiftjis)", $infile  or die;
532*0Sstevel@tonic-gate  open my $out, ">:encoding(euc-jp)",   $outfile or die;
533*0Sstevel@tonic-gate  while(<$in>){ print $out $_; }
534*0Sstevel@tonic-gate
535*0Sstevel@tonic-gate  # via from_to
536*0Sstevel@tonic-gate  open my $in,  "<", $infile  or die;
537*0Sstevel@tonic-gate  open my $out, ">", $outfile or die;
538*0Sstevel@tonic-gate  while(<$in>){
539*0Sstevel@tonic-gate    from_to($_, "shiftjis", "euc-jp", 1);
540*0Sstevel@tonic-gate    print $out $_;
541*0Sstevel@tonic-gate  }
542*0Sstevel@tonic-gate
543*0Sstevel@tonic-gateUnfortunately, it may be that encodings are PerlIO-savvy.  You can check
544*0Sstevel@tonic-gateif your encoding is supported by PerlIO by calling the C<perlio_ok>
545*0Sstevel@tonic-gatemethod.
546*0Sstevel@tonic-gate
547*0Sstevel@tonic-gate  Encode::perlio_ok("hz");             # False
548*0Sstevel@tonic-gate  find_encoding("euc-cn")->perlio_ok;  # True where PerlIO is available
549*0Sstevel@tonic-gate
550*0Sstevel@tonic-gate  use Encode qw(perlio_ok);            # exported upon request
551*0Sstevel@tonic-gate  perlio_ok("euc-jp")
552*0Sstevel@tonic-gate
553*0Sstevel@tonic-gateFortunately, all encodings that come with Encode core are PerlIO-savvy
554*0Sstevel@tonic-gateexcept for hz and ISO-2022-kr.  For gory details, see L<Encode::Encoding> and L<Encode::PerlIO>.
555*0Sstevel@tonic-gate
556*0Sstevel@tonic-gate=head1 Handling Malformed Data
557*0Sstevel@tonic-gate
558*0Sstevel@tonic-gateThe I<CHECK> argument is used as follows.  When you omit it,
559*0Sstevel@tonic-gatethe behaviour is the same as if you had passed a value of 0 for
560*0Sstevel@tonic-gateI<CHECK>.
561*0Sstevel@tonic-gate
562*0Sstevel@tonic-gate=over 2
563*0Sstevel@tonic-gate
564*0Sstevel@tonic-gate=item I<CHECK> = Encode::FB_DEFAULT ( == 0)
565*0Sstevel@tonic-gate
566*0Sstevel@tonic-gateIf I<CHECK> is 0, (en|de)code will put a I<substitution character>
567*0Sstevel@tonic-gatein place of a malformed character.  For UCM-based encodings,
568*0Sstevel@tonic-gateE<lt>subcharE<gt> will be used.  For Unicode, the code point C<0xFFFD> is used.
569*0Sstevel@tonic-gateIf the data is supposed to be UTF-8, an optional lexical warning
570*0Sstevel@tonic-gate(category utf8) is given.
571*0Sstevel@tonic-gate
572*0Sstevel@tonic-gate=item I<CHECK> = Encode::FB_CROAK ( == 1)
573*0Sstevel@tonic-gate
574*0Sstevel@tonic-gateIf I<CHECK> is 1, methods will die on error immediately with an error
575*0Sstevel@tonic-gatemessage.  Therefore, when I<CHECK> is set to 1,  you should trap the
576*0Sstevel@tonic-gatefatal error with eval{} unless you really want to let it die on error.
577*0Sstevel@tonic-gate
578*0Sstevel@tonic-gate=item I<CHECK> = Encode::FB_QUIET
579*0Sstevel@tonic-gate
580*0Sstevel@tonic-gateIf I<CHECK> is set to Encode::FB_QUIET, (en|de)code will immediately
581*0Sstevel@tonic-gatereturn the portion of the data that has been processed so far when
582*0Sstevel@tonic-gatean error occurs. The data argument will be overwritten with
583*0Sstevel@tonic-gateeverything after that point (that is, the unprocessed part of data).
584*0Sstevel@tonic-gateThis is handy when you have to call decode repeatedly in the case
585*0Sstevel@tonic-gatewhere your source data may contain partial multi-byte character
586*0Sstevel@tonic-gatesequences, for example because you are reading with a fixed-width
587*0Sstevel@tonic-gatebuffer. Here is some sample code that does exactly this:
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate  my $data = ''; my $utf8 = '';
590*0Sstevel@tonic-gate  while(defined(read $fh, $buffer, 256)){
591*0Sstevel@tonic-gate    # buffer may end in a partial character so we append
592*0Sstevel@tonic-gate    $data .= $buffer;
593*0Sstevel@tonic-gate    $utf8 .= decode($encoding, $data, Encode::FB_QUIET);
594*0Sstevel@tonic-gate    # $data now contains the unprocessed partial character
595*0Sstevel@tonic-gate  }
596*0Sstevel@tonic-gate
597*0Sstevel@tonic-gate=item I<CHECK> = Encode::FB_WARN
598*0Sstevel@tonic-gate
599*0Sstevel@tonic-gateThis is the same as above, except that it warns on error.  Handy when
600*0Sstevel@tonic-gateyou are debugging the mode above.
601*0Sstevel@tonic-gate
602*0Sstevel@tonic-gate=item perlqq mode (I<CHECK> = Encode::FB_PERLQQ)
603*0Sstevel@tonic-gate
604*0Sstevel@tonic-gate=item HTML charref mode (I<CHECK> = Encode::FB_HTMLCREF)
605*0Sstevel@tonic-gate
606*0Sstevel@tonic-gate=item XML charref mode (I<CHECK> = Encode::FB_XMLCREF)
607*0Sstevel@tonic-gate
608*0Sstevel@tonic-gateFor encodings that are implemented by Encode::XS, CHECK ==
609*0Sstevel@tonic-gateEncode::FB_PERLQQ turns (en|de)code into C<perlqq> fallback mode.
610*0Sstevel@tonic-gate
611*0Sstevel@tonic-gateWhen you decode, C<\xI<HH>> will be inserted for a malformed character,
612*0Sstevel@tonic-gatewhere I<HH> is the hex representation of the octet  that could not be
613*0Sstevel@tonic-gatedecoded to utf8.  And when you encode, C<\x{I<HHHH>}> will be inserted,
614*0Sstevel@tonic-gatewhere I<HHHH> is the Unicode ID of the character that cannot be found
615*0Sstevel@tonic-gatein the character repertoire of the encoding.
616*0Sstevel@tonic-gate
617*0Sstevel@tonic-gateHTML/XML character reference modes are about the same, in place of
618*0Sstevel@tonic-gateC<\x{I<HHHH>}>, HTML uses C<&#I<NNNN>>; where I<NNNN> is a decimal digit and
619*0Sstevel@tonic-gateXML uses C<&#xI<HHHH>>; where I<HHHH> is the hexadecimal digit.
620*0Sstevel@tonic-gate
621*0Sstevel@tonic-gate=item The bitmask
622*0Sstevel@tonic-gate
623*0Sstevel@tonic-gateThese modes are actually set via a bitmask.  Here is how the FB_XX
624*0Sstevel@tonic-gateconstants are laid out.  You can import the FB_XX constants via
625*0Sstevel@tonic-gateC<use Encode qw(:fallbacks)>; you can import the generic bitmask
626*0Sstevel@tonic-gateconstants via C<use Encode qw(:fallback_all)>.
627*0Sstevel@tonic-gate
628*0Sstevel@tonic-gate                     FB_DEFAULT FB_CROAK FB_QUIET FB_WARN  FB_PERLQQ
629*0Sstevel@tonic-gate DIE_ON_ERR    0x0001             X
630*0Sstevel@tonic-gate WARN_ON_ERR   0x0002                               X
631*0Sstevel@tonic-gate RETURN_ON_ERR 0x0004                      X        X
632*0Sstevel@tonic-gate LEAVE_SRC     0x0008
633*0Sstevel@tonic-gate PERLQQ        0x0100                                        X
634*0Sstevel@tonic-gate HTMLCREF      0x0200
635*0Sstevel@tonic-gate XMLCREF       0x0400
636*0Sstevel@tonic-gate
637*0Sstevel@tonic-gate=back
638*0Sstevel@tonic-gate
639*0Sstevel@tonic-gate=head2 Unimplemented fallback schemes
640*0Sstevel@tonic-gate
641*0Sstevel@tonic-gateIn the future, you will be able to use a code reference to a callback
642*0Sstevel@tonic-gatefunction for the value of I<CHECK> but its API is still undecided.
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gateThe fallback scheme does not work on EBCDIC platforms.
645*0Sstevel@tonic-gate
646*0Sstevel@tonic-gate=head1 Defining Encodings
647*0Sstevel@tonic-gate
648*0Sstevel@tonic-gateTo define a new encoding, use:
649*0Sstevel@tonic-gate
650*0Sstevel@tonic-gate    use Encode qw(define_encoding);
651*0Sstevel@tonic-gate    define_encoding($object, 'canonicalName' [, alias...]);
652*0Sstevel@tonic-gate
653*0Sstevel@tonic-gateI<canonicalName> will be associated with I<$object>.  The object
654*0Sstevel@tonic-gateshould provide the interface described in L<Encode::Encoding>.
655*0Sstevel@tonic-gateIf more than two arguments are provided then additional
656*0Sstevel@tonic-gatearguments are taken as aliases for I<$object>.
657*0Sstevel@tonic-gate
658*0Sstevel@tonic-gateSee L<Encode::Encoding> for more details.
659*0Sstevel@tonic-gate
660*0Sstevel@tonic-gate=head1 The UTF-8 flag
661*0Sstevel@tonic-gate
662*0Sstevel@tonic-gateBefore the introduction of utf8 support in perl, The C<eq> operator
663*0Sstevel@tonic-gatejust compared the strings represented by two scalars. Beginning with
664*0Sstevel@tonic-gateperl 5.8, C<eq> compares two strings with simultaneous consideration
665*0Sstevel@tonic-gateof I<the utf8 flag>. To explain why we made it so, I will quote page
666*0Sstevel@tonic-gate402 of C<Programming Perl, 3rd ed.>
667*0Sstevel@tonic-gate
668*0Sstevel@tonic-gate=over 2
669*0Sstevel@tonic-gate
670*0Sstevel@tonic-gate=item Goal #1:
671*0Sstevel@tonic-gate
672*0Sstevel@tonic-gateOld byte-oriented programs should not spontaneously break on the old
673*0Sstevel@tonic-gatebyte-oriented data they used to work on.
674*0Sstevel@tonic-gate
675*0Sstevel@tonic-gate=item Goal #2:
676*0Sstevel@tonic-gate
677*0Sstevel@tonic-gateOld byte-oriented programs should magically start working on the new
678*0Sstevel@tonic-gatecharacter-oriented data when appropriate.
679*0Sstevel@tonic-gate
680*0Sstevel@tonic-gate=item Goal #3:
681*0Sstevel@tonic-gate
682*0Sstevel@tonic-gatePrograms should run just as fast in the new character-oriented mode
683*0Sstevel@tonic-gateas in the old byte-oriented mode.
684*0Sstevel@tonic-gate
685*0Sstevel@tonic-gate=item Goal #4:
686*0Sstevel@tonic-gate
687*0Sstevel@tonic-gatePerl should remain one language, rather than forking into a
688*0Sstevel@tonic-gatebyte-oriented Perl and a character-oriented Perl.
689*0Sstevel@tonic-gate
690*0Sstevel@tonic-gate=back
691*0Sstevel@tonic-gate
692*0Sstevel@tonic-gateBack when C<Programming Perl, 3rd ed.> was written, not even Perl 5.6.0
693*0Sstevel@tonic-gatewas born and many features documented in the book remained
694*0Sstevel@tonic-gateunimplemented for a long time.  Perl 5.8 corrected this and the introduction
695*0Sstevel@tonic-gateof the UTF-8 flag is one of them.  You can think of this perl notion as of a
696*0Sstevel@tonic-gatebyte-oriented mode (utf8 flag off) and a character-oriented mode (utf8
697*0Sstevel@tonic-gateflag on).
698*0Sstevel@tonic-gate
699*0Sstevel@tonic-gateHere is how Encode takes care of the utf8 flag.
700*0Sstevel@tonic-gate
701*0Sstevel@tonic-gate=over 2
702*0Sstevel@tonic-gate
703*0Sstevel@tonic-gate=item *
704*0Sstevel@tonic-gate
705*0Sstevel@tonic-gateWhen you encode, the resulting utf8 flag is always off.
706*0Sstevel@tonic-gate
707*0Sstevel@tonic-gate=item *
708*0Sstevel@tonic-gate
709*0Sstevel@tonic-gateWhen you decode, the resulting utf8 flag is on unless you can
710*0Sstevel@tonic-gateunambiguously represent data.  Here is the definition of
711*0Sstevel@tonic-gatedis-ambiguity.
712*0Sstevel@tonic-gate
713*0Sstevel@tonic-gateAfter C<$utf8 = decode('foo', $octet);>,
714*0Sstevel@tonic-gate
715*0Sstevel@tonic-gate  When $octet is...   The utf8 flag in $utf8 is
716*0Sstevel@tonic-gate  ---------------------------------------------
717*0Sstevel@tonic-gate  In ASCII only (or EBCDIC only)            OFF
718*0Sstevel@tonic-gate  In ISO-8859-1                              ON
719*0Sstevel@tonic-gate  In any other Encoding                      ON
720*0Sstevel@tonic-gate  ---------------------------------------------
721*0Sstevel@tonic-gate
722*0Sstevel@tonic-gateAs you see, there is one exception, In ASCII.  That way you can assue
723*0Sstevel@tonic-gateGoal #1.  And with Encode Goal #2 is assumed but you still have to be
724*0Sstevel@tonic-gatecareful in such cases mentioned in B<CAVEAT> paragraphs.
725*0Sstevel@tonic-gate
726*0Sstevel@tonic-gateThis utf8 flag is not visible in perl scripts, exactly for the same
727*0Sstevel@tonic-gatereason you cannot (or you I<don't have to>) see if a scalar contains a
728*0Sstevel@tonic-gatestring, integer, or floating point number.   But you can still peek
729*0Sstevel@tonic-gateand poke these if you will.  See the section below.
730*0Sstevel@tonic-gate
731*0Sstevel@tonic-gate=back
732*0Sstevel@tonic-gate
733*0Sstevel@tonic-gate=head2 Messing with Perl's Internals
734*0Sstevel@tonic-gate
735*0Sstevel@tonic-gateThe following API uses parts of Perl's internals in the current
736*0Sstevel@tonic-gateimplementation.  As such, they are efficient but may change.
737*0Sstevel@tonic-gate
738*0Sstevel@tonic-gate=over 2
739*0Sstevel@tonic-gate
740*0Sstevel@tonic-gate=item is_utf8(STRING [, CHECK])
741*0Sstevel@tonic-gate
742*0Sstevel@tonic-gate[INTERNAL] Tests whether the UTF-8 flag is turned on in the STRING.
743*0Sstevel@tonic-gateIf CHECK is true, also checks the data in STRING for being well-formed
744*0Sstevel@tonic-gateUTF-8.  Returns true if successful, false otherwise.
745*0Sstevel@tonic-gate
746*0Sstevel@tonic-gateAs of perl 5.8.1, L<utf8> also has utf8::is_utf8().
747*0Sstevel@tonic-gate
748*0Sstevel@tonic-gate=item _utf8_on(STRING)
749*0Sstevel@tonic-gate
750*0Sstevel@tonic-gate[INTERNAL] Turns on the UTF-8 flag in STRING.  The data in STRING is
751*0Sstevel@tonic-gateB<not> checked for being well-formed UTF-8.  Do not use unless you
752*0Sstevel@tonic-gateB<know> that the STRING is well-formed UTF-8.  Returns the previous
753*0Sstevel@tonic-gatestate of the UTF-8 flag (so please don't treat the return value as
754*0Sstevel@tonic-gateindicating success or failure), or C<undef> if STRING is not a string.
755*0Sstevel@tonic-gate
756*0Sstevel@tonic-gate=item _utf8_off(STRING)
757*0Sstevel@tonic-gate
758*0Sstevel@tonic-gate[INTERNAL] Turns off the UTF-8 flag in STRING.  Do not use frivolously.
759*0Sstevel@tonic-gateReturns the previous state of the UTF-8 flag (so please don't treat the
760*0Sstevel@tonic-gatereturn value as indicating success or failure), or C<undef> if STRING is
761*0Sstevel@tonic-gatenot a string.
762*0Sstevel@tonic-gate
763*0Sstevel@tonic-gate=back
764*0Sstevel@tonic-gate
765*0Sstevel@tonic-gate=head1 SEE ALSO
766*0Sstevel@tonic-gate
767*0Sstevel@tonic-gateL<Encode::Encoding>,
768*0Sstevel@tonic-gateL<Encode::Supported>,
769*0Sstevel@tonic-gateL<Encode::PerlIO>,
770*0Sstevel@tonic-gateL<encoding>,
771*0Sstevel@tonic-gateL<perlebcdic>,
772*0Sstevel@tonic-gateL<perlfunc/open>,
773*0Sstevel@tonic-gateL<perlunicode>,
774*0Sstevel@tonic-gateL<utf8>,
775*0Sstevel@tonic-gatethe Perl Unicode Mailing List E<lt>perl-unicode@perl.orgE<gt>
776*0Sstevel@tonic-gate
777*0Sstevel@tonic-gate=head1 MAINTAINER
778*0Sstevel@tonic-gate
779*0Sstevel@tonic-gateThis project was originated by Nick Ing-Simmons and later maintained
780*0Sstevel@tonic-gateby Dan Kogai E<lt>dankogai@dan.co.jpE<gt>.  See AUTHORS for a full
781*0Sstevel@tonic-gatelist of people involved.  For any questions, use
782*0Sstevel@tonic-gateE<lt>perl-unicode@perl.orgE<gt> so we can all share.
783*0Sstevel@tonic-gate
784*0Sstevel@tonic-gate=cut
785