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   59260 use 5.008001;
  2         18  
4 2     2   10 use strict;
  2         9  
  2         40  
5 2     2   8 use warnings;
  2         2  
  2         92  
6              
7             our $VERSION = '0.042_02';
8             our $DEBUG = 0;
9              
10 2     2   17 use Carp;
  2         3  
  2         151  
11 2     2   84 BEGIN{ @AnyDBM_File::ISA = qw(DB_File GDBM_File SDBM_File) ; }
12 2     2   735 use AnyDBM_File;
  2         502575  
  2         169  
13 2     2   16 use Fcntl;
  2         3  
  2         1369  
14              
15             sub new($;){
16 2     2 0 541 my $class = shift;
17 2         4 my $dir = __FILE__; $dir =~ s/\.pm//o;
  2         11  
18 2 50       62 -d $dir or die "DB Directory $dir nonexistent!";
19 2         16 return bless { '_dir_' => $dir, @_ } => $class;
20             }
21              
22             sub load($$){
23 88     88 0 159 my ($self, $name) = @_;
24 88 50       201 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       158 unless ( $self->{$name} ){
31 88         191 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     3028 (-f $file or -f "$file.dir") or croak "There is no DB for $name";
35 88 50       219 tie %{$self->{$name}}, 'AnyDBM_File', $file, O_RDONLY, 0444
  88         3600  
36             or die "$file: $!";
37             }
38 88         247 $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   2364 $DEBUG and warn "$_[0] destroyed!";
57             }
58              
59             sub AUTOLOAD {
60 88     88   66484 my $self = shift;
61 88         122 my $name = our $AUTOLOAD;
62 88         499 $name =~ s/.*:://o;
63 88         246 $self->load($name);
64 2     2   13 no strict 'refs';
  2         4  
  2         320  
65             *$AUTOLOAD = sub {
66 88 50   88   118 my $self = shift; @_ or return;
  88         168  
67 88 50       124 my $str = shift; length($str) or return;
  88         141  
68 88 50       162 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         2591 return $self->{$name}{ord($str)};
76             }
77 88         588 };
78 88         277 return $self->$name(@_);
79             }
80              
81             1;
82             __END__