# $File: //member/autrijus/Encode-HanConvert/lib/Encode/HanConvert/Perl.pm-orig $ $Author: autrijus $ # $Revision: #8 $ $Change: 10738 $ $DateTime: 2004/06/03 20:56:41 $ package Encode::HanConvert::Perl; use strict; use base 'Exporter'; use vars qw/$VERSION @EXPORT @EXPORT_OK/; $VERSION = '0.05'; @EXPORT = qw(big5_to_gb gb_to_big5); ### perl 5.006 ### push @EXPORT, qw(trad_to_simp simp_to_trad); @EXPORT_OK = qw(simple trad); ### /perl 5.006 ### my (%b2g, %g2b, %t2s, %s2t); sub big5_to_gb ($) { ### perl 5.006 ### use bytes; ### /perl 5.006 ### eval q*( %b2g = ( ### include b2g_map.txt ### ) )* unless %b2g; if (defined wantarray) { my $var = $_[0]; $var =~ s/([\xA1-\xF9].)/$b2g{$1}/eg; return $var; } else { $_[0] =~ s/([\xA1-\xF9].)/$b2g{$1}/eg; } } sub gb_to_big5 ($) { ### perl 5.006 ### use bytes; ### /perl 5.006 ### eval q*( %g2b = ( ### include g2b_map.txt ### ) )* unless %g2b; if (defined wantarray) { my $var = $_[0]; $var =~ s/([\x81-\xFE].)/$g2b{$1}/eg; return $var; } else { $_[0] =~ s/([\x81-\xFE].)/$g2b{$1}/eg; } } ### perl 5.006 ### sub trad_to_simp ($) { use utf8; # 5.006 require Encode if $] >= 5.008; eval q*( %t2s = ( ### include b2g_map.utf8 ### ) )* unless %t2s; if (defined wantarray) { my $var = $_[0]; Encode::_utf8_on($_[0]) if $] >= 5.008; $var =~ s/(\S)/$t2s{$1} || $1/eg; return $var; } else { Encode::_utf8_on($_[0]) if $] >= 5.008; $_[0] =~ s/(\S)/$t2s{$1} || $1/eg; } } sub simp_to_trad ($) { use utf8; # 5.006 require Encode if $] >= 5.008; eval q*( %s2t = ( ### include g2b_map.utf8 ### ) )* unless %s2t; if (defined wantarray) { my $var = $_[0]; Encode::_utf8_on($_[0]) if $] >= 5.008; $var =~ s/(\S)/$s2t{$1} || $1/eg; return $var; } else { Encode::_utf8_on($_[0]) if $] >= 5.008; $_[0] =~ s/(\S)/$s2t{$1} || $1/eg; } } # Lingua::ZH::HanConvert drop-in replacement -- not exported by default sub trad { simp_to_trad($_[0]) }; sub simple { trad_to_simp($_[0]) }; ### /perl 5.006 ### 1; __END__ =head1 NAME Encode::HanConvert::Perl - Perl-based Encode::HanConvert =head1 SYNOPSIS use Encode::HanConvert; # autoloads Encode::HanConvert::Perl # Conversion between Chinese encodings $euc_cn = big5_to_gb($big5); # Big5 to GBK $big5 = gb_to_big5($euc_cn); # GBK to Big5 # Conversion between Perl's Unicode strings - v5.6.0+ only $simp = trad_to_simp($trad); # Traditional to Simplified $trad = simp_to_trad($simp); # Simplified to Traditional # All functions may be used in void context to transform $_[0] big5_to_gb($string); # transform $string from big5 to gb =head1 DESCRIPTION This Perl-based module provides part of the functionality of B, namely the C and C functions. The implementation is straightforward, and is significantly slower than the XS-based B; all Unicode-related functions are disabled for pre-5.6.0 perls as well. This module should only be used in perl 5.7.2 or below. =head1 SEE ALSO L The L and L utilities installed with this module. =head1 AUTHORS Audrey Tang Ecpan@audreyt.orgE =head1 COPYRIGHT Copyright 2002, 2003, 2004 by Audrey Tang Ecpan@audreyt.orgE. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L =cut