dslinux/user/perl/ext/Encode/lib/Encode Alias.pm CJKConstants.pm Config.pm Encoder.pm Encoding.pm Guess.pm PerlIO.pod Supported.pod

cayenne dslinux_cayenne at user.in-berlin.de
Tue Dec 5 05:26:38 CET 2006


Update of /cvsroot/dslinux/dslinux/user/perl/ext/Encode/lib/Encode
In directory antilope:/tmp/cvs-serv7729/ext/Encode/lib/Encode

Added Files:
	Alias.pm CJKConstants.pm Config.pm Encoder.pm Encoding.pm 
	Guess.pm PerlIO.pod Supported.pod 
Log Message:
Adding fresh perl source to HEAD to branch from

--- NEW FILE: Guess.pm ---
package Encode::Guess;
use strict;

use Encode qw(:fallbacks find_encoding);
our $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

my $Canon = 'Guess';
sub DEBUG () { 0 }
our %DEF_SUSPECTS = map { $_ => find_encoding($_) } qw(ascii utf8);
$Encode::Encoding{$Canon} = 
    bless { 
	   Name       => $Canon,
	   Suspects => { %DEF_SUSPECTS },
	  } => __PACKAGE__;

use base qw(Encode::Encoding);
sub needs_lines { 1 }
sub perlio_ok { 0 }

our @EXPORT = qw(guess_encoding);
our $NoUTFAutoGuess = 0;
our $UTF8_BOM = pack("C3", 0xef, 0xbb, 0xbf);

sub import { # Exporter not used so we do it on our own
    my $callpkg = caller;
    for my $item (@EXPORT){
	no strict 'refs';
	*{"$callpkg\::$item"} = \&{"$item"};
    }
    set_suspects(@_);
}

sub set_suspects{
    my $class = shift;
    my $self = ref($class) ? $class : $Encode::Encoding{$Canon};
    $self->{Suspects} = { %DEF_SUSPECTS };
    $self->add_suspects(@_);
}

sub add_suspects{
    my $class = shift;
    my $self = ref($class) ? $class : $Encode::Encoding{$Canon};
    for my $c (@_){
	my $e = find_encoding($c) or die "Unknown encoding: $c";
	$self->{Suspects}{$e->name} = $e;
	DEBUG and warn "Added: ", $e->name;
    }
}

sub decode($$;$){
    my ($obj, $octet, $chk) = @_;
    my $guessed = guess($obj, $octet);
    unless (ref($guessed)){
	require Carp;
	Carp::croak($guessed);
    }
    my $utf8 = $guessed->decode($octet, $chk);
    $_[1] = $octet if $chk;
    return $utf8;
}

sub guess_encoding{
    guess($Encode::Encoding{$Canon}, @_);
}

sub guess {
    my $class = shift;
    my $obj   = ref($class) ? $class : $Encode::Encoding{$Canon};
    my $octet = shift;

    # sanity check
    return unless defined $octet and length $octet;

    # cheat 0: utf8 flag;
    if ( Encode::is_utf8($octet) ) {
	return find_encoding('utf8') unless $NoUTFAutoGuess;
	Encode::_utf8_off($octet);
    }
    # cheat 1: BOM
    use Encode::Unicode;
    unless ($NoUTFAutoGuess) {
	my $BOM = pack('C3', unpack("C3", $octet));
	return find_encoding('utf8')
	    if (defined $BOM and $BOM eq $UTF8_BOM);
	$BOM = unpack('N', $octet);
	return find_encoding('UTF-32')
	    if (defined $BOM and ($BOM == 0xFeFF or $BOM == 0xFFFe0000));
	$BOM = unpack('n', $octet);
	return find_encoding('UTF-16')
	    if (defined $BOM and ($BOM == 0xFeFF or $BOM == 0xFFFe));
	if ($octet =~ /\x00/o){ # if \x00 found, we assume UTF-(16|32)(BE|LE)
	    my $utf;
	    my ($be, $le) = (0, 0);
	    if ($octet =~ /\x00\x00/o){ # UTF-32(BE|LE) assumed
		$utf = "UTF-32";
		for my $char (unpack('N*', $octet)){
		    $char & 0x0000ffff and $be++;
		    $char & 0xffff0000 and $le++;
		}
	    }else{ # UTF-16(BE|LE) assumed
		$utf = "UTF-16";
		for my $char (unpack('n*', $octet)){
		    $char & 0x00ff and $be++;
		    $char & 0xff00 and $le++;
		}
	    }
	    DEBUG and warn "$utf, be == $be, le == $le";
	    $be == $le 
		and return
		    "Encodings ambiguous between $utf BE and LE ($be, $le)";
	    $utf .= ($be > $le) ? 'BE' : 'LE';
	    return find_encoding($utf);
	}
    }
    my %try =  %{$obj->{Suspects}};
    for my $c (@_){
	my $e = find_encoding($c) or die "Unknown encoding: $c";
	$try{$e->name} = $e;
	DEBUG and warn "Added: ", $e->name;
    }
    my $nline = 1;
    for my $line (split /\r\n?|\n/, $octet){
	# cheat 2 -- \e in the string
	if ($line =~ /\e/o){
	    my @keys = keys %try;
	    delete @try{qw/utf8 ascii/};
	    for my $k (@keys){
		ref($try{$k}) eq 'Encode::XS' and delete $try{$k};
	    }
	}
	my %ok = %try;
	# warn join(",", keys %try);
	for my $k (keys %try){
	    my $scratch = $line;
	    $try{$k}->decode($scratch, FB_QUIET);
	    if ($scratch eq ''){
		DEBUG and warn sprintf("%4d:%-24s ok\n", $nline, $k);
	    }else{
		use bytes ();
		DEBUG and 
		    warn sprintf("%4d:%-24s not ok; %d bytes left\n", 
				 $nline, $k, bytes::length($scratch));
		delete $ok{$k};
	    }
	}
	%ok or return "No appropriate encodings found!";
	if (scalar(keys(%ok)) == 1){
	    my ($retval) = values(%ok);
	    return $retval;
	}
	%try = %ok; $nline++;
    }
    $try{ascii} or 
	return  "Encodings too ambiguous: ", join(" or ", keys %try);
    return $try{ascii};
}



1;
__END__

=head1 NAME

Encode::Guess -- Guesses encoding from data

=head1 SYNOPSIS

  # if you are sure $data won't contain anything bogus

  use Encode;
  use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;
  my $utf8 = decode("Guess", $data);
  my $data = encode("Guess", $utf8);   # this doesn't work!

  # more elaborate way
  use Encode::Guess;
  my $enc = guess_encoding($data, qw/euc-jp shiftjis 7bit-jis/);
  ref($enc) or die "Can't guess: $enc"; # trap error this way
  $utf8 = $enc->decode($data);
  # or
  $utf8 = decode($enc->name, $data)

=head1 ABSTRACT

Encode::Guess enables you to guess in what encoding a given data is
encoded, or at least tries to.  

=head1 DESCRIPTION

By default, it checks only ascii, utf8 and UTF-16/32 with BOM.

  use Encode::Guess; # ascii/utf8/BOMed UTF

To use it more practically, you have to give the names of encodings to
check (I<suspects> as follows).  The name of suspects can either be
canonical names or aliases.

CAVEAT: Unlike UTF-(16|32), BOM in utf8 is NOT AUTOMATICALLY STRIPPED.

 # tries all major Japanese Encodings as well
  use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;

If the C<$Encode::Guess::NoUTFAutoGuess> variable is set to a true
value, no heuristics will be applied to UTF8/16/32, and the result
will be limited to the suspects and C<ascii>.

=over 4

=item Encode::Guess->set_suspects

You can also change the internal suspects list via C<set_suspects>
method. 

  use Encode::Guess;
  Encode::Guess->set_suspects(qw/euc-jp shiftjis 7bit-jis/);

=item Encode::Guess->add_suspects

Or you can use C<add_suspects> method.  The difference is that
C<set_suspects> flushes the current suspects list while
C<add_suspects> adds.

  use Encode::Guess;
  Encode::Guess->add_suspects(qw/euc-jp shiftjis 7bit-jis/);
  # now the suspects are euc-jp,shiftjis,7bit-jis, AND
  # euc-kr,euc-cn, and big5-eten
  Encode::Guess->add_suspects(qw/euc-kr euc-cn big5-eten/);

=item Encode::decode("Guess" ...)

When you are content with suspects list, you can now

  my $utf8 = Encode::decode("Guess", $data);

=item Encode::Guess->guess($data)

But it will croak if:

=over

=item *

Two or more suspects remain

=item *

No suspects left

=back

So you should instead try this;

  my $decoder = Encode::Guess->guess($data);

On success, $decoder is an object that is documented in
L<Encode::Encoding>.  So you can now do this;

  my $utf8 = $decoder->decode($data);

On failure, $decoder now contains an error message so the whole thing
would be as follows;

  my $decoder = Encode::Guess->guess($data);
  die $decoder unless ref($decoder);
  my $utf8 = $decoder->decode($data);

=item guess_encoding($data, [, I<list of suspects>])

You can also try C<guess_encoding> function which is exported by
default.  It takes $data to check and it also takes the list of
suspects by option.  The optional suspect list is I<not reflected> to
the internal suspects list.

  my $decoder = guess_encoding($data, qw/euc-jp euc-kr euc-cn/);
  die $decoder unless ref($decoder);
  my $utf8 = $decoder->decode($data);
  # check only ascii and utf8
  my $decoder = guess_encoding($data);

=back

=head1 CAVEATS

=over 4

=item *

Because of the algorithm used, ISO-8859 series and other single-byte
encodings do not work well unless either one of ISO-8859 is the only
one suspect (besides ascii and utf8).

  use Encode::Guess;
  # perhaps ok
  my $decoder = guess_encoding($data, 'latin1');
  # definitely NOT ok
  my $decoder = guess_encoding($data, qw/latin1 greek/);

The reason is that Encode::Guess guesses encoding by trial and error.
It first splits $data into lines and tries to decode the line for each
suspect.  It keeps it going until all but one encoding is eliminated
out of suspects list.  ISO-8859 series is just too successful for most
cases (because it fills almost all code points in \x00-\xff).

=item *

Do not mix national standard encodings and the corresponding vendor
encodings.

  # a very bad idea
  my $decoder
     = guess_encoding($data, qw/shiftjis MacJapanese cp932/);

