1package EncodeUpdate; 2use strict; 3use Encode (); 4use Exporter; 5our @ISA = qw(Encode); 6our @EXPORT; 7 8# This module overrides some Encode.pm functions when its version is less than 2.23 to pass our tests. 9 10sub find_encoding($;$) { 11 my ( $name, $skip_external ) = @_; 12 return __PACKAGE__->getEncoding( $name, $skip_external ); 13} 14 15sub encode ($$;$) { 16 my ( $name, $string, $check ) = @_; 17 return undef unless defined $string; 18 $string .= '' if ref $string; # stringify; 19 $check ||= 0; 20 my $enc = find_encoding($name); 21 unless ( defined $enc ) { 22 require Carp; 23 Carp::croak("Unknown encoding '$name'"); 24 } 25 my $octets = $enc->encode( $string, $check ); 26 $_[1] = $string if $check and !ref $check and !( $check & LEAVE_SRC() ); 27 return $octets; 28} 29 30sub decode ($$;$) { 31 my ( $name, $octets, $check ) = @_; 32 return undef unless defined $octets; 33 $octets .= '' if ref $octets; 34 $check ||= 0; 35 my $enc = find_encoding($name); 36 unless ( defined $enc ) { 37 require Carp; 38 Carp::croak("Unknown encoding '$name'"); 39 } 40 my $string = $enc->decode( $octets, $check ); 41 $_[1] = $octets if $check and !ref $check and !( $check & LEAVE_SRC() ); 42 return $string; 43} 44 45sub from_to($$$;$) { 46 my ( $string, $from, $to, $check ) = @_; 47 return undef unless defined $string; 48 $check ||= 0; 49 my $f = find_encoding($from); 50 unless ( defined $f ) { 51 require Carp; 52 Carp::croak("Unknown encoding '$from'"); 53 } 54 my $t = find_encoding($to); 55 unless ( defined $t ) { 56 require Carp; 57 Carp::croak("Unknown encoding '$to'"); 58 } 59 my $uni = $f->decode($string); 60 $_[0] = $string = $t->encode( $uni, $check ); 61 return undef if ( $check && length($uni) ); 62 return defined( $_[0] ) ? length($string) : undef; 63} 64 65my $encver = ($Encode::VERSION =~ /^([\d\.]+)/)[0]; 66if ($encver < 2.23) { 67 *Encode::encode = \&encode; 68 *Encode::decode = \&decode; 69 *Encode::from_to = \&from_to; 70} 71 721; 73