| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Unicode::Unihan; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 70067 | use 5.008001; | 
|  | 2 |  |  |  |  | 17 |  | 
| 4 | 2 |  |  | 2 |  | 12 | use strict; | 
|  | 2 |  |  |  |  | 10 |  | 
|  | 2 |  |  |  |  | 45 |  | 
| 5 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 99 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.044'; | 
| 8 |  |  |  |  |  |  | our $DEBUG = 0; | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 2 |  |  | 2 |  | 22 | use Carp; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 150 |  | 
| 11 | 2 |  |  | 2 |  | 100 | BEGIN{  @AnyDBM_File::ISA = qw(DB_File GDBM_File SDBM_File) ; } | 
| 12 | 2 |  |  | 2 |  | 974 | use AnyDBM_File; | 
|  | 2 |  |  |  |  | 6105 |  | 
|  | 2 |  |  |  |  | 93 |  | 
| 13 | 2 |  |  | 2 |  | 14 | use Fcntl; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 1481 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub new($;){ | 
| 16 | 2 |  |  | 2 | 0 | 590 | my $class = shift; | 
| 17 | 2 |  |  |  |  | 4 | my $dir = __FILE__; $dir =~ s/\.pm//o; | 
|  | 2 |  |  |  |  | 10 |  | 
| 18 | 2 | 50 |  |  |  | 47 | -d $dir or die "DB Directory $dir nonexistent!"; | 
| 19 | 2 |  |  |  |  | 15 | return bless { '_dir_' => $dir, @_ } => $class; | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub load($$){ | 
| 23 | 88 |  |  | 88 | 0 | 184 | my ($self, $name) = @_; | 
| 24 | 88 | 50 |  |  |  | 221 | if ($self->{'-savemem'}){ | 
| 25 | 0 |  |  |  |  | 0 | for my $k (keys %$self){ | 
| 26 | 0 | 0 |  |  |  | 0 | $k eq $name and next; | 
| 27 | 0 | 0 |  |  |  | 0 | $k =~ /^[A-Z]/o and delete $self->{$k}; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | } | 
| 30 | 88 | 50 |  |  |  | 194 | unless ( $self->{$name} ){ | 
| 31 | 88 |  |  |  |  | 240 | my $file = $self->{_dir_} . "/$name.db"; | 
| 32 |  |  |  |  |  |  | # SDBM files attach a .dir and .pag (two files), so allow for | 
| 33 |  |  |  |  |  |  | # that secret .dir at the end | 
| 34 | 88 | 50 | 33 |  |  | 3962 | (-f $file or -f "$file.dir") or croak "There is no DB for $name"; | 
| 35 | 88 | 50 |  |  |  | 257 | tie %{$self->{$name}}, 'AnyDBM_File', $file, O_RDONLY, 0444 | 
|  | 88 |  |  |  |  | 4336 |  | 
| 36 |  |  |  |  |  |  | or die "$file: $!"; | 
| 37 |  |  |  |  |  |  | } | 
| 38 | 88 |  |  |  |  | 284 | $self; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub unload($;){ | 
| 42 | 0 |  |  | 0 | 0 | 0 | my $self = shift; | 
| 43 | 0 | 0 |  |  |  | 0 | if (@_){ | 
| 44 | 0 |  |  |  |  | 0 | while(my $k = shift) { | 
| 45 | 0 | 0 |  |  |  | 0 | $k =~ /^[A-Z]/o and delete $self->{$k}; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | }else{ | 
| 48 | 0 |  |  |  |  | 0 | for my $k (keys %$self){ | 
| 49 | 0 | 0 |  |  |  | 0 | $k =~ /^[A-Z]/o and delete $self->{$k}; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | } | 
| 52 | 0 |  |  |  |  | 0 | $self; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub DESTROY { | 
| 56 | 2 | 50 |  | 2 |  | 2422 | $DEBUG and warn "$_[0] destroyed!"; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub AUTOLOAD { | 
| 60 | 88 |  |  | 88 |  | 84186 | my $self = shift; | 
| 61 | 88 |  |  |  |  | 159 | my $name = our $AUTOLOAD; | 
| 62 | 88 |  |  |  |  | 556 | $name =~ s/.*:://o; | 
| 63 | 88 |  |  |  |  | 294 | $self->load($name); | 
| 64 | 2 |  |  | 2 |  | 15 | no strict 'refs'; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 407 |  | 
| 65 |  |  |  |  |  |  | *$AUTOLOAD = sub { | 
| 66 | 88 | 50 |  | 88 |  | 155 | my $self = shift; @_ or return; | 
|  | 88 |  |  |  |  | 192 |  | 
| 67 | 88 | 50 |  |  |  | 138 | my $str = shift;  length($str) or return; | 
|  | 88 |  |  |  |  | 164 |  | 
| 68 | 88 | 50 |  |  |  | 177 | if (wantarray){ | 
| 69 | 0 |  |  |  |  | 0 | my @result = (); | 
| 70 | 0 |  |  |  |  | 0 | for my $ord (unpack("U*", $str)){ | 
| 71 | 0 |  |  |  |  | 0 | push @result, $self->{$name}{$ord}; | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 0 |  |  |  |  | 0 | return @result; | 
| 74 |  |  |  |  |  |  | }else{ | 
| 75 | 88 |  |  |  |  | 3189 | return $self->{$name}{ord($str)}; | 
| 76 |  |  |  |  |  |  | } | 
| 77 | 88 |  |  |  |  | 681 | }; | 
| 78 | 88 |  |  |  |  | 323 | return $self->$name(@_); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | 1; | 
| 82 |  |  |  |  |  |  | __END__ |