The reason is that vendor encoding is usually a superset of national
standard so it becomes too ambiguous for most cases.

=item *

On the other hand, mixing various national standard encodings
automagically works unless $data is too short to allow for guessing.

 # This is ok if $data is long enough
 my $decoder =  
  guess_encoding($data, qw/euc-cn
                           euc-jp shiftjis 7bit-jis
                           euc-kr
                           big5-eten/);

=item *

DO NOT PUT TOO MANY SUSPECTS!  Don't you try something like this!

  my $decoder = guess_encoding($data, 
                               Encode->encodings(":all"));

=back

It is, after all, just a guess.  You should alway be explicit when it
comes to encodings.  But there are some, especially Japanese,
environment that guess-coding is a must.  Use this module with care. 

=head1 TO DO

Encode::Guess does not work on EBCDIC platforms.

=head1 SEE ALSO

L<Encode>, L<Encode::Encoding>

=cut


--- NEW FILE: Supported.pod ---
=head1 NAME

Encode::Supported -- Encodings supported by Encode

=head1 DESCRIPTION

=head2 Encoding Names

Encoding names are case insensitive. White space in names
is ignored.  In addition, an encoding may have aliases.
Each encoding has one "canonical" name.  The "canonical"
name is chosen from the names of the encoding by picking
the first in the following sequence (with a few exceptions).

=over 4

=item *

The name used by the Perl community.  That includes 'utf8' and 'ascii'.
Unlike aliases, canonical names directly reach the method so such
frequently used words like 'utf8' don't need to do alias lookups.

=item *

The MIME name as defined in IETF RFCs.  This includes all "iso-"s.

=item * 

The name in the IANA registry.

=item *

The name used by the organization that defined it.

=back

In case I<de jure> canonical names differ from that of the Encode
module, they are always aliased if it ever be implemented.  So you can
safely tell if a given encoding is implemented or not just by passing 
the canonical name.

Because of all the alias issues, and because in the general case 
encodings have state, "Encode" uses an encoding object internally 
once an operation is in progress.

=head1 Supported Encodings

As of Perl 5.8.0, at least the following encodings are recognized.
Note that unless otherwise specified, they are all case insensitive
(via alias) and all occurrence of spaces are replaced with '-'.
In other words, "ISO 8859 1" and "iso-8859-1" are identical.

Encodings are categorized and implemented in several different modules
but you don't have to C<use Encode::XX> to make them available for
most cases.  Encode.pm will automatically load those modules on demand.

=head2 Built-in Encodings

The following encodings are always available.

  Canonical     Aliases                      Comments & References
  ----------------------------------------------------------------
  ascii         US-ascii ISO-646-US                         [ECMA]
  ascii-ctrl			                  Special Encoding
  iso-8859-1    latin1                                       [ISO]
  null				                  Special Encoding
  utf8          UTF-8                                    [RFC2279]
  ----------------------------------------------------------------

I<null> and I<ascii-ctrl> are special.  "null" fails for all character
so when you set fallback mode to PERLQQ, HTMLCREF or XMLCREF, ALL
CHARACTERS will fall back to character references.  Ditto for
"ascii-ctrl" except for control characters.  For fallback modes, see
L<Encode>.

=head2 Encode::Unicode -- other Unicode encodings

Unicode coding schemes other than native utf8 are supported by
Encode::Unicode, which will be autoloaded on demand.

  ----------------------------------------------------------------
  UCS-2BE       UCS-2, iso-10646-1                      [IANA, UC]
  UCS-2LE                                                     [UC]
  UTF-16                                                      [UC]
  UTF-16BE                                                    [UC]
  UTF-16LE                                                    [UC]
  UTF-32                                                      [UC]
  UTF-32BE	UCS-4                                         [UC]
  UTF-32LE                                                    [UC]
  UTF-7                                                  [RFC2152]
  ----------------------------------------------------------------

To find how (UCS-2|UTF-(16|32))(LE|BE)? differ from one another,
see L<Encode::Unicode>. 

UTF-7 is a special encoding which "re-encodes" UTF-16BE into a 7-bit
encoding.  It is implemented seperately by Encode::Unicode::UTF7.

=head2 Encode::Byte -- Extended ASCII

Encode::Byte implements most single-byte encodings except for
Symbols and EBCDIC. The following encodings are based on single-byte
encodings implemented as extended ASCII.  Most of them map
\x80-\xff (upper half) to non-ASCII characters.

=over 4

=item ISO-8859 and corresponding vendor mappings

Since there are so many, they are presented in table format with
languages and corresponding encoding names by vendors.  Note that
the table is sorted in order of ISO-8859 and the corresponding vendor
mappings are slightly different from that of ISO.  See
L<http://czyborra.com/charsets/iso8859.html> for details.

  Lang/Regions  ISO/Other Std.  DOS     Windows Macintosh  Others
  ----------------------------------------------------------------
  N. America    (ASCII)         cp437        AdobeStandardEncoding
                                cp863 (DOSCanadaF)
  W. Europe     iso-8859-1      cp850   cp1252  MacRoman  nextstep
                                                         hp-roman8
                                cp860 (DOSPortuguese)
  Cntrl. Europe iso-8859-2      cp852   cp1250  MacCentralEurRoman
                                                MacCroatian
                                                MacRomanian
                                                MacRumanian
  Latin3[1]     iso-8859-3      
  Latin4[2]     iso-8859-4              
  Cyrillics     iso-8859-5      cp855   cp1251  MacCyrillic
    (See also next section)     cp866           MacUkrainian
  Arabic        iso-8859-6      cp864   cp1256  MacArabic
                                cp1006          MacFarsi
  Greek         iso-8859-7      cp737   cp1253  MacGreek
                                cp869 (DOSGreek2)
  Hebrew        iso-8859-8      cp862   cp1255  MacHebrew
  Turkish       iso-8859-9      cp857   cp1254  MacTurkish
  Nordics       iso-8859-10     cp865
                                cp861           MacIcelandic
                                                MacSami
  Thai          iso-8859-11[3]  cp874           MacThai
  (iso-8859-12 is nonexistent. Reserved for Indics?)
  Baltics       iso-8859-13     cp775           cp1257
  Celtics       iso-8859-14
  Latin9 [4]    iso-8859-15
  Latin10       iso-8859-16
  Vietnamese    viscii                  cp1258  MacVietnamese
  ----------------------------------------------------------------

  [1] Esperanto, Maltese, and Turkish. Turkish is now on 8859-9.
  [2] Baltics.  Now on 8859-10, except for Latvian.
  [3] TIS 620 +  Non-Breaking Space (0xA0 / U+00A0)
  [4] Nicknamed Latin0; the Euro sign as well as French and Finnish
      letters that are missing from 8859-1 were added.

All cp* are also available as ibm-*, ms-*, and windows-* .  See also
L<http://czyborra.com/charsets/codepages.html>.

Macintosh encodings don't seem to be registered in such entities as
IANA.  "Canonical" names in Encode are based upon Apple's Tech Note
1150.  See L<http://developer.apple.com/technotes/tn/tn1150.html> 
for details.

=item KOI8 - De Facto Standard for the Cyrillic world

Though ISO-8859 does have ISO-8859-5, the KOI8 series is far more
popular in the Net.   L<Encode> comes with the following KOI charsets.
For gory details, see L<http://czyborra.com/charsets/cyrillic.html>

  ----------------------------------------------------------------
  koi8-f                                        
  koi8-r cp878                                           [RFC1489]
  koi8-u                                                 [RFC2319]
  ----------------------------------------------------------------

=item gsm0338 - Hentai Latin 1

GSM0338 is for GSM handsets. Though it shares alphanumerals with
ASCII, control character ranges and other parts are mapped very
differently, mainly to store Greek characters.  There are also escape
sequences (starting with 0x1B) to cover e.g. the Euro sign.  Some
special cases like a trailing 0x00 byte or a lone 0x1B byte are not
well-defined and decode() will return an empty string for them.
One possible workaround is

   $gsm =~ s/\x00\z/\x00\x00/;
   $uni = decode("gsm0338", $gsm);
   $uni .= "\xA0" if $gsm =~ /\x1B\z/;

