#
-# $Id: Encode.pm,v 2.9 2004/12/03 19:16:40 dankogai Exp $
+# $Id: Encode.pm,v 2.12 2005/09/08 14:17:17 dankogai Exp dankogai $
#
package Encode;
use strict;
-our $VERSION = do { my @r = (q$Revision: 2.9 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
+our $VERSION = sprintf "%d.%02d", q$Revision: 2.12 $ =~ /(\d+)/g;
sub DEBUG () { 0 }
use XSLoader ();
XSLoader::load(__PACKAGE__, $VERSION);
);
our @FB_FLAGS = qw(DIE_ON_ERR WARN_ON_ERR RETURN_ON_ERR LEAVE_SRC
- PERLQQ HTMLCREF XMLCREF);
+ PERLQQ HTMLCREF XMLCREF STOP_AT_PARTIAL);
our @FB_CONSTS = qw(FB_DEFAULT FB_CROAK FB_QUIET FB_WARN
FB_PERLQQ FB_HTMLCREF FB_XMLCREF);
Carp::croak("Unknown encoding '$name'");
}
my $octets = $enc->encode($string,$check);
- $_[1] = $string if $check;
+ $_[1] = $string if $check and !($check & LEAVE_SRC());
return $octets;
}
Carp::croak("Unknown encoding '$name'");
}
my $string = $enc->decode($octets,$check);
- $_[1] = $octets if $check;
+ $_[1] = $octets if $check and !($check & LEAVE_SRC());
return $string;
}
if ($check){
return decode("utf8", $str, $check);
}else{
- return undef unless utf8::decode($str);
+ return decode("utf8", $str);
return $str;
}
}
};
$Encode::Encoding{utf8} =
bless {Name => "utf8"} => "Encode::utf8";
+ $Encode::Encoding{"utf-8-strict"} =
+ bless {Name => "utf-8-strict", strict_utf8 => 1 } => "Encode::utf8";
}
}
the result is always off, even when it contains completely valid utf8
string. See L</"The UTF-8 flag"> below.
-If the $string is C<undef> or a reference then C<undef> is returned.
+If the $string is C<undef> then C<undef> is returned.
=item $string = decode(ENCODING, $octets [, CHECK])
ASCII data (or EBCDIC on EBCDIC machines). See L</"The UTF-8 flag">
below.
-If the $string is C<undef> or a reference then C<undef> is returned.
+If the $string is C<undef> then C<undef> is returned.
=item [$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])
=head1 Handling Malformed Data
-The optional I<CHECK> argument is used as follows. When you omit it,
-Encode::FB_DEFAULT ( == 0 ) is assumed.
+The optional I<CHECK> argument tells Encode what to do when it
+encounters malformed data. Without CHECK, Encode::FB_DEFAULT ( == 0 )
+is assumed.
+
+As of version 2.12 Encode supports coderef values for CHECK. See below.
=over 2
-=item B<NOTE:> Not all encoding suppport this feature
+=item B<NOTE:> Not all encoding support this feature
Some encodings ignore I<CHECK> argument. For example,
L<Encode::Unicode> ignores I<CHECK> and it always croaks on error.
C<\x{I<HHHH>}>, HTML uses C<&#I<NNN>;> where I<NNN> is a decimal number and
XML uses C<&#xI<HHHH>;> where I<HHHH> is the hexadecimal number.
+In Encode 2.10 or later, C<LEAVE_SRC> is also implied.
+
=item The bitmask
These modes are actually set via a bitmask. Here is how the FB_XX
DIE_ON_ERR 0x0001 X
WARN_ON_ERR 0x0002 X
RETURN_ON_ERR 0x0004 X X
- LEAVE_SRC 0x0008
+ LEAVE_SRC 0x0008 X
PERLQQ 0x0100 X
HTMLCREF 0x0200
XMLCREF 0x0400
=back
-=head2 Unimplemented fallback schemes
+=head2 coderef for CHECK
+
+As of Encode 2.12 CHECK can also be a code reference which takes the
+ord value of unmapped caharacter as an argument and returns a string
+that represents the fallback character. For instance,
-In the future, you will be able to use a code reference to a callback
-function for the value of I<CHECK> but its API is still undecided.
+ $ascii = encode("ascii", $utf8, sub{ sprintf "<U+%04X>", shift });
-The fallback scheme does not work on EBCDIC platforms.
+Acts like FB_PERLQQ but E<lt>U+I<XXXX>E<gt> is used instead of
+\x{I<XXXX>}.
=head1 Defining Encodings
In any other Encoding ON
---------------------------------------------
-As you see, there is one exception, In ASCII. That way you can assue
+As you see, there is one exception, In ASCII. That way you can assume
Goal #1. And with Encode Goal #2 is assumed but you still have to be
careful in such cases mentioned in B<CAVEAT> paragraphs.
=back
+=head1 UTF-8 vs. utf8
+
+ ....We now view strings not as sequences of bytes, but as sequences
+ of numbers in the range 0 .. 2**32-1 (or in the case of 64-bit
+ computers, 0 .. 2**64-1) -- Programming Perl, 3rd ed.
+
+That has been the perl's notion of UTF-8 but official UTF-8 is more
+strict; Its ranges is much narrower (0 .. 10FFFF), some sequences are
+not allowed (i.e. Those used in the surrogate pair, 0xFFFE, et al).
+
+Now that is overruled by Larry Wall himself.
+
+ From: Larry Wall <larry@wall.org>
+ Date: December 04, 2004 11:51:58 JST
+ To: perl-unicode@perl.org
+ Subject: Re: Make Encode.pm support the real UTF-8
+ Message-Id: <20041204025158.GA28754@wall.org>
+
+ On Fri, Dec 03, 2004 at 10:12:12PM +0000, Tim Bunce wrote:
+ : I've no problem with 'utf8' being perl's unrestricted uft8 encoding,
+ : but "UTF-8" is the name of the standard and should give the
+ : corresponding behaviour.
+
+ For what it's worth, that's how I've always kept them straight in my
+ head.
+
+ Also for what it's worth, Perl 6 will mostly default to strict but
+ make it easy to switch back to lax.
+
+ Larry
+
+Do you copy? As of Perl 5.8.7, B<UTF-8> means strict, official UTF-8
+while B<utf8> means liberal, lax, version thereof. And Encode version
+2.10 or later thus groks the difference between C<UTF-8> and C"utf8".
+
+ encode("utf8", "\x{FFFF_FFFF}", 1); # okay
+ encode("UTF-8", "\x{FFFF_FFFF}", 1); # croaks
+
+C<UTF-8> in Encode is actually a canonical name for C<utf-8-strict>.
+Yes, the hyphen between "UTF" and "8" is important. Without it Encode
+goes "liberal"
+
+ find_encoding("UTF-8")->name # is 'utf-8-strict'
+ find_encoding("utf-8")->name # ditto. names are case insensitive
+ find_encoding("utf8")->name # ditto. "_" are treated as "-"
+ find_encoding("UTF8")->name # is 'utf8'.
+
+
=head1 SEE ALSO
L<Encode::Encoding>,