blib/lib/MARC/Charset/Table.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 83.3% 5 / 6 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 17 / 17 Podpod 100% 8 / 8 totaltotal 89.4% 85 / 95 CC 6 SCAR 3.9
CC
6
Cov
91%
new
2.1
get_code
2.1
brand_new
2.1
CRAP 1.5
« blib/lib/MARC/Charset/Constants.pm ↑ Summary build_db.PL »
1 package MARC::Charset::Table;
2  
3 =head1 NAME
4  
5 MARC::Charset::Table - character mapping db
6  
7 =head1 SYNOPSIS
8  
9 use MARC::Charset::Table;
10 use MARC::Charset::Constants qw(:all);
11  
12 # create the table object
13 my $table = MARC::Charset::Table->new();
14
15 # get a code using the marc8 character set code and the character
16 my $code = $table->lookup_by_marc8(CYRILLIC_BASIC, 'K');
17  
18 # get a code using the utf8 value
19 $code = $table->lookup_by_utf8(chr(0x043A));
20  
21 =head1 DESCRIPTION
22  
23 MARC::Charset::Table is a wrapper around the character mapping database,
24 which is implemented as a tied hash on disk. This database gets generated
25 by Makefile.PL on installation of MARC::Charset using
26 MARC::Charset::Compiler.
27  
28 The database is essentially a key/value mapping where a key is a
29 MARC-8 character set code + a MARC-8 character, or an integer representing the
30 UCS code point. These keys map to a serialized MARC::Charset::Code object.
31  
32 =cut
33  
34 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 18 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
38 18 @AnyDBM_File::ISA = qw(GDBM_File DB_File NDBM_File ODBM_File SDBM_File);
39 # SDBM_File is last on the list because it produces the largest database
40 # on disk.
41 }
42 18 ▶use AnyDBM_File;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 18 ▶use MARC::Charset::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 18 ▶use MARC::Charset::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 18 ▶use Storable qw(nfreeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 =head2 new()
48  
49 The consturctor.
50  
51 =cut
52  
53 sub new
54 17 ▶{
Subroutine new: called CC=2 SCAR=7.4
Pod documented
55 17 my $class = shift;
56 17 ▶ my $self = bless {}, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0170
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
57 17 $self->_init(O_RDONLY);
58 17 return $self;
59 }
60  
61  
62 =head2 add_code()
63  
64 Add a MARC::Charset::Code to the table.
65  
66 =cut
67  
68  
69 sub add_code
70 16407 ▶{
Subroutine add_code: called CC=2 SCAR=6.9
Pod documented
71 16407 my ($self, $code) = @_;
72  
73 # the Code object is serialized
74 16407 my $frozen = nfreeze($code);
75  
76 # to support lookup by marc8 and utf8 values we
77 # stash away the rule in the db using two keys
78 16407 my $marc8_key = $code->marc8_hash_code();
79 16407 my $utf8_key = $code->utf8_hash_code();
80  
81 # stash away the marc8 lookup key
82 16407 $self->{db}->{$marc8_key} = $frozen;
83  
84 # stash away the utf8 lookup key (only if it's not already there!)
85 # this means that the sets that appear in the xml file will have
86 # precedence ascii/ansel. Note that we're using 'defined' instead of
87 # 'exists' because NDBM_File and ODBM_File don't support 'exists'.
88 16407 ▶ $self->{db}->{$utf8_key} = $frozen unless defined $self->{db}->{$utf8_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"db"}{$utf8_key} 1 16406 16407
89 }
90  
91  
92 =head2 get_code()
93  
94 Retrieve a code using a hash key.
95  
96 =cut
97  
98 sub get_code
99 480 ▶{
Subroutine get_code: called CC=2 SCAR=7.4
Pod documented
100 480 my ($self, $key) = @_;
101 480 my $db = $self->db();
102 480 my $frozen = $db->{$key};
103 480 ▶ return thaw($frozen) if $frozen;
Branch: 1 branch
BranchTrueFalseTotal
if $frozen 480 0 480
104 0 return;
105 }
106  
107  
108 =head2 lookup_by_marc8()
109  
110 Looks up MARC::Charset::Code entry using a character set code and a MARC-8
111 value.
112  
113 use MARC::Charset::Constants qw(HEBREW);
114 $code = $table->lookup_by_marc8(HEBREW, chr(0x60));
115  
116 =cut
117  
118 sub lookup_by_marc8
119 367 ▶{
Subroutine lookup_by_marc8: called CC=2 SCAR=6.9
Pod documented
120 367 my ($self, $charset, $marc8) = @_;
121 367 ▶ $charset = BASIC_LATIN if $charset eq ASCII_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq "s" 178 189 367
122 367 return $self->get_code(sprintf('%s:%s', $charset, $marc8));
123 }
124  
125  
126 =head2 lookup_by_utf8()
127  
128 Looks up a MARC::Charset::Code object using a utf8 value.
129  
130 =cut
131  
132 sub lookup_by_utf8
133 111 ▶{
Subroutine lookup_by_utf8: called CC=1 SCAR=0.0
Pod documented
134 111 my ($self, $value) = @_;
135 111 return $self->get_code(ord($value));
136 }
137  
138  
139  
140  
141 =head2 db()
142  
143 Returns a reference to a tied character database. MARC::Charset::Table
144 wraps access to the db, but you can get at it if you want.
145  
146 =cut
147  
148 sub db
149 481 ▶{
Subroutine db: called CC=1 SCAR=0.0
Pod documented
150 481 return shift->{db};
151 }
152  
153  
154 =head2 db_path()
155  
156 Returns the path to the character encoding database. Can be called
157 statically too:
158  
159 print MARC::Charset::Table->db_path();
160  
161 =cut
162  
163 sub db_path
164 19 ▶{
Subroutine db_path: called CC=1 SCAR=0.0
Pod documented
165 19 my $path = $INC{'MARC/Charset/Table.pm'};
166 19 $path =~ s/\.pm$//;
167 19 return $path;
168 }
169  
170  
171 =head2 brand_new()
172  
173 An alternate constructor which removes the existing database and starts
174 afresh. Be careful with this one, it's really only used on MARC::Charset
175 installation.
176  
177 =cut
178  
179 sub brand_new
180 1 ▶{
Subroutine brand_new: called CC=2 SCAR=7.4
Pod documented
181 1 my $class = shift;
182 1 ▶ my $self = bless {}, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
183 1 $self->_init(O_CREAT|O_RDWR);
184  
185 1 return $self;
186 }
187  
188  
189 # helper function for initializing table internals
190  
191 sub _init
192 18 ▶{
Subroutine _init: called CC=1 SCAR=0.0
193 18 my ($self, $dbm_opts) = @_;
194 18 tie my %db, 'AnyDBM_File', db_path(), $dbm_opts, 0644; ## no critic (ValuesAndExpressions::ProhibitLeadingZeros)
195 18 $self->{db} = \%db;
196 }
197  
198  
199  
200  
201  
202 18 1;
« blib/lib/MARC/Charset/Constants.pm ↑ Summary build_db.PL »