Note that the Encode implementation of GSM0338 does not implement the
reuse of Latin capital letters as Greek capital letters (for example,
the 0x5A is U+005A (LATIN CAPITAL LETTER Z), not U+0396 (GREEK CAPITAL
LETTER ZETA).

The GSM0338 is also covered in Encode::Byte even though it is not
an "extended ASCII" encoding.

=back

=head2 CJK: Chinese, Japanese, Korean (Multibyte)

Note that Vietnamese is listed above.  Also read "Encoding vs Charset"
below.  Also note that these are implemented in distinct modules by
countries, due to the size concerns (simplified Chinese is mapped
to 'CN', continental China, while traditional Chinese is mapped to
'TW', Taiwan).  Please refer to their respective documentation pages.

=over 4

=item Encode::CN -- Continental China

  Standard      DOS/Win Macintosh                Comment/Reference
  ----------------------------------------------------------------
  euc-cn [1]            MacChineseSimp
  (gbk)         cp936 [2]
  gb12345-raw                      { GB12345 without CES }
  gb2312-raw                       { GB2312  without CES }
  hz
  iso-ir-165
  ----------------------------------------------------------------

  [1] GB2312 is aliased to this.  See L<Microsoft-related naming mess>
  [2] gbk is aliased to this.  See L<Microsoft-related naming mess>

=item Encode::JP -- Japan

  Standard      DOS/Win Macintosh                Comment/Reference
  ----------------------------------------------------------------
  euc-jp
  shiftjis      cp932   macJapanese
  7bit-jis
  iso-2022-jp                                            [RFC1468]
  iso-2022-jp-1                                          [RFC2237]
  jis0201-raw  { JIS X 0201 (roman + halfwidth kana) without CES }
  jis0208-raw  { JIS X 0208 (Kanji + fullwidth kana) without CES }
  jis0212-raw  { JIS X 0212 (Extended Kanji)         without CES }
  ----------------------------------------------------------------

=item Encode::KR -- Korea

  Standard      DOS/Win Macintosh                Comment/Reference
  ----------------------------------------------------------------
  euc-kr                MacKorean                        [RFC1557]
                cp949 [1]                    
  iso-2022-kr                                            [RFC1557]
  johab                                  [KS X 1001:1998, Annex 3]
  ksc5601-raw                              { KSC5601 without CES }
  ----------------------------------------------------------------

  [1] ks_c_5601-1987, (x-)?windows-949, and uhc are aliased to this.
  See below.

=item Encode::TW -- Taiwan

  Standard      DOS/Win Macintosh                Comment/Reference
  ----------------------------------------------------------------
  big5-eten     cp950   MacChineseTrad {big5 aliased to big5-eten}
  big5-hkscs                              
  ----------------------------------------------------------------

=item Encode::HanExtra -- More Chinese via CPAN

Due to the size concerns, additional Chinese encodings below are
distributed separately on CPAN, under the name Encode::HanExtra.

  Standard      DOS/Win Macintosh                Comment/Reference
  ----------------------------------------------------------------
  big5ext                                   CMEX's Big5e Extension
  big5plus                                  CMEX's Big5+ Extension
  cccii         Chinese Character Code for Information Interchange
  euc-tw                             EUC (Extended Unix Character)
  gb18030                          GBK with Traditional Characters
  ----------------------------------------------------------------

=item Encode::JIS2K -- JIS X 0213 encodings via CPAN

Due to size concerns, additional Japanese encodings below are
distributed separately on CPAN, under the name Encode::JIS2K.

  Standard      DOS/Win Macintosh                Comment/Reference
  ----------------------------------------------------------------
  euc-jisx0213
  shiftjisx0123
  iso-2022-jp-3
  jis0213-1-raw
  jis0213-2-raw
  ----------------------------------------------------------------

=back

=head2 Miscellaneous encodings

=over 4

=item Encode::EBCDIC

See L<perlebcdic> for details.

  ----------------------------------------------------------------
  cp37
  cp500  
  cp875  
  cp1026  
  cp1047  
  posix-bc
  ----------------------------------------------------------------

=item Encode::Symbols

For symbols  and dingbats.

  ----------------------------------------------------------------
  symbol
  dingbats
  MacDingbats
  AdobeZdingbat
  AdobeSymbol
  ----------------------------------------------------------------

=item Encode::MIME::Header

Strictly speaking, MIME header encoding documented in RFC 2047 is more
of encapsulation than encoding.  However, their support in modern
world is imperative so they are supported.

  ----------------------------------------------------------------
  MIME-Header                                            [RFC2047]
  MIME-B                                                 [RFC2047]
  MIME-Q                                                 [RFC2047]
  ----------------------------------------------------------------

=item Encode::Guess

This one is not a name of encoding but a utility that lets you pick up
the most appropriate encoding for a data out of given I<suspects>.  See
L<Encode::Guess> for details.

=back

=head1 Unsupported encodings

The following encodings are not supported as yet; some because they
are rarely used, some because of technical difficulties.  They may
be supported by external modules via CPAN in the future, however.

=over 4

=item   ISO-2022-JP-2 [RFC1554]

Not very popular yet.  Needs Unicode Database or equivalent to
implement encode() (because it includes JIS X 0208/0212, KSC5601, and
GB2312 simultaneously, whose code points in Unicode overlap.  So you
need to lookup the database to determine to what character set a given
Unicode character should belong). 

=item ISO-2022-CN [RFC1922]

Not very popular.  Needs CNS 11643-1 and -2 which are not available in
this module.  CNS 11643 is supported (via euc-tw) in Encode::HanExtra.
Autrijus Tang may add support for this encoding in his module in future.

=item Various HP-UX encodings

The following are unsupported due to the lack of mapping data.

  '8'  - arabic8, greek8, hebrew8, kana8, thai8, and turkish8
  '15' - japanese15, korean15, and roi15

=item Cyrillic encoding ISO-IR-111

Anton Tagunov doubts its usefulness.

=item ISO-8859-8-1 [Hebrew]

None of the Encode team knows Hebrew enough (ISO-8859-8, cp1255 and
MacHebrew are supported because and just because there were mappings
available at L<http://www.unicode.org/>).  Contributions welcome.

=item ISIRI 3342, Iran System, ISIRI 2900 [Farsi]

Ditto.

=item Thai encoding TCVN

Ditto.

=item Vietnamese encodings VPS

Though Jungshik Shin has reported that Mozilla supports this encoding,
it was too late before 5.8.0 for us to add it.  In the future, it
may be available via a separate module.  See
L<http://lxr.mozilla.org/seamonkey/source/intl/uconv/ucvlatin/vps.uf>
and
L<http://lxr.mozilla.org/seamonkey/source/intl/uconv/ucvlatin/vps.ut>
if you are interested in helping us.

=item Various Mac encodings

The following are unsupported due to the lack of mapping data. 

  MacArmenian,  MacBengali,   MacBurmese,   MacEthiopic
  MacExtArabic, MacGeorgian,  MacKannada,   MacKhmer
  MacLaotian,   MacMalayalam, MacMongolian, MacOriya
  MacSinhalese, MacTamil,     MacTelugu,    MacTibetan
  MacVietnamese

The rest which are already available are based upon the vendor mappings
at L<http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/> .

=item (Mac) Indic encodings

The maps for the following are available at L<http://www.unicode.org/>
but remain unsupport because those encodings need algorithmical
approach, currently unsupported by F<enc2xs>:

  MacDevanagari
  MacGurmukhi
  MacGujarati

For details, please see C<Unicode mapping issues and notes:> at
L<http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/DEVANAGA.TXT> .

I believe this issue is prevalent not only for Mac Indics but also in
other Indic encodings, but the above were the only Indic encodings
maps that I could find at L<http://www.unicode.org/> .

=back

=head1 Encoding vs. Charset -- terminology

We are used to using the term (character) I<encoding> and I<character
set> interchangeably.  But just as confusing the terms byte and
character is dangerous and the terms should be differentiated when
needed, we need to differentiate I<encoding> and I<character set>.

To understand that, here is a description of how we make computers
grok our characters.

=over 4

=item *

First we start with which characters to include.  We call this
collection of characters I<character repertoire>.

=item *

Then we have to give each character a unique ID so your computer can
tell the difference between 'a' and 'A'.  This itemized character
repertoire is now a I<character set>.

=item *

If your computer can grow the character set without further
processing, you can go ahead and use it.  This is called a I<coded
character set> (CCS) or I<raw character encoding>.  ASCII is used this
way for most cases.

=item *

But in many cases, especially multi-byte CJK encodings, you have to
tweak a little more.  Your network connection may not accept any data
with the Most Significant Bit set, and your computer may not be able to
tell if a given byte is a whole character or just half of it.  So you
have to I<encode> the character set to use it.

A I<character encoding scheme> (CES) determines how to encode a given
character set, or a set of multiple character sets.  7bit ISO-2022 is
an example of a CES.  You switch between character sets via I<escape
sequences>.

=back

Technically, or mathematically, speaking, a character set encoded in
such a CES that maps character by character may form a CCS.  EUC is such
an example.  The CES of EUC is as follows:

=over 4

=item *

Map ASCII unchanged.

=item *

Map such a character set that consists of 94 or 96 powered by N
members by adding 0x80 to each byte.

=item *

You can also use 0x8e and 0x8f to indicate that the following sequence of
characters belongs to yet another character set.  To each following byte
is added the value 0x80.

=back

By carefully looking at the encoded byte sequence, you can find that the
byte sequence conforms a unique number.  In that sense, EUC is a CCS
generated by a CES above from up to four CCS (complicated?).  UTF-8
falls into this category.  See L<perlUnicode/"UTF-8"> to find out how
UTF-8 maps Unicode to a byte sequence.

You may also have found out by now why 7bit ISO-2022 cannot comprise
a CCS.  If you look at a byte sequence \x21\x21, you can't tell if
it is two !'s or IDEOGRAPHIC SPACE.  EUC maps the latter to \xA1\xA1
so you have no trouble differentiating between "!!". and S<"  ">.

=head1 Encoding Classification (by Anton Tagunov and Dan Kogai)

This section tries to classify the supported encodings by their 
applicability for information exchange over the Internet and to 
choose the most suitable aliases to name them in the context of 
such communication.

=over 4

=item * 

To (en|de)code encodings marked by C<(**)>, you need 
C<Encode::HanExtra>, available from CPAN.

=back

Encoding names

  US-ASCII    UTF-8    ISO-8859-*  KOI8-R
  Shift_JIS   EUC-JP   ISO-2022-JP ISO-2022-JP-1
  EUC-KR      Big5     GB2312

are registered with IANA as preferred MIME names and may
be used over the Internet.

C<Shift_JIS> has been officialized by JIS X 0208:1997.
L<Microsoft-related naming mess> gives details.

C<GB2312> is the IANA name for C<EUC-CN>.
See L<Microsoft-related naming mess> for details.

C<GB_2312-80> I<raw> encoding is available as C<gb2312-raw>
with Encode. See L<Encode::CN> for details.

  EUC-CN
  KOI8-U        [RFC2319]

have not been registered with IANA (as of March 2002) but
seem to be supported by major web browsers. 
The IANA name for C<EUC-CN> is C<GB2312>.

  KS_C_5601-1987

is heavily misused.
See L<Microsoft-related naming mess> for details.

C<KS_C_5601-1987> I<raw> encoding is available as C<kcs5601-raw>
with Encode. See L<Encode::KR> for details.

  UTF-16 UTF-16BE UTF-16LE

are IANA-registered C<charset>s. See [RFC 2781] for details.
Jungshik Shin reports that UTF-16 with a BOM is well accepted
by MS IE 5/6 and NS 4/6. Beware however that

=over 4

=item *

C<UTF-16> support in any software you're going to be
using/interoperating with has probably been less tested
then C<UTF-8> support

=item *

C<UTF-8> coded data seamlessly passes traditional
command piping (C<cat>, C<more>, etc.) while C<UTF-16> coded
data is likely to cause confusion (with its zero bytes,
for example)

=item *

it is beyond the power of words to describe the way HTML browsers
encode non-C<ASCII> form data. To get a general impression, visit
L<http://ppewww.ph.gla.ac.uk/~flavell/charset/form-i18n.html>.
While encoding of form data has stabilized for C<UTF-8> encoded pages
(at least IE 5/6, NS 6, and Opera 6 behave consistently), be sure to
expect fun (and cross-browser discrepancies) with C<UTF-16> encoded
pages!

=back

The rule of thumb is to use C<UTF-8> unless you know what
you're doing and unless you really benefit from using C<UTF-16>.

  ISO-IR-165    [RFC1345]
  VISCII
  GB 12345
  GB 18030 (**)  (see links bellow)
  EUC-TW   (**)

are totally valid encodings but not registered at IANA.
The names under which they are listed here are probably the
most widely-known names for these encodings and are recommended
names.

  BIG5PLUS (**)

is a proprietary name. 

=head2 Microsoft-related naming mess

Microsoft products misuse the following names:

=over 4

=item KS_C_5601-1987

Microsoft extension to C<EUC-KR>.

Proper names: C<CP949>, C<UHC>, C<x-windows-949> (as used by Mozilla).

See L<http://lists.w3.org/Archives/Public/ietf-charsets/2001AprJun/0033.html>
for details.

Encode aliases C<KS_C_5601-1987> to C<cp949> to reflect this common
misusage. I<Raw> C<KS_C_5601-1987> encoding is available as
C<kcs5601-raw>.

See L<Encode::KR> for details.

=item GB2312

Microsoft extension to C<EUC-CN>.

Proper names: C<CP936>, C<GBK>.

C<GB2312> has been registered in the C<EUC-CN> meaning at
IANA. This has partially repaired the situation: Microsoft's 
C<GB2312> has become a superset of the official C<GB2312>.

Encode aliases C<GB2312> to C<euc-cn> in full agreement with
IANA registration. C<cp936> is supported separately.
I<Raw> C<GB_2312-80> encoding is available as C<gb2312-raw>.

See L<Encode::CN> for details.

=item Big5

Microsoft extension to C<Big5>.

Proper name: C<CP950>.

Encode separately supports C<Big5> and C<cp950>.

=item Shift_JIS

Microsoft's understanding of C<Shift_JIS>.

JIS has not endorsed the full Microsoft standard however.
The official C<Shift_JIS> includes only JIS X 0201 and JIS X 0208
character sets, while Microsoft has always used C<Shift_JIS>
to encode a wider character repertoire. See C<IANA> registration for
C<Windows-31J>.

As a historical predecessor, Microsoft's variant
probably has more rights for the name, though it may be objected
that Microsoft shouldn't have used JIS as part of the name
in the first place.

Unambiguous name: C<CP932>. C<IANA> name (also used by Mozilla, and
provided as an alias by Encode): C<Windows-31J>.

Encode separately supports C<Shift_JIS> and C<cp932>.

=back

=head1 Glossary

=over 4

=item character repertoire

A collection of unique characters.  A I<character> set in the strictest
sense. At this stage, characters are not numbered.

=item coded character set (CCS)

A character set that is mapped in a way computers can use directly.
Many character encodings, including EUC, fall in this category.

=item character encoding scheme (CES)

An algorithm to map a character set to a byte sequence.  You don't
have to be able to tell which character set a given byte sequence
belongs.  7-bit ISO-2022 is a CES but it cannot be a CCS.  EUC is an
example of being both a CCS and CES.

=item charset (in MIME context)

has long been used in the meaning of C<encoding>, CES.

While the word combination C<character set> has lost this meaning
in MIME context since [RFC 2130], the C<charset> abbreviation has
retained it. This is how [RFC 2277] and [RFC 2278] bless C<charset>:

 This document uses the term "charset" to mean a set of rules for
 mapping from a sequence of octets to a sequence of characters, such
 as the combination of a coded character set and a character encoding
 scheme; this is also what is used as an identifier in MIME "charset="
 parameters, and registered in the IANA charset registry ...  (Note
 that this is NOT a term used by other standards bodies, such as ISO).
 [RFC 2277]

=item EUC

Extended Unix Character.  See ISO-2022.

=item ISO-2022

A CES that was carefully designed to coexist with ASCII.  There are a 7
bit version and an 8 bit version.  

The 7 bit version switches character set via escape sequence so it
cannot form a CCS.  Since this is more difficult to handle in programs
than the 8 bit version, the 7 bit version is not very popular except for
iso-2022-jp, the I<de facto> standard CES for e-mails.

The 8 bit version can form a CCS.  EUC and ISO-8859 are two examples
thereof.  Pre-5.6 perl could use them as string literals.

=item UCS

Short for I<Universal Character Set>.  When you say just UCS, it means
I<Unicode>.

=item UCS-2

ISO/IEC 10646 encoding form: Universal Character Set coded in two
octets.

=item Unicode

A character set that aims to include all character repertoires of the
world.  Many character sets in various national as well as industrial
standards have become, in a way, just subsets of Unicode.

=item UTF

Short for I<Unicode Transformation Format>.  Determines how to map a
Unicode character into a byte sequence.

=item UTF-16

A UTF in 16-bit encoding.  Can either be in big endian or little
endian.  The big endian version is called UTF-16BE (equal to UCS-2 + 
surrogate support) and the little endian version is called UTF-16LE.

=back

=head1 See Also

L<Encode>, 
L<Encode::Byte>, 
L<Encode::CN>, L<Encode::JP>, L<Encode::KR>, L<Encode::TW>,
L<Encode::EBCDIC>, L<Encode::Symbol>
L<Encode::MIME::Header>, L<Encode::Guess>

=head1 References

=over 4

=item ECMA

European Computer Manufacturers Association
L<http://www.ecma.ch>

=over 4

=item ECMA-035 (eq C<ISO-2022>)

L<http://www.ecma.ch/ecma1/STAND/ECMA-035.HTM> 

The specification of ISO-2022 is available from the link above.

=back

=item IANA

Internet Assigned Numbers Authority
L<http://www.iana.org/>

=over 4

=item Assigned Charset Names by IANA

L<http://www.iana.org/assignments/character-sets>

Most of the C<canonical names> in Encode derive from this list
so you can directly apply the string you have extracted from MIME
header of mails and web pages.

=back

=item ISO

International Organization for Standardization
L<http://www.iso.ch/>

=item RFC

Request For Comments -- need I say more?
L<http://www.rfc-editor.org/>, L<http://www.rfc.net/>,
L<http://www.faqs.org/rfcs/>

=item UC

Unicode Consortium
L<http://www.unicode.org/>

=over 4

=item Unicode Glossary

L<http://www.unicode.org/glossary/>

The glossary of this document is based upon this site.

=back

=back

=head2 Other Notable Sites

=over 4

=item czyborra.com

L<http://czyborra.com/>

Contains a lot of useful information, especially gory details of ISO
vs. vendor mappings.

=item CJK.inf

L<http://www.oreilly.com/people/authors/lunde/cjk_inf.html>

Somewhat obsolete (last update in 1996), but still useful.  Also try

L<ftp://ftp.oreilly.com/pub/examples/nutshell/cjkv/pdf/GB18030_Summary.pdf>

You will find brief info on C<EUC-CN>, C<GBK> and mostly on C<GB 18030>.

=item Jungshik Shin's Hangul FAQ

L<http://jshin.net/faq>

And especially its subject 8.

L<http://jshin.net/faq/qa8.html>

A comprehensive overview of the Korean (C<KS *>) standards.

=item debian.org: "Introduction to i18n"

A brief description for most of the mentioned CJK encodings is
contained in
L<http://www.debian.org/doc/manuals/intro-i18n/ch-codes.en.html>

=back

=head2 Offline sources

=over 4

=item C<CJKV Information Processing> by Ken Lunde

CJKV Information Processing
1999 O'Reilly & Associates, ISBN : 1-56592-224-7

The modern successor of C<CJK.inf>.

Features a comprehensive coverage of CJKV character sets and
encodings along with many other issues faced by anyone trying
to better support CJKV languages/scripts in all the areas of
information processing.

To purchase this book, visit
L<http://www.oreilly.com/catalog/cjkvinfo/>
or your favourite bookstore.

=back

=cut

--- NEW FILE: Encoding.pm ---
package Encode::Encoding;
# Base class for classes which implement encodings
use strict;
our $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

require Encode;

sub DEBUG { 0 }
sub Define
{
    my $obj = shift;
    my $canonical = shift;
    $obj = bless { Name => $canonical },$obj unless ref $obj;
    # warn "$canonical => $obj\n";
    Encode::define_encoding($obj, $canonical, @_);
}

sub name  { return shift->{'Name'} }

# sub renew { return $_[0] }

sub renew {
    my $self = shift;
    my $clone = bless { %$self } => ref($self);
    $clone->{renewed}++; # so the caller can see it
    DEBUG and warn $clone->{renewed};
    return $clone;
}

sub renewed{ return $_[0]->{renewed} || 0 }

*new_sequence = \&renew;

sub needs_lines { 0 };

sub perlio_ok { 
    eval{ require PerlIO::encoding };
    return $@ ? 0 : 1;
}

# (Temporary|legacy) methods

sub toUnicode    { shift->decode(@_) }
sub fromUnicode  { shift->encode(@_) }

#
# Needs to be overloaded or just croak
#

sub encode {
    require Carp;
    my $obj = shift;
    my $class = ref($obj) ? ref($obj) : $obj;
    Carp::croak($class . "->encode() not defined!");
}

sub decode{
    require Carp;
    my $obj = shift;
    my $class = ref($obj) ? ref($obj) : $obj;
    Carp::croak($class . "->encode() not defined!");
}

sub DESTROY {}

1;
__END__

=head1 NAME

Encode::Encoding - Encode Implementation Base Class

=head1 SYNOPSIS

  package Encode::MyEncoding;
  use base qw(Encode::Encoding);

  __PACKAGE__->Define(qw(myCanonical myAlias));

=head1 DESCRIPTION

As mentioned in L<Encode>, encodings are (in the current
implementation at least) defined as objects. The mapping of encoding
name to object is via the C<%Encode::Encoding> hash.  Though you can
directly manipulate this hash, it is strongly encouraged to use this
base class module and add encode() and decode() methods.

=head2 Methods you should implement

You are strongly encouraged to implement methods below, at least
either encode() or decode().

=over 4

=item -E<gt>encode($string [,$check])

MUST return the octet sequence representing I<$string>. 

=over 2

=item *

If I<$check> is true, it SHOULD modify I<$string> in place to remove
the converted part (i.e.  the whole string unless there is an error).
If perlio_ok() is true, SHOULD becomes MUST.

=item *

If an error occurs, it SHOULD return the octet sequence for the
fragment of string that has been converted and modify $string in-place
to remove the converted part leaving it starting with the problem
fragment.  If perlio_ok() is true, SHOULD becomes MUST.

=item *

If I<$check> is is false then C<encode> MUST  make a "best effort" to
convert the string - for example, by using a replacement character.

=back

=item -E<gt>decode($octets [,$check])

MUST return the string that I<$octets> represents. 

=over 2

=item *

If I<$check> is true, it SHOULD modify I<$octets> in place to remove
the converted part (i.e.  the whole sequence unless there is an
error).  If perlio_ok() is true, SHOULD becomes MUST.

=item *

If an error occurs, it SHOULD return the fragment of string that has
been converted and modify $octets in-place to remove the converted
part leaving it starting with the problem fragment.  If perlio_ok() is
true, SHOULD becomes MUST.

=item *

If I<$check> is false then C<decode> should make a "best effort" to
convert the string - for example by using Unicode's "\x{FFFD}" as a
replacement character.

=back

=back

If you want your encoding to work with L<encoding> pragma, you should
also implement the method below.

=over 4

=item -E<gt>cat_decode($destination, $octets, $offset, $terminator [,$check])

MUST decode I<$octets> with I<$offset> and concatenate it to I<$destination>.
Decoding will terminate when $terminator (a string) appears in output.
I<$offset> will be modified to the last $octets position at end of decode.
Returns true if $terminator appears output, else returns false.

=back

=head2 Other methods defined in Encode::Encodings

You do not have to override methods shown below unless you have to.

=over 4

=item -E<gt>name

Predefined As:

  sub name  { return shift->{'Name'} }

MUST return the string representing the canonical name of the encoding.

=item -E<gt>renew

Predefined As:

  sub renew {
    my $self = shift;
    my $clone = bless { %$self } => ref($self);
    $clone->{renewed}++;
    return $clone;
  }

This method reconstructs the encoding object if necessary.  If you need
to store the state during encoding, this is where you clone your object.

PerlIO ALWAYS calls this method to make sure it has its own private
encoding object.

=item -E<gt>renewed

Predefined As:

  sub renewed { $_[0]->{renewed} || 0 }

Tells whether the object is renewed (and how many times).  Some
modules emit C<Use of uninitialized value in null operation> warning
unless the value is numeric so return 0 for false.

=item -E<gt>perlio_ok()

Predefined As:

  sub perlio_ok { 
      eval{ require PerlIO::encoding };
      return $@ ? 0 : 1;
  }

If your encoding does not support PerlIO for some reasons, just;

 sub perlio_ok { 0 }

=item -E<gt>needs_lines()

Predefined As:

  sub needs_lines { 0 };

If your encoding can work with PerlIO but needs line buffering, you
MUST define this method so it returns true.  7bit ISO-2022 encodings
are one example that needs this.  When this method is missing, false
is assumed.

=back

=head2 Example: Encode::ROT13

  package Encode::ROT13;
  use strict;
  use base qw(Encode::Encoding);

  __PACKAGE__->Define('rot13');

  sub encode($$;$){
      my ($obj, $str, $chk) = @_;
      $str =~ tr/A-Za-z/N-ZA-Mn-za-m/;
      $_[1] = '' if $chk; # this is what in-place edit means
      return $str;
  }

  # Jr pna or ynml yvxr guvf;
  *decode = \&encode;

  1;

=head1 Why the heck Encode API is different?

It should be noted that the I<$check> behaviour is different from the
outer public API. The logic is that the "unchecked" case is useful
when the encoding is part of a stream which may be reporting errors
(e.g. STDERR).  In such cases, it is desirable to get everything
through somehow without causing additional errors which obscure the
original one. Also, the encoding is best placed to know what the
correct replacement character is, so if that is the desired behaviour
then letting low level code do it is the most efficient.

By contrast, if I<$check> is true, the scheme above allows the
encoding to do as much as it can and tell the layer above how much
that was. What is lacking at present is a mechanism to report what
went wrong. The most likely interface will be an additional method
call to the object, or perhaps (to avoid forcing per-stream objects
on otherwise stateless encodings) an additional parameter.

It is also highly desirable that encoding classes inherit from
C<Encode::Encoding> as a base class. This allows that class to define
additional behaviour for all encoding objects.

  package Encode::MyEncoding;
  use base qw(Encode::Encoding);

  __PACKAGE__->Define(qw(myCanonical myAlias));

to create an object with C<< bless {Name => ...}, $class >>, and call
define_encoding.  They inherit their C<name> method from
C<Encode::Encoding>.

=head2 Compiled Encodings

For the sake of speed and efficiency, most of the encodings are now
supported via a I<compiled form>: XS modules generated from UCM
files.   Encode provides the enc2xs tool to achieve that.  Please see
L<enc2xs> for more details.

=head1 SEE ALSO

L<perlmod>, L<enc2xs>

=begin future

=over 4

=item Scheme 1

The fixup routine gets passed the remaining fragment of string being
processed.  It modifies it in place to remove bytes/characters it can
understand and returns a string used to represent them.  For example:

 sub fixup {
   my $ch = substr($_[0],0,1,'');
   return sprintf("\x{%02X}",ord($ch);
 }

This scheme is close to how the underlying C code for Encode works,
but gives the fixup routine very little context.

=item Scheme 2

The fixup routine gets passed the original string, an index into
it of the problem area, and the output string so far.  It appends
what it wants to the output string and returns a new index into the
original string.  For example:

 sub fixup {
   # my ($s,$i,$d) = @_;
   my $ch = substr($_[0],$_[1],1);
   $_[2] .= sprintf("\x{%02X}",ord($ch);
   return $_[1]+1;
 }

This scheme gives maximal control to the fixup routine but is more
complicated to code, and may require that the internals of Encode be tweaked to
keep the original string intact.

=item Other Schemes

Hybrids of the above.

Multiple return values rather than in-place modifications.

Index into the string could be C<pos($str)> allowing C<s/\G...//>.

=back

=end future

=cut

--- NEW FILE: Encoder.pm ---
#
# $Id: Encoder.pm,v 1.1 2006-12-05 04:26:35 dslinux_cayenne Exp $
#
package Encode::Encoder;
use strict;
use warnings;
our $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d"  x $#r, @r };

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw ( encoder );

our $AUTOLOAD;
sub DEBUG () { 0 }
use Encode qw(encode decode find_encoding from_to);
use Carp;

sub new{
    my ($class, $data, $encname) = @_;
    unless($encname){
	$encname = Encode::is_utf8($data) ? 'utf8' : '';
    }else{
	my $obj = find_encoding($encname) 
	    or croak __PACKAGE__, ": unknown encoding: $encname";
	$encname = $obj->name;
    }
    my $self = {
		data     => $data,
		encoding => $encname,
	       };
    bless $self => $class;
}

sub encoder{ __PACKAGE__->new(@_) }

sub data{
    my ($self, $data) = @_;
    if (defined $data){
	$self->{data} = $data;
	return $data;
    }else{
	return $self->{data};
    }
}

sub encoding{
    my ($self, $encname) = @_;
    if ($encname){
	my $obj = find_encoding($encname) 
	    or confess __PACKAGE__, ": unknown encoding: $encname";
	$self->{encoding} = $obj->name;
	return $self;
    }else{
	return $self->{encoding}
    }
}

sub bytes {
    my ($self, $encname) = @_;
    $encname ||= $self->{encoding};
    my $obj = find_encoding($encname) 
	    or confess __PACKAGE__, ": unknown encoding: $encname";
    $self->{data} = $obj->decode($self->{data}, 1);
    $self->{encoding} = '' ;
    return $self;
}

sub DESTROY{ # defined so it won't autoload.
    DEBUG and warn shift;
}

sub AUTOLOAD {
    my $self = shift;
    my $type = ref($self)
	or confess "$self is not an object";
    my $myname = $AUTOLOAD;
    $myname =~ s/.*://;   # strip fully-qualified portion
    my $obj = find_encoding($myname) 
	    or confess __PACKAGE__, ": unknown encoding: $myname";
    DEBUG and warn $self->{encoding}, " => ", $obj->name;
    if ($self->{encoding}){
	from_to($self->{data}, $self->{encoding}, $obj->name, 1);
    }else{
	$self->{data} = $obj->encode($self->{data}, 1);
    }
    $self->{encoding} = $obj->name;
    return $self;
}

use overload 
    q("") => sub { $_[0]->{data} },
    q(0+) => sub { use bytes (); bytes::length($_[0]->{data}) },
    fallback => 1,
    ;

1;
__END__

=head1 NAME

Encode::Encoder -- Object Oriented Encoder

=head1 SYNOPSIS

  use Encode::Encoder;
  # Encode::encode("ISO-8859-1", $data); 
  Encode::Encoder->new($data)->iso_8859_1; # OOP way
  # shortcut
  use Encode::Encoder qw(encoder);
  encoder($data)->iso_8859_1;
  # you can stack them!
  encoder($data)->iso_8859_1->base64;  # provided base64() is defined
  # you can use it as a decoder as well
  encoder($base64)->bytes('base64')->latin1;
  # stringified
  print encoder($data)->utf8->latin1;  # prints the string in latin1
  # numified
  encoder("\x{abcd}\x{ef}g")->utf8 == 6; # true. bytes::length($data)

=head1 ABSTRACT

B<Encode::Encoder> allows you to use Encode in an object-oriented
style.  This is not only more intuitive than a functional approach,
but also handier when you want to stack encodings.  Suppose you want
your UTF-8 string converted to Latin1 then Base64: you can simply say

  my $base64 = encoder($utf8)->latin1->base64;

instead of

  my $latin1 = encode("latin1", $utf8);
  my $base64 = encode_base64($utf8);

or the lazier and more convoluted

  my $base64 = encode_base64(encode("latin1", $utf8));

=head1 Description

Here is how to use this module.

=over 4

=item *

There are at least two instance variables stored in a hash reference,
{data} and {encoding}.

=item *

When there is no method, it takes the method name as the name of the
encoding and encodes the instance I<data> with I<encoding>.  If successful,
the instance I<encoding> is set accordingly.

=item *

You can retrieve the result via -E<gt>data but usually you don't have to 
because the stringify operator ("") is overridden to do exactly that.

=back

=head2 Predefined Methods

This module predefines the methods below:

=over 4

=item $e = Encode::Encoder-E<gt>new([$data, $encoding]);

returns an encoder object.  Its data is initialized with $data if
present, and its encoding is set to $encoding if present.

When $encoding is omitted, it defaults to utf8 if $data is already in
utf8 or "" (empty string) otherwise.

=item encoder()

is an alias of Encode::Encoder-E<gt>new().  This one is exported on demand.

=item $e-E<gt>data([$data])

When $data is present, sets the instance data to $data and returns the
object itself.  Otherwise, the current instance data is returned.

=item $e-E<gt>encoding([$encoding])

When $encoding is present, sets the instance encoding to $encoding and
returns the object itself.  Otherwise, the current instance encoding is
returned.

=item $e-E<gt>bytes([$encoding])

decodes instance data from $encoding, or the instance encoding if
omitted.  If the conversion is successful, the instance encoding
will be set to "".

The name I<bytes> was deliberately picked to avoid namespace tainting
-- this module may be used as a base class so method names that appear
in Encode::Encoding are avoided.

=back

=head2 Example: base64 transcoder

This module is designed to work with L<Encode::Encoding>.
To make the Base64 transcoder example above really work, you could
write a module like this:

  package Encode::Base64;
  use base 'Encode::Encoding';
  __PACKAGE__->Define('base64');
  use MIME::Base64;
  sub encode{ 
      my ($obj, $data) = @_; 
      return encode_base64($data);
  }
  sub decode{
      my ($obj, $data) = @_; 
      return decode_base64($data);
  }
  1;
  __END__

And your caller module would be something like this:

  use Encode::Encoder;
  use Encode::Base64;

  # now you can really do the following

  encoder($data)->iso_8859_1->base64;
  encoder($base64)->bytes('base64')->latin1;

=head2 Operator Overloading

This module overloads two operators, stringify ("") and numify (0+).

Stringify dumps the data inside the object.

Numify returns the number of bytes in the instance data.

They come in handy when you want to print or find the size of data.

=head1 SEE ALSO

L<Encode>,
L<Encode::Encoding>

=cut

--- NEW FILE: CJKConstants.pm ---
#
# $Id: CJKConstants.pm,v 1.1 2006-12-05 04:26:35 dslinux_cayenne Exp $
#

package Encode::CJKConstants;

use strict;

our $RCSID = q$Id: CJKConstants.pm,v 1.1 2006-12-05 04:26:35 dslinux_cayenne Exp $;
our $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

use Carp;

require Exporter;
our @ISA         = qw(Exporter);
our @EXPORT      = qw();
our @EXPORT_OK   = qw(%CHARCODE %ESC %RE);
our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK, @EXPORT ] );

my %_0208 = (
	       1978 => '\e\$\@',
	       1983 => '\e\$B',
	       1990 => '\e&\@\e\$B',
		);

our %CHARCODE = (
	     UNDEF_EUC  =>     "\xa2\xae",  # ¢® in EUC
	     UNDEF_SJIS =>     "\x81\xac",  # ¢® in SJIS
	     UNDEF_JIS  =>     "\xa2\xf7",  # ¢÷ -- used in unicode
	     UNDEF_UNICODE  => "\x20\x20",  # ¢÷ -- used in unicode
	 );

our %ESC =  (
	 GB_2312  => "\e\$A",
	 JIS_0208 => "\e\$B",
	 JIS_0212 => "\e\$(D",
	 KSC_5601 => "\e\$(C",
	 ASC      => "\e\(B",
	 KANA     => "\e\(I",
	 '2022_KR' => "\e\$)C",
	 );

our %RE =
    (
     ASCII     => '[\x00-\x7f]',
     BIN       => '[\x00-\x06\x7f\xff]',
     EUC_0212  => '\x8f[\xa1-\xfe][\xa1-\xfe]',
     EUC_C     => '[\xa1-\xfe][\xa1-\xfe]',
     EUC_KANA  => '\x8e[\xa1-\xdf]',
     JIS_0208  =>  "$_0208{1978}|$_0208{1983}|$_0208{1990}",
     JIS_0212  => "\e" . '\$\(D',
     ISO_ASC   => "\e" . '\([BJ]',     
     JIS_KANA  => "\e" . '\(I',
     '2022_KR' => "\e" . '\$\)C',
     SJIS_C    => '[\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc]',
     SJIS_KANA => '[\xa1-\xdf]',
     UTF8      => '[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]'
     );

1;

=head1 NAME

Encode::CJKConstants.pm -- Internally used by Encode::??::ISO_2022_*

=cut

--- NEW FILE: Config.pm ---
#
# Demand-load module list
#
package Encode::Config;
our $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

use strict;

our %ExtModule = 
    (
      # Encode::Byte
      #iso-8859-1 is in Encode.pm itself
     'iso-8859-2'             => 'Encode::Byte',
     'iso-8859-3'             => 'Encode::Byte',
     'iso-8859-4'             => 'Encode::Byte',
     'iso-8859-5'             => 'Encode::Byte',
     'iso-8859-6'             => 'Encode::Byte',
     'iso-8859-7'             => 'Encode::Byte',
     'iso-8859-8'             => 'Encode::Byte',
     'iso-8859-9'             => 'Encode::Byte',
     'iso-8859-10'            => 'Encode::Byte',
     'iso-8859-11'            => 'Encode::Byte',
     'iso-8859-13'            => 'Encode::Byte',
     'iso-8859-14'            => 'Encode::Byte',
     'iso-8859-15'            => 'Encode::Byte',
     'iso-8859-16'            => 'Encode::Byte',
     'koi8-f'                 => 'Encode::Byte',
     'koi8-r'                 => 'Encode::Byte',
     'koi8-u'                 => 'Encode::Byte',
     'viscii'                 => 'Encode::Byte',
     'cp424'                  => 'Encode::Byte',
     'cp437'                  => 'Encode::Byte',
     'cp737'                  => 'Encode::Byte',
     'cp775'                  => 'Encode::Byte',
     'cp850'                  => 'Encode::Byte',
     'cp852'                  => 'Encode::Byte',
     'cp855'                  => 'Encode::Byte',
     'cp856'                  => 'Encode::Byte',
     'cp857'                  => 'Encode::Byte',
     'cp860'                  => 'Encode::Byte',
     'cp861'                  => 'Encode::Byte',
     'cp862'                  => 'Encode::Byte',
     'cp863'                  => 'Encode::Byte',
     'cp864'                  => 'Encode::Byte',
     'cp865'                  => 'Encode::Byte',
     'cp866'                  => 'Encode::Byte',
     'cp869'                  => 'Encode::Byte',
     'cp874'                  => 'Encode::Byte',
     'cp1006'                 => 'Encode::Byte',
     'cp1250'                 => 'Encode::Byte',
     'cp1251'                 => 'Encode::Byte',
     'cp1252'                 => 'Encode::Byte',
     'cp1253'                 => 'Encode::Byte',
     'cp1254'                 => 'Encode::Byte',
     'cp1255'                 => 'Encode::Byte',
     'cp1256'                 => 'Encode::Byte',
     'cp1257'                 => 'Encode::Byte',
     'cp1258'                 => 'Encode::Byte',
     'AdobeStandardEncoding'  => 'Encode::Byte',
     'MacArabic'              => 'Encode::Byte',
     'MacCentralEurRoman'     => 'Encode::Byte',
     'MacCroatian'            => 'Encode::Byte',
     'MacCyrillic'            => 'Encode::Byte',
     'MacFarsi'               => 'Encode::Byte',
     'MacGreek'               => 'Encode::Byte',
     'MacHebrew'              => 'Encode::Byte',
     'MacIcelandic'           => 'Encode::Byte',
     'MacRoman'               => 'Encode::Byte',
     'MacRomanian'            => 'Encode::Byte',
     'MacRumanian'            => 'Encode::Byte',
     'MacSami'                => 'Encode::Byte',
     'MacThai'                => 'Encode::Byte',
     'MacTurkish'             => 'Encode::Byte',
     'MacUkrainian'           => 'Encode::Byte',
     'nextstep'               => 'Encode::Byte',
     'hp-roman8'              => 'Encode::Byte',
     'gsm0338'                => 'Encode::Byte',
     # Encode::EBCDIC
     'cp37'                   => 'Encode::EBCDIC',
     'cp500'                  => 'Encode::EBCDIC',
     'cp875'                  => 'Encode::EBCDIC',
     'cp1026'                 => 'Encode::EBCDIC',
     'cp1047'                 => 'Encode::EBCDIC',
     'posix-bc'               => 'Encode::EBCDIC',
     # Encode::Symbol
     'dingbats'               => 'Encode::Symbol',
     'symbol'                 => 'Encode::Symbol',
     'AdobeSymbol'            => 'Encode::Symbol',
     'AdobeZdingbat'          => 'Encode::Symbol',
     'MacDingbats'            => 'Encode::Symbol',
     'MacSymbol'              => 'Encode::Symbol',
     # Encode::Unicode
     'UCS-2BE'                => 'Encode::Unicode',
     'UCS-2LE'                => 'Encode::Unicode',
     'UTF-16'                 => 'Encode::Unicode',
     'UTF-16BE'               => 'Encode::Unicode',
     'UTF-16LE'               => 'Encode::Unicode',
     'UTF-32'                 => 'Encode::Unicode',
     'UTF-32BE'               => 'Encode::Unicode',
     'UTF-32LE'               => 'Encode::Unicode',
     'UTF-7'                  => 'Encode::Unicode::UTF7',
    );

unless (ord("A") == 193){
    %ExtModule =
	(
	 %ExtModule,
	 'euc-cn'             => 'Encode::CN',
	 'gb12345-raw'        => 'Encode::CN',
	 'gb2312-raw'         => 'Encode::CN',
	 'hz'                 => 'Encode::CN',
	 'iso-ir-165'         => 'Encode::CN',
	 'cp936'              => 'Encode::CN',
	 'MacChineseSimp'     => 'Encode::CN',

	 '7bit-jis'           => 'Encode::JP',
	 'euc-jp'             => 'Encode::JP',
	 'iso-2022-jp'        => 'Encode::JP',
	 'iso-2022-jp-1'      => 'Encode::JP',
	 'jis0201-raw'        => 'Encode::JP',
	 'jis0208-raw'        => 'Encode::JP',
	 'jis0212-raw'        => 'Encode::JP',
	 'cp932'              => 'Encode::JP',
	 'MacJapanese'        => 'Encode::JP',
	 'shiftjis'           => 'Encode::JP',


	 'euc-kr'             => 'Encode::KR',
	 'iso-2022-kr'        => 'Encode::KR',
	 'johab'              => 'Encode::KR',
	 'ksc5601-raw'        => 'Encode::KR',
	 'cp949'              => 'Encode::KR',
	 'MacKorean'          => 'Encode::KR',

	 'big5-eten'          => 'Encode::TW',
	 'big5-hkscs'         => 'Encode::TW',
	 'cp950'              => 'Encode::TW',
	 'MacChineseTrad'     => 'Encode::TW',

	 #'big5plus'           => 'Encode::HanExtra',
	 #'euc-tw'             => 'Encode::HanExtra',
	 #'gb18030'            => 'Encode::HanExtra',

	 'MIME-Header'        => 'Encode::MIME::Header',
	 'MIME-B'             => 'Encode::MIME::Header',
	 'MIME-Q'             => 'Encode::MIME::Header',

	 'MIME-Header-ISO_2022_JP' => 'Encode::MIME::Header::ISO_2022_JP',
	);
}

#
# Why not export ? to keep ConfigLocal Happy!
#
while (my ($enc,$mod) = each %ExtModule){
    $Encode::ExtModule{$enc} = $mod;
}

1;
__END__

=head1 NAME

Encode::Config -- internally used by Encode

=cut

--- NEW FILE: Alias.pm ---
package Encode::Alias;
use strict;
no warnings 'redefine';
use Encode;
our $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
sub DEBUG () { 0 }

use base qw(Exporter);

# Public, encouraged API is exported by default

our @EXPORT = 
    qw (
	define_alias
	find_alias
	);

our @Alias;  # ordered matching list
our %Alias;  # cached known aliases

sub find_alias{
    my $class = shift;
    my $find = shift;
    unless (exists $Alias{$find}) {
        $Alias{$find} = undef; # Recursion guard
	for (my $i=0; $i < @Alias; $i += 2){
	    my $alias = $Alias[$i];
	    my $val   = $Alias[$i+1];
	    my $new;
	    if (ref($alias) eq 'Regexp' && $find =~ $alias){
		DEBUG and warn "eval $val";
		$new = eval $val;
		DEBUG and $@ and warn "$val, $@";
	    }elsif (ref($alias) eq 'CODE'){
		DEBUG and warn "$alias", "->", "($find)";
		$new = $alias->($find);
	    }elsif (lc($find) eq lc($alias)){
		$new = $val;
	    }
	    if (defined($new)){
		next if $new eq $find; # avoid (direct) recursion on bugs
		DEBUG and warn "$alias, $new";
		my $enc = (ref($new)) ? $new : Encode::find_encoding($new);
		if ($enc){
		    $Alias{$find} = $enc;
		    last;
		}
	    }
	}
	# case insensitive search when canonical is not in all lowercase
	# RT ticket #7835
	unless ($Alias{$find}){
	    my $lcfind = lc($find);
	    for my $name (keys %Encode::Encoding, keys %Encode::ExtModule){
		$lcfind eq lc($name) or next;
		$Alias{$find} =  Encode::find_encoding($name);
		DEBUG and warn "$find => $name";
	    }
	}
    }
    if (DEBUG){
	my $name;
	if (my $e = $Alias{$find}){
	    $name = $e->name;
	}else{
	    $name = "";
	}
	warn "find_alias($class, $find)->name = $name";
    }
    return $Alias{$find};
}

sub define_alias{
    while (@_){
	my ($alias,$name) = splice(@_,0,2);
	unshift(@Alias, $alias => $name);   # newer one has precedence
	if (ref($alias)){
	    # clear %Alias cache to allow overrides
	    my @a = keys %Alias;
	    for my $k (@a){
		if (ref($alias) eq 'Regexp' && $k =~ $alias){
		    DEBUG and warn "delete \$Alias\{$k\}";
		    delete $Alias{$k};
		}
		elsif (ref($alias) eq 'CODE'){
		    DEBUG and warn "delete \$Alias\{$k\}";
		    delete $Alias{$alias->($name)};
		}
	    }
	}else{
	    DEBUG and warn "delete \$Alias\{$alias\}";
	    delete $Alias{$alias};
	}
    }
}

# Allow latin-1 style names as well
# 0  1  2  3  4  5   6   7   8   9  10
our @Latin2iso = ( 0, 1, 2, 3, 4, 9, 10, 13, 14, 15, 16 );
# Allow winlatin1 style names as well
our %Winlatin2cp   = (
		      'latin1'     => 1252,
		      'latin2'     => 1250,
		      'cyrillic'   => 1251,
		      'greek'      => 1253,
		      'turkish'    => 1254,
		      'hebrew'     => 1255,
		      'arabic'     => 1256,
		      'baltic'     => 1257,
		      'vietnamese' => 1258,
		     );

init_aliases();

sub undef_aliases{
    @Alias = ();
    %Alias = ();
}

sub init_aliases
{
    undef_aliases();
    # Try all-lower-case version should all else fails
    define_alias( qr/^(.*)$/ => '"\L$1"' );

    # UTF/UCS stuff
    define_alias( qr/^UTF-?7$/i           => '"UTF-7"');
    define_alias( qr/^UCS-?2-?LE$/i       => '"UCS-2LE"' );
    define_alias( qr/^UCS-?2-?(BE)?$/i    => '"UCS-2BE"',
                  qr/^UCS-?4-?(BE|LE)?$/i => 'uc("UTF-32$1")',
		  qr/^iso-10646-1$/i      => '"UCS-2BE"' );
    define_alias( qr/^UTF-?(16|32)-?BE$/i   => '"UTF-$1BE"',
		  qr/^UTF-?(16|32)-?LE$/i   => '"UTF-$1LE"',
		  qr/^UTF-?(16|32)$/i       => '"UTF-$1"',
		);
    # ASCII
    define_alias(qr/^(?:US-?)ascii$/i => '"ascii"');
    define_alias('C' => 'ascii');
    define_alias(qr/\bISO[-_]?646[-_]?US$/i => '"ascii"');
    # Allow variants of iso-8859-1 etc.
    define_alias( qr/\biso[-_]?(\d+)[-_](\d+)$/i => '"iso-$1-$2"' );

    # At least HP-UX has these.
    define_alias( qr/\biso8859(\d+)$/i => '"iso-8859-$1"' );

    # More HP stuff.
    define_alias( qr/\b(?:hp-)?(arabic|greek|hebrew|kana|roman|thai|turkish)8$/i => '"${1}8"' );

    # The Official name of ASCII.
    define_alias( qr/\bANSI[-_]?X3\.4[-_]?1968$/i => '"ascii"' );

    # This is a font issue, not an encoding issue.
    # (The currency symbol of the Latin 1 upper half
    #  has been redefined as the euro symbol.)
    define_alias( qr/^(.+)\@euro$/i => '"$1"' );

    define_alias( qr/\b(?:iso[-_]?)?latin[-_]?(\d+)$/i 
		  => 'defined $Encode::Alias::Latin2iso[$1] ? "iso-8859-$Encode::Alias::Latin2iso[$1]" : undef' );

    define_alias( qr/\bwin(latin[12]|cyrillic|baltic|greek|turkish|
			 hebrew|arabic|baltic|vietnamese)$/ix => 
		  '"cp" . $Encode::Alias::Winlatin2cp{lc($1)}' );

    # Common names for non-latin preferred MIME names
    define_alias( 'ascii'    => 'US-ascii',
		  'cyrillic' => 'iso-8859-5',
		  'arabic'   => 'iso-8859-6',
		  'greek'    => 'iso-8859-7',
		  'hebrew'   => 'iso-8859-8',
		  'thai'     => 'iso-8859-11',
		  'tis620'   => 'iso-8859-11',
		  );

    # At least AIX has IBM-NNN (surprisingly...) instead of cpNNN.
    # And Microsoft has their own naming (again, surprisingly).
    # And windows-* is registered in IANA! 
    define_alias( qr/\b(?:cp|ibm|ms|windows)[-_ ]?(\d{2,4})$/i => '"cp$1"');

    # Sometimes seen with a leading zero.
    # define_alias( qr/\bcp037\b/i => '"cp37"');

    # Mac Mappings
    # predefined in *.ucm; unneeded
    # define_alias( qr/\bmacIcelandic$/i => '"macIceland"');
    define_alias( qr/^mac_(.*)$/i => '"mac$1"');
    # Ououououou. gone.  They are differente!
    # define_alias( qr/\bmacRomanian$/i => '"macRumanian"');
  
    # Standardize on the dashed versions.
    define_alias( qr/\bkoi8[\s\-_]*([ru])$/i => '"koi8-$1"' );

    unless ($Encode::ON_EBCDIC){
        # for Encode::CN
	define_alias( qr/\beuc.*cn$/i        => '"euc-cn"' );
	define_alias( qr/\bcn.*euc$/i        => '"euc-cn"' );
	# define_alias( qr/\bGB[- ]?(\d+)$/i => '"euc-cn"' )
	# CP936 doesn't have vendor-addon for GBK, so they're identical.
	define_alias( qr/^gbk$/i => '"cp936"');
	# This fixes gb2312 vs. euc-cn confusion, practically
	define_alias( qr/\bGB[-_ ]?2312(?!-?raw)/i => '"euc-cn"' );
	# for Encode::JP
	define_alias( qr/\bjis$/i            => '"7bit-jis"' );
	define_alias( qr/\beuc.*jp$/i        => '"euc-jp"' );
	define_alias( qr/\bjp.*euc$/i        => '"euc-jp"' );
	define_alias( qr/\bujis$/i           => '"euc-jp"' );
	define_alias( qr/\bshift.*jis$/i     => '"shiftjis"' );
	define_alias( qr/\bsjis$/i           => '"shiftjis"' );
	define_alias( qr/\bwindows-31j$/i    => '"cp932"' );
        # for Encode::KR
	define_alias( qr/\beuc.*kr$/i        => '"euc-kr"' );
	define_alias( qr/\bkr.*euc$/i        => '"euc-kr"' );
	# This fixes ksc5601 vs. euc-kr confusion, practically
        define_alias( qr/(?:x-)?uhc$/i            => '"cp949"' );
        define_alias( qr/(?:x-)?windows-949$/i    => '"cp949"' );
        define_alias( qr/\bks_c_5601-1987$/i      => '"cp949"' );
        # for Encode::TW
	define_alias( qr/\bbig-?5$/i		  => '"big5-eten"' );
	define_alias( qr/\bbig5-?et(?:en)?$/i	  => '"big5-eten"' );
	define_alias( qr/\btca[-_]?big5$/i	  => '"big5-eten"' );
	define_alias( qr/\bbig5-?hk(?:scs)?$/i	  => '"big5-hkscs"' );
	define_alias( qr/\bhk(?:scs)?[-_]?big5$/i  => '"big5-hkscs"' );
    }
    # utf8 is blessed :)
    define_alias( qr/^UTF-8$/i => '"utf-8-strict"');
    # At last, Map white space and _ to '-'
    define_alias( qr/^(\S+)[\s_]+(.*)$/i => '"$1-$2"' );
}

1;
__END__

# TODO: HP-UX '8' encodings arabic8 greek8 hebrew8 kana8 thai8 turkish8
# TODO: HP-UX '15' encodings japanese15 korean15 roi15
# TODO: Cyrillic encoding ISO-IR-111 (useful?)
# TODO: Armenian encoding ARMSCII-8
# TODO: Hebrew encoding ISO-8859-8-1
# TODO: Thai encoding TCVN
# TODO: Vietnamese encodings VPS
# TODO: Mac Asian+African encodings: Arabic Armenian Bengali Burmese
#       ChineseSimp ChineseTrad Devanagari Ethiopic ExtArabic
#       Farsi Georgian Gujarati Gurmukhi Hebrew Japanese
#       Kannada Khmer Korean Laotian Malayalam Mongolian
#       Oriya Sinhalese Symbol Tamil Telugu Tibetan Vietnamese

=head1 NAME

Encode::Alias - alias definitions to encodings

=head1 SYNOPSIS

  use Encode;
  use Encode::Alias;
  define_alias( newName => ENCODING);

=head1 DESCRIPTION

Allows newName to be used as an alias for ENCODING. ENCODING may be
either the name of an encoding or an encoding object (as described 
in L<Encode>).

Currently I<newName> can be specified in the following ways:

=over 4

=item As a simple string.

=item As a qr// compiled regular expression, e.g.:

  define_alias( qr/^iso8859-(\d+)$/i => '"iso-8859-$1"' );

In this case, if I<ENCODING> is not a reference, it is C<eval>-ed
in order to allow C<$1> etc. to be substituted.  The example is one
way to alias names as used in X11 fonts to the MIME names for the
iso-8859-* family.  Note the double quotes inside the single quotes.

(or, you don't have to do this yourself because this example is predefined)

If you are using a regex here, you have to use the quotes as shown or
it won't work.  Also note that regex handling is tricky even for the
experienced.  Use this feature with caution.

=item As a code reference, e.g.:

  define_alias( sub {shift =~ /^iso8859-(\d+)$/i ? "iso-8859-$1" : undef } );

The same effect as the example above in a different way.  The coderef
takes the alias name as an argument and returns a canonical name on
success or undef if not.  Note the second argument is not required.
Use this with even more caution than the regex version.

=back

=head3 Changes in code reference aliasing

As of Encode 1.87, the older form

  define_alias( sub { return  /^iso8859-(\d+)$/i ? "iso-8859-$1" : undef } );

no longer works. 

Encode up to 1.86 internally used "local $_" to implement ths older
form.  But consider the code below;

  use Encode;
  $_ = "eeeee" ;
  while (/(e)/g) {
    my $utf = decode('aliased-encoding-name', $1);
    print "position:",pos,"\n";
  }

Prior to Encode 1.86 this fails because of "local $_".

=head2 Alias overloading

You can override predefined aliases by simply applying define_alias().
The new alias is always evaluated first, and when necessary,
define_alias() flushes the internal cache to make the new definition
available.

  # redirect SHIFT_JIS to MS/IBM Code Page 932, which is a
  # superset of SHIFT_JIS

  define_alias( qr/shift.*jis$/i  => '"cp932"' );
  define_alias( qr/sjis$/i        => '"cp932"' );

If you want to zap all predefined aliases, you can use

  Encode::Alias->undef_aliases;

to do so.  And

  Encode::Alias->init_aliases;

gets the factory settings back.

=head1 SEE ALSO

L<Encode>, L<Encode::Supported>

=cut


--- NEW FILE: PerlIO.pod ---
=head1 NAME

Encode::PerlIO -- a detailed document on Encode and PerlIO

=head1 Overview

It is very common to want to do encoding transformations when
reading or writing files, network connections, pipes etc.
If Perl is configured to use the new 'perlio' IO system then
C<Encode> provides a "layer" (see L<PerlIO>) which can transform
data as it is read or written.

Here is how the blind poet would modernise the encoding:

    use Encode;
    open(my $iliad,'<:encoding(iso-8859-7)','iliad.greek');
    open(my $utf8,'>:utf8','iliad.utf8');
    my @epic = <$iliad>;
    print $utf8 @epic;
    close($utf8);
    close($illiad);

In addition, the new IO system can also be configured to read/write
UTF-8 encoded characters (as noted above, this is efficient):

    open(my $fh,'>:utf8','anything');
    print $fh "Any \x{0021} string \N{SMILEY FACE}\n";

Either of the above forms of "layer" specifications can be made the default
for a lexical scope with the C<use open ...> pragma. See L<open>.

Once a handle is open, its layers can be altered using C<binmode>.

Without any such configuration, or if Perl itself is built using the
system's own IO, then write operations assume that the file handle
accepts only I<bytes> and will C<die> if a character larger than 255 is
written to the handle. When reading, each octet from the handle becomes
a byte-in-a-character. Note that this default is the same behaviour
as bytes-only languages (including Perl before v5.6) would have,
and is sufficient to handle native 8-bit encodings e.g. iso-8859-1,
EBCDIC etc. and any legacy mechanisms for handling other encodings
and binary data.

In other cases, it is the program's responsibility to transform
characters into bytes using the API above before doing writes, and to
transform the bytes read from a handle into characters before doing
"character operations" (e.g. C<lc>, C</\W+/>, ...).

You can also use PerlIO to convert larger amounts of data you don't
want to bring into memory.  For example, to convert between ISO-8859-1
(Latin 1) and UTF-8 (or UTF-EBCDIC in EBCDIC machines):

    open(F, "<:encoding(iso-8859-1)", "data.txt") or die $!;
    open(G, ">:utf8",                 "data.utf") or die $!;
    while (<F>) { print G }

    # Could also do "print G <F>" but that would pull
    # the whole file into memory just to write it out again.

More examples:

    open(my $f, "<:encoding(cp1252)")
    open(my $g, ">:encoding(iso-8859-2)")
    open(my $h, ">:encoding(latin9)")       # iso-8859-15

See also L<encoding> for how to change the default encoding of the
data in your script.

=head1 How does it work?

Here is a crude diagram of how filehandle, PerlIO, and Encode
interact.

  filehandle <-> PerlIO        PerlIO <-> scalar (read/printed)
                       \      /
                        Encode   

When PerlIO receives data from either direction, it fills a buffer
(currently with 1024 bytes) and passes the buffer to Encode.
Encode tries to convert the valid part and passes it back to PerlIO,
leaving invalid parts (usually a partial character) in the buffer.
PerlIO then appends more data to the buffer, calls Encode again,
and so on until the data stream ends.

To do so, PerlIO always calls (de|en)code methods with CHECK set to 1.
This ensures that the method stops at the right place when it
encounters partial character.  The following is what happens when
PerlIO and Encode tries to encode (from utf8) more than 1024 bytes
and the buffer boundary happens to be in the middle of a character.

   A   B   C   ....   ~     \x{3000}    ....
  41  42  43   ....  7E   e3   80   80  ....
  <- buffer --------------->
  << encoded >>>>>>>>>>
                       <- next buffer ------

Encode converts from the beginning to \x7E, leaving \xe3 in the buffer
because it is invalid (partial character).

Unfortunately, this scheme does not work well with escape-based
encodings such as ISO-2022-JP.

=head1 Line Buffering

Now let's see what happens when you try to decode from ISO-2022-JP and
the buffer ends in the middle of a character.

			  JIS208-ESC   \x{5f3e}
   A   B   C   ....   ~   \e   $   B  |DAN | ....
  41  42  43   ....  7E   1b  24  41  43  46 ....
  <- buffer --------------------------->
  << encoded >>>>>>>>>>>>>>>>>>>>>>>

As you see, the next buffer begins with \x43.  But \x43 is 'C' in
ASCII, which is wrong in this case because we are now in JISX 0208
area so it has to convert \x43\x46, not \x43.  Unlike utf8 and EUC,
in escape-based encodings you can't tell if a given octet is a whole
character or just part of it.

Fortunately PerlIO also supports line buffer if you tell PerlIO to use
one instead of fixed buffer.  Since ISO-2022-JP is guaranteed to revert to ASCII at the end of the line, partial
character will never happen when line buffer is used.

To tell PerlIO to use line buffer, implement -E<gt>needs_lines method
for your encoding object.  See  L<Encode::Encoding> for details.

Thanks to these efforts most encodings that come with Encode support
PerlIO but that still leaves following encodings.

  iso-2022-kr
  MIME-B
  MIME-Header
  MIME-Q

Fortunately iso-2022-kr is hardly used (according to Jungshik) and
MIME-* are very unlikely to be fed to PerlIO because they are for mail
headers.  See L<Encode::MIME::Header> for details.

=head2 How can I tell whether my encoding fully supports PerlIO ?

As of this writing, any encoding whose class belongs to Encode::XS and
Encode::Unicode works.  The Encode module has a C<perlio_ok> method
which you can use before applying PerlIO encoding to the filehandle.
Here is an example:

  my $use_perlio = perlio_ok($enc);
  my $layer = $use_perlio ? "<:raw" : "<:encoding($enc)";
  open my $fh, $layer, $file or die "$file : $!";
  while(<$fh>){
    $_ = decode($enc, $_) unless $use_perlio;
    # .... 
  }

=head1 SEE ALSO

L<Encode::Encoding>,
L<Encode::Supported>,
L<Encode::PerlIO>, 
L<encoding>,
L<perlebcdic>, 
L<perlfunc/open>, 
L<perlunicode>, 
L<utf8>, 
the Perl Unicode Mailing List E<lt>perl-unicode at perl.orgE<gt>

=cut





More information about the dslinux-commit mailing list