File Coverage

blib/lib/Unicode/Unihan.pm
Criterion Covered Total %
statement 47 61 77.0
branch 9 28 32.1
condition 1 3 33.3
subroutine 13 14 92.8
pod 0 3 0.0
total 70 109 64.2


line stmt bran cond sub pod time code
1             package Unicode::Unihan;
2              
3 2     2   58741 use 5.008001;
  2         14  
4 2     2   10 use strict;
  2         8  
  2         41  
5 2     2   9 use warnings;
  2         2  
  2         96  
6              
7             our $VERSION = '0.043';
8             our $DEBUG = 0;
9              
10 2     2   11 use Carp;
  2         2  
  2         175  
11 2     2   88 BEGIN{ @AnyDBM_File::ISA = qw(DB_File GDBM_File SDBM_File) ; }
12 2     2   812 use AnyDBM_File;
  2         5341  
  2         94  
13 2     2   13 use Fcntl;
  2         2  
  2         1230  
14              
15             sub new($;){
16 2     2 0 614 my $class = shift;
17 2         5 my $dir = __FILE__; $dir =~ s/\.pm//o;
  2         10  
18 2 50       43 -d $dir or die "DB Directory $dir nonexistent!";
19 2         18 return bless { '_dir_' => $dir, @_ } => $class;
20             }
21              
22             sub load($$){
23 88     88 0 134 my ($self, $name) = @_;
24 88 50       187 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       180 unless ( $self->{$name} ){
31 88         520 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     2839 (-f $file or -f "$file.dir") or croak "There is no DB for $name";
35 88 50       217 tie %{$self->{$name}}, 'AnyDBM_File', $file, O_RDONLY, 0444
  88         3522  
36             or die "$file: $!";
37             }
38 88         249 $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   2331 $DEBUG and warn "$_[0] destroyed!";
57             }
58              
59             sub AUTOLOAD {
60 88     88   65360 my $self = shift;
61 88         118 my $name = our $AUTOLOAD;
62 88         459 $name =~ s/.*:://o;
63 88         219 $self->load($name);
64 2     2   14 no strict 'refs';
  2         4  
  2         326  
65             *$AUTOLOAD = sub {
66 88 50   88   123 my $self = shift; @_ or return;
  88         162  
67 88 50       117 my $str = shift; length($str) or return;
  88         156  
68 88 50       164 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         2585 return $self->{$name}{ord($str)};
76             }
77 88         622 };
78 88         294 return $self->$name(@_);
79             }
80              
81             1;
82             __END__