Attempt to fix core-specific logic in IPC::Open2 tests
[p5sagit/p5-mst-13.2.git] / ext / Encode / Encode.pm
index c52e7c4..4492164 100644 (file)
@@ -1,10 +1,10 @@
 #
-# $Id: Encode.pm,v 2.20 2007/04/22 14:56:12 dankogai Exp dankogai $
+# $Id: Encode.pm,v 2.35 2009/07/13 00:49:38 dankogai Exp $
 #
 package Encode;
 use strict;
 use warnings;
-our $VERSION = sprintf "%d.%02d", q$Revision: 2.20 $ =~ /(\d+)/g;
+our $VERSION = sprintf "%d.%02d", q$Revision: 2.35 $ =~ /(\d+)/g;
 sub DEBUG () { 0 }
 use XSLoader ();
 XSLoader::load( __PACKAGE__, $VERSION );
@@ -36,7 +36,8 @@ our @EXPORT_OK = (
 
 our %EXPORT_TAGS = (
     all          => [ @EXPORT,    @EXPORT_OK ],
-    fallbacks    => [@FB_CONSTS],
+    default      => [ @EXPORT ],
+    fallbacks    => [ @FB_CONSTS ],
     fallback_all => [ @FB_CONSTS, @FB_FLAGS ],
 );
 
@@ -50,7 +51,14 @@ use Encode::Alias;
 our %Encoding;
 our %ExtModule;
 require Encode::Config;
-eval { require Encode::ConfigLocal };
+#  See
+#  https://bugzilla.redhat.com/show_bug.cgi?id=435505#c2
+#  to find why sig handers inside eval{} are disabled.
+eval {
+    local $SIG{__DIE__};
+    local $SIG{__WARN__};
+    require Encode::ConfigLocal;
+};
 
 sub encodings {
     my $class = shift;
@@ -138,13 +146,17 @@ sub encode($$;$) {
     return undef unless defined $string;
     $string .= '' if ref $string;    # stringify;
     $check ||= 0;
+    unless ( defined $name ) {
+        require Carp;
+        Carp::croak("Encoding name should not be undef");
+    }
     my $enc = find_encoding($name);
     unless ( defined $enc ) {
         require Carp;
         Carp::croak("Unknown encoding '$name'");
     }
     my $octets = $enc->encode( $string, $check );
-    $_[1] = $string if $check and !( $check & LEAVE_SRC() );
+    $_[1] = $string if $check and !ref $check and !( $check & LEAVE_SRC() );
     return $octets;
 }
 *str2bytes = \&encode;
@@ -160,7 +172,7 @@ sub decode($$;$) {
         Carp::croak("Unknown encoding '$name'");
     }
     my $string = $enc->decode( $octets, $check );
-    $_[1] = $octets if $check and !( $check & LEAVE_SRC() );
+    $_[1] = $octets if $check and !ref $check and !( $check & LEAVE_SRC() );
     return $string;
 }
 *bytes2str = \&decode;
@@ -499,6 +511,20 @@ but only #2 turns UTF8 flag on.  #1 is equivalent to
 
 See L</"The UTF8 flag"> below.
 
+Also note that
+
+  from_to($octets, $from, $to, $check);
+
+is equivalent to
+
+  $octets = encode($to, decode($from, $octets), $check);
+
+Yes, it does not respect the $check during decoding.  It is
+deliberately done that way.  If you need minute control, C<decode>
+then C<encode> as follows;
+
+  $octets = encode($to, decode($from, $octets, $check_from), $check_to);
+
 =item $octets = encode_utf8($string);
 
 Equivalent to C<$octets = encode("utf8", $string);> The characters
@@ -565,6 +591,22 @@ exported via C<use Encode qw(resolve_alias)>.
 
 See L<Encode::Alias> for details.
 
+=head2 Finding IANA Character Set Registry names
+
+The canonical name of a given encoding does not necessarily agree with
+IANA IANA Character Set Registry, commonly seen as C<< Content-Type:
+text/plain; charset=I<whatever> >>.  For most cases canonical names
+work but sometimes it does not (notably 'utf-8-strict').
+
+Therefore as of Encode version 2.21, a new method C<mime_name()> is added.
+
+  use Encode;
+  my $enc = find_encoding('UTF-8');
+  warn $enc->name;      # utf-8-strict
+  warn $enc->mime_name; # UTF-8
+
+See also:  L<Encode::Encoding>
+
 =head1 Encoding via PerlIO
 
 If your perl supports I<PerlIO> (which is the default), you can use a
@@ -704,7 +746,7 @@ you're not interested in this, then bitwise-or the bitmask with it.
 
 =back
 
-=Head2 coderef for CHECK
+=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
@@ -825,6 +867,8 @@ B<know> that the STRING is well-formed UTF-8.  Returns the previous
 state of the UTF8 flag (so please don't treat the return value as
 indicating success or failure), or C<undef> if STRING is not a string.
 
+This function does not work on tainted values.
+
 =item _utf8_off(STRING)
 
 [INTERNAL] Turns off the UTF8 flag in STRING.  Do not use frivolously.
@@ -832,6 +876,8 @@ Returns the previous state of the UTF8 flag (so please don't treat the
 return value as indicating success or failure), or C<undef> if STRING is
 not a string.
 
+This function does not work on tainted values.
+
 =back
 
 =head1 UTF-8 vs. utf8 vs. UTF8
@@ -892,7 +938,7 @@ L<Encode::PerlIO>,
 L<encoding>,
 L<perlebcdic>,
 L<perlfunc/open>,
-L<perlunicode>,
+L<perlunicode>, L<perluniintro>, L<perlunifaq>, L<perlunitut>
 L<utf8>,
 the Perl Unicode Mailing List E<lt>perl-unicode@perl.orgE<gt>