From: Nick Ing-Simmons Date: Sun, 8 Oct 2000 12:54:42 +0000 (+0000) Subject: Make "encodings" work post-install when Encode/*.enc files may not X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5345d506e0f862025d90da43f18289aba83efacd;p=p5sagit%2Fp5-mst-13.2.git Make "encodings" work post-install when Encode/*.enc files may not be in same directory as Encode.pm p4raw-id: //depot/perl@7167 --- diff --git a/ext/Encode/Encode.pm b/ext/Encode/Encode.pm index 6af4548..9d5e07f 100644 --- a/ext/Encode/Encode.pm +++ b/ext/Encode/Encode.pm @@ -340,58 +340,69 @@ sub from_to return length($_[0] = $string); } +my %encoding = ( Unicode => bless({},'Encode::Unicode'), + 'iso10646-1' => bless({},'Encode::iso10646_1'), + ); + sub encodings { my ($class) = @_; - my ($dir) = __FILE__ =~ /^(.*)\.pm$/; - my @names = ('Unicode'); - if (opendir(my $dh,$dir)) + foreach my $dir (@INC) { - while (defined(my $name = readdir($dh))) + if (opendir(my $dh,"$dir/Encode")) { - push(@names,$1) if ($name =~ /^(.*)\.enc$/); + while (defined(my $name = readdir($dh))) + { + if ($name =~ /^(.*)\.enc$/) + { + next if exists $encoding{$1}; + $encoding{$1} = "$dir/$name"; + } + } + closedir($dh); } - closedir($dh); + } + return keys %encoding; +} + +sub loadEncoding +{ + my ($class,$name,$file) = @_; + if (open(my $fh,$file)) + { + my $type; + while (1) + { + my $line = <$fh>; + $type = substr($line,0,1); + last unless $type eq '#'; + } + $class .= ('::'.(($type eq 'E') ? 'Escape' : 'Table')); + return $class->read($fh,$name,$type); } else { - die "Cannot open $dir:$!"; + return undef; } - return @names; } -my %encoding = ( Unicode => bless({},'Encode::Unicode'), - 'iso10646-1' => bless({},'Encode::iso10646_1'), - ); - sub getEncoding { my ($class,$name) = @_; - unless (exists $encoding{$name}) + my $enc; + unless (ref($enc = $encoding{$name})) { - my $file; - foreach my $dir (@INC) - { - last if -f ($file = "$dir/Encode/$name.enc"); - } - if (open(my $fh,$file)) + $enc = $class->loadEncoding($name,$enc) if defined $enc; + unless (ref($enc)) { - my $type; - while (1) + foreach my $dir (@INC) { - my $line = <$fh>; - $type = substr($line,0,1); - last unless $type eq '#'; + last if ($enc = $class->loadEncoding($name,"$dir/Encode/$name.enc")); } - $class .= ('::'.(($type eq 'E') ? 'Escape' : 'Table')); - $encoding{$name} = $class->read($fh,$name,$type); - } - else - { - $encoding{$name} = undef; } + $encoding{$name} = $enc; } - return $encoding{$name}; + return $enc; } package Encode::Unicode;