diff options
author | Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> | 2017-12-28 17:24:43 +0000 |
---|---|---|
committer | Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> | 2017-12-28 18:06:51 +0000 |
commit | b788e8d877836b749025872e40ca34429cd45f92 (patch) | |
tree | 09696cc52f4f1ff036401490c445102bc1c71705 /lib | |
parent | 355c3ab756cb5e5a562d9172ca66afcebe30a822 (diff) | |
download | perl-b788e8d877836b749025872e40ca34429cd45f92.tar.gz |
Simplify Unicode::UCD::openunicode() and callers
Get rid of the file-global filehandles and the unused filename return
value, instead return the filehandle and assign it to a lexical
variable. Also don't bother checking the return value; it croaks on
failure anyway.
In passing, eliminate erroneous assignment of {} to %CASESPEC for
Unicode < 2.1.8.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Unicode/UCD.pm | 70 |
1 files changed, 27 insertions, 43 deletions
diff --git a/lib/Unicode/UCD.pm b/lib/Unicode/UCD.pm index 2021680987..6733e119ea 100644 --- a/lib/Unicode/UCD.pm +++ b/lib/Unicode/UCD.pm @@ -140,28 +140,18 @@ Note that the largest code point in Unicode is U+10FFFF. =cut -my $BLOCKSFH; -my $VERSIONFH; -my $CASEFOLDFH; -my $CASESPECFH; -my $NAMEDSEQFH; my $v_unicode_version; # v-string. sub openunicode { - my ($rfh, @path) = @_; - my $f; - unless (defined $$rfh) { - for my $d (@INC) { - use File::Spec; - $f = File::Spec->catfile($d, "unicore", @path); - last if open($$rfh, '<', $f); - undef $f; - } - croak __PACKAGE__, ": failed to find ", - File::Spec->catfile(@path), " in @INC" - unless defined $f; + my (@path) = @_; + my $rfh; + for my $d (@INC) { + use File::Spec; + my $f = File::Spec->catfile($d, "unicore", @path); + return $rfh if open($rfh, '<', $f); } - return $f; + croak __PACKAGE__, ": failed to find ", + File::Spec->catfile("unicore", @path), " in @INC"; } sub _dclone ($) { # Use Storable::dclone if available; otherwise emulate it. @@ -880,10 +870,11 @@ sub _charblocks { push @BLOCKS, $subrange; push @{$BLOCKS{'No_Block'}}, $subrange; } - elsif (openunicode(\$BLOCKSFH, "Blocks.txt")) { + else { + my $blocksfh = openunicode("Blocks.txt"); local $_; local $/ = "\n"; - while (<$BLOCKSFH>) { + while (<$blocksfh>) { # Old versions used a different syntax to mark the range. $_ =~ s/;\s+/../ if $v_unicode_version lt v3.1.0; @@ -895,7 +886,6 @@ sub _charblocks { push @{$BLOCKS{$3}}, $subrange; } } - close($BLOCKSFH); if (! IS_ASCII_PLATFORM) { # The first two blocks, through 0xFF, are wrong on EBCDIC # platforms. @@ -1648,13 +1638,11 @@ my %CASESPEC; sub _casespec { unless (%CASESPEC) { UnicodeVersion() unless defined $v_unicode_version; - if ($v_unicode_version lt v2.1.8) { - %CASESPEC = {}; - } - elsif (openunicode(\$CASESPECFH, "SpecialCasing.txt")) { + if ($v_unicode_version ge v2.1.8) { + my $casespecfh = openunicode("SpecialCasing.txt"); local $_; local $/ = "\n"; - while (<$CASESPECFH>) { + while (<$casespecfh>) { if (/^([0-9A-F]+); ([0-9A-F]+(?: [0-9A-F]+)*)?; ([0-9A-F]+(?: [0-9A-F]+)*)?; ([0-9A-F]+(?: [0-9A-F]+)*)?; (\w+(?: \w+)*)?/) { my ($hexcode, $lower, $title, $upper, $condition) = @@ -1719,7 +1707,6 @@ sub _casespec { } } } - close($CASESPECFH); } } } @@ -1769,19 +1756,17 @@ my %NAMEDSEQ; sub _namedseq { unless (%NAMEDSEQ) { - if (openunicode(\$NAMEDSEQFH, "Name.pl")) { - local $_; - local $/ = "\n"; - while (<$NAMEDSEQFH>) { - if (/^ [0-9A-F]+ \ /x) { - chomp; - my ($sequence, $name) = split /\t/; - my @s = map { chr(hex($_)) } split(' ', $sequence); - $NAMEDSEQ{$name} = join("", @s); - } - } - close($NAMEDSEQFH); - } + my $namedseqfh = openunicode("Name.pl"); + local $_; + local $/ = "\n"; + while (<$namedseqfh>) { + if (/^ [0-9A-F]+ \ /x) { + chomp; + my ($sequence, $name) = split /\t/; + my @s = map { chr(hex($_)) } split(' ', $sequence); + $NAMEDSEQ{$name} = join("", @s); + } + } } } @@ -4121,10 +4106,9 @@ my $UNICODEVERSION; sub UnicodeVersion { unless (defined $UNICODEVERSION) { - openunicode(\$VERSIONFH, "version"); + my $versionfh = openunicode("version"); local $/ = "\n"; - chomp($UNICODEVERSION = <$VERSIONFH>); - close($VERSIONFH); + chomp($UNICODEVERSION = <$versionfh>); croak __PACKAGE__, "::VERSION: strange version '$UNICODEVERSION'" unless $UNICODEVERSION =~ /^\d+(?:\.\d+)+$/; } |