MARC.pm

filter: Statementstmt 81.2% 1334 / 1642 Branchbran 63.3% 437 / 690 Conditioncond 54.6% 118 / 216 MC/DCmcdc 28.3% 34 / 120 Subroutinesub 77.1% 91 / 118 Podpod 85.0% 34 / 40 totaltotal 72.4% 2048 / 2826 CC 484 SCAR 37.2
CC
484
Cov
74%
output
506.0
_bib_format
105.9
updaterecord
90.0
CRAP 41.3
↑ Summary
1 package MARC;
2  
3 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG $TEST);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 $VERSION = '1.07';
8 5 $MARC::DEBUG = 0;
9 5 $MARC::TEST = 0;
10  
11 5 require Exporter;
12 5 require 5.004;
13  
14 5 @ISA = qw(Exporter);
15 5 @EXPORT= qw();
16 5 @EXPORT_OK= qw();
17  
18 #### Not using these yet
19  
20 #### %EXPORT_TAGS = (USTEXT => [qw( marc2ustext )]);
21 #### Exporter::export_ok_tags('USTEXT');
22 #### $EXPORT_TAGS{ALL} = \@EXPORT_OK;
23  
24  
25 # Preloaded methods go here.
26  
27 4 ▶sub mycarp { # rec
Subroutine mycarp: called CC=2 SCAR=7.6
Pod undocumented
28 4 ▶ Carp::carp (@_) unless $MARC::TEST;
Branch: 1 branch
BranchTrueFalseTotal
unless $MARC::TEST 0 4 4
29 }
30  
31 ####################################################################
32 # This is the constructor method that creates the MARC object. It #
33 # will call the appropriate read using the file and format #
34 # parameters that are passed. #
35 ####################################################################
36 5 ▶sub new { # rec
Subroutine new: called CC=16 SCAR=31.5
Pod documented
37 5 my $proto = shift;
38 5 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
39 5 my $file = shift;
40 5 my $marc = [];
41 5 my $totalrecord;
42 5 $marc->[0]{'increment'}=-1; #store the default increment in the object
43 5 my $proto_rec;
44 # print STDERR "foo\n";
45 {
46 # We are going to look for related classes in Perl's
47 # symbol table. This is a little tricky.
48 # Shoot me.
49
50 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 # Next, we set up a symbolic reference.
52 5 my $g = $ {$class.'::Rec::VERSION'}; # space for emacs.
53 # That was a sample of Perl reflection. Yup, what Smalltalk
54 # does with Class and MetaClass, Perl does with strings.
55 # Not much structure, but also not much fuss.
56  
57 5 ▶ my $rec_class = $class."::Rec" if $g;
Branch: 1 branch
BranchTrueFalseTotal
if $g 4 1 5
58 # Now we will use the related Rec class if it exists.
59 5 ▶ $rec_class ||= "MARC::Rec";
Condition100%2/2
$rec_class ||= "MARC::Rec"
l!l
41
MC/DC100%1/1
$rec_class ||= "MARC::Rec"
$rec_class
Truth table100%2/2
A
A= $rec_class
Aresult
11
00
60
61 5 $proto_rec = $rec_class->new();
62 }
63  
64 5 $marc->[0]{'proto_rec'}=$proto_rec; # Used for future manipulations.
65 5 bless ($marc, $class);
66 # bless early so _readxxx can use methods
67 #if file isn't defined then just return the empty MARC object
68 5 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 5 0 5
69 0 ▶ unless (-e $file) {mycarp "File $file doesn't exist"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 5 5
70 #if the file doesn't exist return an error
71 5 ▶ my $format = shift || "usmarc";
Condition100%2/2
shift() || 'usmarc'
l!l
14
MC/DC100%1/1
shift() || 'usmarc'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
72 # $format defaults to USMARC if undefined
73 5 ▶ open(*file, $file) or mycarp "Open Error: $file, $!";
Branch: 1 branch
BranchTrueFalseTotal
open *file, $file or mycarp("Open Error: $file, $!") 5 0 5
74 5 binmode *file;
75 5 $marc->[0]{'handle'}=\*file;
76 5 $proto_rec->{'handle'} = $marc->[0]{'handle'};
77 5 $proto_rec->{'format'} = lc $format;
78 5 ▶ if ($format =~ /usmarc$/io) {
Branch: 4 branches
BranchTrueFalseTotal
if ($format =~ /usmarc$/io) { } 4 1 5
elsif ($format =~ /unimarc$/io) { } 0 1 1
elsif ($format =~ /marcmaker$/io) { } 1 0 1
elsif ($format =~ /xml/io) { } 0 0 0
79 4 $marc->[0]{'format'}='usmarc';
80 4 $totalrecord = _readmarc($marc);
81 4 ▶ close *file or mycarp "Close Error: $file, $!";
Branch: 1 branch
BranchTrueFalseTotal
close file or mycarp("Close Error: $file, $!") 4 0 4
82 }
83 elsif ($format =~ /unimarc$/io) {
84 0 $marc->[0]{'format'}='unimarc';
85 0 $totalrecord = _readmarc($marc);
86 0 ▶ close *file or mycarp "Close Error: $file, $!";
Branch: 1 branch
BranchTrueFalseTotal
close file or mycarp("Close Error: $file, $!") 0 0 0
87 }
88 elsif ($format =~ /marcmaker$/io) {
89 1 $marc->[0]{'lineterm'}="\015\012"; # MS-DOS default for MARCMaker
90 1 $totalrecord = _readmarcmaker($marc);
91 1 ▶ close *file or mycarp "Close Error: $file, $!";
Branch: 1 branch
BranchTrueFalseTotal
close file or mycarp("Close Error: $file, $!") 1 0 1
92 }
93 elsif ($format =~ /xml/oi) {
94 0 mycarp "XML formats are now handled by MARC::XML";
95 0 return;
96 }
97 else {
98 0 mycarp "I don't recognize format $format";
99 0 return;
100 }
101 }
102 5 ▶ print "read in $totalrecord records\n" if $MARC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::DEBUG 0 5 5
103 5 return $marc;
104 }
105 ####################################################################
106  
107 # clone returns a new MARC object with copies of the data.
108 # Admin information remains linked to original.
109  
110 ####################################################################
111  
112 0 ▶sub clone {
Subroutine clone: not called CC=3 SCAR=24.8
Pod undocumented
113 0 my $marc = shift;
114 0 ▶ my $class = shift || ref $marc;
Condition0%0/3
shift() || ref $marc
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || ref $marc
shift() ref $marc
Truth table0%0/3
A || B
A= shift()B= ref $marc
ABresult
1X1
011
000
115 0 my $ans = $marc->new;
116 0 bless $ans, $class;
117 0 $ans->[0] = $marc->[0];
118 0 foreach my $i (1..$#$marc) {
119 0 my $rec = $marc->[$i];
120  
121 0 my $newrec = $rec->clone();
122 0 bless $newrec, $class."::Rec";
123 0 push @$ans, $newrec;
124 }
125 0 return $ans;
126 }
127  
128 ###################################################################
129 # _readmarc() reads in a MARC file into the $marc object #
130 ###################################################################
131 8 ▶sub _readmarc { # also rec
Subroutine _readmarc: called CC=8 SCAR=20.8
132 8 my $marc = shift;
133 8 my $handle = $marc->[0]{'handle'};
134 8 my $proto_rec = $marc->[0]{'proto_rec'};
135 8 my $increment = $marc->[0]{'increment'}; #pick out increment from the object
136 8 my $recordcount = 0;
137  
138 8 ▶ while ($increment==-1 || $recordcount<$increment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($increment == -1 or $recordcount < $increment) 17 1 18
Condition100%3/3
$increment == -1 or $recordcount < $increment
l!l&&r!l&&!r
1251
MC/DC100%2/2
$increment == -1 or $recordcount < $increment
$increment == -1 $recordcount < $increment
Truth table100%3/3
A or B
A= $increment == -1B= $recordcount < $increment
ABresult
1X1
011
000
139 17 my ($rec,$status)=$proto_rec->_readmarc();
140 17 ▶ last unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 5 12 17
141 12 ▶ if ($status == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == -1) 1 11 12
142 1 mycarp "Invalid record, size does not match leader";
143 1 ▶ return unless $recordcount; # undef if first
Branch: 1 branch
BranchTrueFalseTotal
unless $recordcount 1 0 1
144 0 return -$recordcount; # if some are valid
145 }
146 11 ▶ if ($status == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == -2) 1 10 11
147 1 mycarp "Invalid record, leader size not numeric";
148 1 ▶ return unless $recordcount; # undef if first
Branch: 1 branch
BranchTrueFalseTotal
unless $recordcount 0 1 1
149 1 return -$recordcount; # if some are valid
150 }
151 10 push @$marc, $rec;
152 10 $recordcount++;
153 } #end processing this record
154 6 return $recordcount;
155 }
156  
157 ###################################################################
158 # readmarcmaker() reads a marcmaker file into the MARC object #
159 ###################################################################
160 5 ▶sub _readmarcmaker { # rec
Subroutine _readmarcmaker: called CC=7 SCAR=19.5
161 5 my $marc = shift;
162 5 my $handle = $marc->[0]{'handle'};
163 5 my $proto_rec = $marc->[0]{'proto_rec'};
164 5 my $increment = $marc->[0]{'increment'}; #pick out increment from the object
165 5 ▶ unless (exists $marc->[0]{'makerchar'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $marc->[0]{"makerchar"}) 1 4 5
166 1 $marc->[0]{'makerchar'} = usmarc_default(); # hash ref
167 1 $proto_rec->{'makerchar'} = $marc->[0]{'makerchar'};
168 }
169 5 my $recordcount = 0;
170  
171 5 ▶ while ($increment==-1 or $recordcount<$increment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($increment == -1 or $recordcount < $increment) 15 2 17
Condition100%3/3
$increment == -1 or $recordcount < $increment
l!l&&r!l&&!r
962
MC/DC100%2/2
$increment == -1 or $recordcount < $increment
$increment == -1 $recordcount < $increment
Truth table100%3/3
A or B
A= $increment == -1B= $recordcount < $increment
ABresult
1X1
011
000
172 15 my ($rec,$status) = $proto_rec->_readmarcmaker();
173 15 ▶ last unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 1 14 15
174 14 ▶ if ($status == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == -1) 2 12 14
175 2 mycarp 'Invalid record, prefix "=LDR " not found';
176 2 ▶ return unless $recordcount; # undef if first
Branch: 1 branch
BranchTrueFalseTotal
unless $recordcount 1 1 2
177 1 return -$recordcount; # if some are valid
178 }
179 12 push @$marc, $rec;
180 12 $recordcount++;
181 } #end reading this record
182 3 return $recordcount;
183 }
184  
185 329 ▶sub _maker2char { # rec
Subroutine _maker2char: called CC=1 SCAR=0.0
186 329 return MARC::Rec::_maker2char(@_);
187 }
188  
189 1 ▶sub usmarc_default { # rec
Subroutine usmarc_default: called CC=1 SCAR=0.0
Pod documented
190 1 return MARC::Rec::usmarc_default(@_);
191 }
192  
193 ####################################################################
194 # marc_count() returns the number of records in a #
195 # particular MARC object #
196 ####################################################################
197 10 ▶sub marc_count {
Subroutine marc_count: called CC=1 SCAR=0.0
Pod documented
198 10 my $marc=shift;
199 10 return $#$marc;
200 }
201  
202 ####################################################################
203 # openmarc() is a method for reading in a MARC file. It takes #
204 # several parameters: file (name of the marc file) ; format, ie. #
205 # usmarc ; and increment which defines how many records to read in #
206 ####################################################################
207 3 ▶sub openmarc {
Subroutine openmarc: called CC=13 SCAR=29.9
Pod documented
208 3 my $marc=shift;
209 3 my $params=shift;
210 3 my $file=$params->{'file'};
211 0 ▶ if (not(-e $file)) {mycarp "File \"$file\" doesn't exist"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 3 3
212 3 $marc->[0]{'format'}=$params->{'format'}; #store format in object
213 3 my $totalrecord;
214 3 ▶ $marc->[0]{'increment'}=$params->{'increment'} || 0;
Condition100%2/2
$params->{'increment'} || 0
l!l
12
MC/DC100%1/1
$params->{'increment'} || 0
$params->{'increment'}
Truth table100%2/2
A
A= $params->{'increment'}
Aresult
11
00
215 #store increment in the object, default is 0
216 0 ▶ unless ($marc->[0]{'format'}) {$marc->[0]{'format'}="usmarc"}; #default to usmarc
Branch: 1 branch
BranchTrueFalseTotal
unless ($marc->[0]{"format"}) 0 3 3
217 3 ▶ open(*file, $file) or mycarp "Open Error: $file, $!";
Branch: 1 branch
BranchTrueFalseTotal
open *file, $file or mycarp("Open Error: $file, $!") 3 0 3
218 3 binmode *file;
219 3 $marc->[0]{'handle'}=\*file; #store filehandle in object
220 3 my $proto_rec = $marc->[0]{'proto_rec'};
221 3 $proto_rec->{'handle'} = $marc->[0]{'handle'};
222 3 $proto_rec->{'format'} = lc $marc->[0]{'format'};
223 3 ▶ if ($marc->[0]{'format'} =~ /usmarc/oi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($marc->[0]{'format'} =~ /usmarc/io) { } 1 2 3
elsif ($marc->[0]{'format'} =~ /marcmaker/io) { } 2 0 2
224 1 $totalrecord = _readmarc($marc);
225 }
226 elsif ($marc->[0]{'format'} =~ /marcmaker/oi) {
227 2 ▶ if (exists $params->{'charset'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{'charset'}) { } 0 2 2
228 0 $marc->[0]{makerchar} = $params->{'charset'}; # hash ref
229 }
230 else {
231 2 ▶ unless (exists $marc->[0]{'makerchar'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $marc->[0]{"makerchar"}) 0 2 2
232 0 $marc->[0]{makerchar} = usmarc_default(); # hash ref
233 }
234 }
235 2 ▶ $marc->[0]{'lineterm'} = $params->{'lineterm'} || "\015\012";
Condition50%1/2
$params->{'lineterm'} || "\r\n"
l!l
02
MC/DC0%0/1
$params->{'lineterm'} || "\r\n"
$params->{'lineterm'}
Truth table50%1/2
A
A= $params->{'lineterm'}
Aresult
11
00
236 2 $totalrecord = _readmarcmaker($marc);
237 }
238 else {
239 0 close *file;
240 0 ▶ if ($params->{'format'} =~ /xml/oi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'format'} =~ /xml/io) { } 0 0 0
241 0 mycarp "XML formats are now handled by MARC::XML";
242 }
243 else {
244 0 mycarp "Unrecognized format $marc->[0]{'format'}";
245 }
246 0 return;
247 }
248 3 ▶ print "read in $totalrecord records\n" if $MARC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::DEBUG 0 3 3
249 3 ▶ if ($totalrecord==0) {$totalrecord="0 but true"}
Branch: 1 branch
BranchTrueFalseTotal
if ($totalrecord == 0) 2 1 3
250 3 return $totalrecord;
251 }
252  
253 ####################################################################
254 # closemarc() will close a file-handle that was opened with #
255 # openmarc() #
256 ####################################################################
257 3 ▶sub closemarc {
Subroutine closemarc: called CC=2 SCAR=7.5
Pod documented
258 3 my $marc = shift;
259 3 $marc->[0]{'increment'}=0;
260 3 ▶ if (not($marc->[0]{'handle'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($marc->[0]{"handle"}) 0 3 3
261 0 mycarp "There isn't a MARC file to close";
262 0 return;
263 }
264 3 my $ok = close $marc->[0]{'handle'};
265 3 $marc->[0]{'handle'}=undef;
266 3 return $ok;
267 }
268  
269 ####################################################################
270 # nextmarc() will read in more records from a file that has #
271 # already been opened with openmarc(). the increment can be #
272 # adjusted if necessary by passing a new value as a parameter. the #
273 # new records will be APPENDED to the MARC object #
274 ####################################################################
275 5 ▶sub nextmarc {
Subroutine nextmarc: called CC=5 SCAR=17.2
Pod documented
276 5 my $marc=shift;
277 5 my $increment=shift;
278 5 my $totalrecord;
279 5 ▶ if (not($marc->[0]{'handle'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($marc->[0]{"handle"}) 0 5 5
280 0 mycarp "There isn't a MARC file open";
281 0 return;
282 }
283 5 ▶ if ($increment) {$marc->[0]{'increment'}=$increment}
Branch: 1 branch
BranchTrueFalseTotal
if ($increment) 5 0 5
284 5 ▶ if ($marc->[0]{'format'} =~ /usmarc/oi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($marc->[0]{'format'} =~ /usmarc/io) { } 3 2 5
elsif ($marc->[0]{'format'} =~ /marcmaker/io) { } 2 0 2
285 3 $totalrecord = _readmarc($marc);
286 }
287 elsif ($marc->[0]{'format'} =~ /marcmaker/oi) {
288 2 $totalrecord = _readmarcmaker($marc);
289 }
290 0 else {return}
291 5 return $totalrecord;
292 }
293  
294 ####################################################################
295  
296 # add_map() takes a recnum and a ref to a field in ($tag,
297 # $i1,$i2,a=>"bar",...) or ($tag, $field) formats and will append to
298 # the various indices that we have hanging off that record. It is
299 # intended for use in creating records de novo and as a component for
300 # rebuild_map(). It carefully does not copy subfield values or entire
301 # fields, maintaining some reference relationships. What this means
302 # for indices created with add_map that you can directly edit
303 # subfield values in $marc->[recnum]{array} and the index will adjust
304 # automatically. Vice-versa, if you edit subfield values in
305 # $marc->{recnum}{tag}{subfield_code} the fields in
306 # $marc->[recnum]{array} will adjust. If you change structural
307 # information in the array with such an index, you must rebuild the
308 # part of the index related to the current tag (and possibly the old
309 # tag if you change the tag).
310  
311 ####################################################################
312  
313 25 ▶sub add_map { # rec
Subroutine add_map: called CC=1 SCAR=0.0
Pod documented
314 25 my $marc=shift;
315 25 my $recnum = shift;
316 25 my $rafield = shift;
317 25 $marc->[$recnum]->add_map($rafield);
318 }
319  
320 ####################################################################
321  
322 # rebuild_map() takes a recnum and a tag and will synchronize the
323 # index with all elements in the [recnum]{array} with that tag.
324  
325 ####################################################################
326 2 ▶sub rebuild_map { # rec
Subroutine rebuild_map: called CC=2 SCAR=7.0
Pod documented
327 2 my $marc=shift;
328 2 my $recnum = shift;
329 2 my $tag = shift;
330 2 ▶ return undef if $tag eq '000'; #currently ldr is different...
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "000" 0 2 2
331 2 $marc->[$recnum]->rebuild_map($tag);
332 }
333  
334 ####################################################################
335  
336 # rebuild_map_all() takes a recnum and will synchronize the
337 # index with all elements in the [recnum]{array}
338  
339 ####################################################################
340 0 ▶sub rebuild_map_all { # rec
Subroutine rebuild_map_all: not called CC=1 SCAR=6.9
Pod documented
341 0 my $marc=shift;
342 0 my $recnum = shift;
343 0 $marc->[$recnum]->rebuild_map_all();
344 }
345  
346 ####################################################################
347 # deletemarc() will delete entire records, specific fields, as #
348 # well as specific subfields depending on what parameters are #
349 # passed to it #
350 ####################################################################
351 8 ▶sub deletemarc {
Subroutine deletemarc: called CC=6 SCAR=17.9
Pod documented
352 8 my $marc=shift;
353 8 my $template=shift;
354  
355 8 my $params = _params($template,@_);
356  
357 8 my @delrecords= _records($marc,$params);
358 8 my %delrecords= map {$_=>1} @delrecords;
359 #if records parameter not passed set to all records in MARC object
360 8 my $field=$params->{field};
361 8 my $subfield=$params->{subfield};
362  
363 8 my $deletecount=0;
364 8 my @keepers = grep {!$delrecords{$_}} (0..$#$marc);
365  
366 #delete entire records
367 8 ▶ if (not($field) and not($subfield)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field or $subfield) 3 5 8
Condition66%2/3
$field or $subfield
l!l&&r!l&&!r
503
MC/DC50%1/2
$field or $subfield
$field $subfield
Truth table66%2/3
A or B
A= $fieldB= $subfield
ABresult
1X1
011
000
368 3 my $class = ref $marc;
369 3 my @newmarc = @$marc[@keepers]; # array slice, look it up.
370 3 @$marc=@newmarc;
371 3 bless $marc,$class;
372 3 return @delrecords;
373 }
374  
375 #delete fields and/or subfields. deletefirst takes care of the details.
376 # This may be slow. If so write a loop using deletesubfield, etc.
377  
378 5 foreach my $i (1..$#$marc) {
379 12 ▶ next unless $delrecords{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless $delrecords{$i} 7 5 12
380 5 my $rec=$marc->[$i];
381 5 my @newfields =();
382 5 while (1) {
383 16 my $has_subfield = $rec->deletefirst($template);
384 16 ▶ last unless $has_subfield;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_subfield 5 11 16
385 11 $deletecount++;
386 }
387 5 $rec->rebuild_map($field);
388 }
389 5 return $deletecount;
390 }
391  
392 ####################################################################
393 # selectmarc() performs the opposite function of deletemarc(). It #
394 # will select specified elements of a MARC object and return them #
395 # as a MARC object. So if you wanted to select records 1-10 and 15 #
396 # of a MARC object you could say $x=$x->selectmarc(["1-10","15"]); #
397 ####################################################################
398 1 ▶sub selectmarc {
Subroutine selectmarc: called CC=4 SCAR=14.0
Pod documented
399 1 my $marc=shift;
400 1 my $selarray=shift;
401  
402 1 my @keepers=(0); # so we have admin information.
403 1 foreach my $selelement (@$selarray) {
404 2 ▶ if ($selelement=~/(\d+)-(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selelement =~ /(\d+)-(\d+)/) { } 1 1 2
405 1 push @keepers,($1..$2);
406 } else {
407 1 push @keepers, $selelement;
408 }
409 }
410 0 ▶ if (not($selarray)) {@{$selarray}= (1..$#$marc)}
Branch: 1 branch
BranchTrueFalseTotal
unless ($selarray) 0 1 1
411 1 my $class = ref $marc;
412 1 my @newmarc = @$marc[@keepers]; # array slice, look it up.
413 1 @$marc=@newmarc;
414 1 bless $marc,$class;
415 1 return scalar(@keepers) -1; # minus off the $marc->[0]
416 }
417  
418 ####################################################################
419 # searchmarc() is method for searching a MARC object for specific #
420 # values. It will return an array which contains the record #
421 # numbers that matched. #
422 ####################################################################
423 36 ▶sub searchmarc {
Subroutine searchmarc: called CC=55 SCAR=42.0
Pod documented
424 36 my $marc=shift;
425 36 my $template=shift;
426 36 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 36 36
427 36 my $params = _params($template,@_);
428  
429 36 ▶ my $field=$params->{field} || return;
Condition50%1/2
$params->{'field'} || (return)
l!l
360
MC/DC0%0/1
$params->{'field'} || (return)
$params->{'field'}
Truth table50%1/2
A
A= $params->{'field'}
Aresult
11
00
430 36 my $subfield=$params->{subfield};
431 36 my $regex=$params->{regex};
432 36 my $notregex=$params->{notregex};
433 36 my @results;
434 my $searchtype;
435  
436 #determine the type of search
437 36 ▶ if ($field and not($subfield) and not($regex) and not($notregex)) {
Branch: 6 branches
BranchTrueFalseTotal
if ($field and not $subfield and not $regex and not $notregex) { } 15 21 36
elsif ($field and $subfield and not $regex and not $notregex) { } 13 8 21
elsif ($field and not $subfield and $regex) { } 2 6 8
elsif ($field and $subfield and $regex) { } 2 4 6
elsif ($field and not $subfield and $notregex) { } 2 2 4
elsif ($field and $subfield and $notregex) { } 2 0 2
Condition88%8/9
$field and not $subfield and not $regex and not $notregex
!ll&&!rl&&r
19215
$field and not $subfield and not $regex
!ll&&!rl&&r
17217
$field and not $subfield
!ll&&!rl&&r
01719
MC/DC75%3/4
$field and not $subfield and not $regex and not $notregex
$field not $subfield not $regex not $notregex
Truth table80%4/5
A and not(B) and not(C) and not(D)
A= $fieldB= not $subfieldC= not $regexD= not $notregex
ABCDresult
0XXX0
10XX0
110X0
11100
11111
438 15 $searchtype="fieldpresence"}
439 ▶ elsif ($field and $subfield and not($regex) and not($notregex)) {
Condition88%8/9
$field and $subfield and not $regex and not $notregex
!ll&&!rl&&r
6213
$field and $subfield and not $regex
!ll&&!rl&&r
4215
$field and $subfield
!ll&&!rl&&r
0417
MC/DC75%3/4
$field and $subfield and not $regex and not $notregex
$field $subfield not $regex not $notregex
Truth table80%4/5
A and B and not(C) and not(D)
A= $fieldB= $subfieldC= not $regexD= not $notregex
ABCDresult
0XXX0
10XX0
110X0
11100
11111
440 13 $searchtype="subfieldpresence"}
441 ▶ elsif ($field and not($subfield) and $regex) {
Condition83%5/6
$field and not $subfield and $regex
!ll&&!rl&&r
422
$field and not $subfield
!ll&&!rl&&r
044
MC/DC66%2/3
$field and not $subfield and $regex
$field not $subfield $regex
Truth table75%3/4
A and not(B) and C
A= $fieldB= not $subfieldC= $regex
ABCresult
0XX0
10X0
1100
1111
442 2 $searchtype="fieldvalue"}
443 ▶ elsif ($field and $subfield and $regex) {
Condition83%5/6
$field and $subfield and $regex
!ll&&!rl&&r
222
$field and $subfield
!ll&&!rl&&r
024
MC/DC66%2/3
$field and $subfield and $regex
$field $subfield $regex
Truth table75%3/4
A and B and C
A= $fieldB= $subfieldC= $regex
ABCresult
0XX0
10X0
1100
1111
444 2 $searchtype="subfieldvalue"}
445 ▶ elsif ($field and not($subfield) and $notregex) {
Condition66%4/6
$field and not $subfield and $notregex
!ll&&!rl&&r
202
$field and not $subfield
!ll&&!rl&&r
022
MC/DC33%1/3
$field and not $subfield and $notregex
$field not $subfield $notregex
Truth table50%2/4
A and not(B) and C
A= $fieldB= not $subfieldC= $notregex
ABCresult
0XX0
10X0
1100
1111
446 2 $searchtype="fieldnotvalue"}
447 ▶ elsif ($field and $subfield and $notregex) {
Condition33%2/6
$field and $subfield and $notregex
!ll&&!rl&&r
002
$field and $subfield
!ll&&!rl&&r
002
MC/DC0%0/3
$field and $subfield and $notregex
$field $subfield $notregex
Truth table25%1/4
A and B and C
A= $fieldB= $subfieldC= $notregex
ABCresult
0XX0
10X0
1100
1111
448 2 $searchtype="subfieldnotvalue"}
449  
450 #do the search by cycling through each record
451 36 ▶ for (my $i=1; $i<=$#$marc; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $marc->$#*) 76 36 112
452  
453 76 my $flag=0;
454 76 ▶ if ($searchtype eq "fieldpresence") {
Branch: 6 branches
BranchTrueFalseTotal
if ($searchtype eq 'fieldpresence') { } 31 45 76
elsif ($searchtype eq 'subfieldpresence') { } 29 16 45
elsif ($searchtype eq 'fieldvalue') { } 4 12 16
elsif ($searchtype eq 'subfieldvalue') { } 4 8 12
elsif ($searchtype eq 'fieldnotvalue') { } 4 4 8
elsif ($searchtype eq 'subfieldnotvalue') { } 4 0 4
455 31 ▶ next unless exists $marc->[$i]{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field} 15 16 31
456 16 push(@results,$i);
457 }
458 elsif ($searchtype eq "subfieldpresence") {
459 29 ▶ next unless exists $marc->[$i]{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field} 9 20 29
460 20 ▶ next unless exists $marc->[$i]{$field}{$subfield};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field}{$subfield} 6 14 20
461 14 push(@results,$i);
462 }
463 elsif ($searchtype eq "fieldvalue") {
464 4 ▶ next unless exists $marc->[$i]{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field} 0 4 4
465 4 ▶ next unless exists $marc->[$i]{$field}{field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field}{"field"} 0 4 4
466 4 my $x=$marc->[$i]{$field}{field};
467 4 foreach my $y (@$x) {
468 4 my $z=_joinfield($y,$field);
469 4 ▶ if (eval qq("$z" =~ $regex)) {$flag=1}
Branch: 1 branch
BranchTrueFalseTotal
if (eval qq["$z" =~ $regex]) 2 2 4
470 }
471 4 ▶ if ($flag) {push (@results,$i)}
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 2 2 4
472 }
473 elsif ($searchtype eq "subfieldvalue") {
474 4 ▶ next unless exists $marc->[$i]{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field} 0 4 4
475 4 ▶ next unless exists $marc->[$i]{$field}{$subfield};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field}{$subfield} 0 4 4
476 4 my $x=$marc->[$i]{$field}{$subfield};
477 4 foreach my $y (@$x) {
478 4 ▶ if (eval qq("$$y" =~ $regex)) {$flag=1}
Branch: 1 branch
BranchTrueFalseTotal
if (eval qq["$$y" =~ $regex]) 2 2 4
479 }
480 4 ▶ if ($flag) {push (@results,$i)}
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 2 2 4
481 }
482 elsif ($searchtype eq "fieldnotvalue" ) {
483 4 ▶ next unless exists $marc->[$i]{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field} 0 4 4
484 4 ▶ next unless exists $marc->[$i]{$field}{field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field}{"field"} 0 4 4
485 4 my $x=$marc->[$i]{$field}{field};
486 0 ▶ if (not($x)) {push(@results,$i); next}
Branch: 1 branch
BranchTrueFalseTotal
unless ($x) 0 4 4
487 4 foreach my $y (@$x) {
488 4 my $z=_joinfield($y,$field);
489 4 ▶ if (eval qq("$z" =~ $notregex)) {$flag=1}
Branch: 1 branch
BranchTrueFalseTotal
if (eval qq["$z" =~ $notregex]) 2 2 4
490 }
491 4 ▶ if (not($flag)) {push (@results,$i)}
Branch: 1 branch
BranchTrueFalseTotal
unless ($flag) 2 2 4
492 }
493 elsif ($searchtype eq "subfieldnotvalue") {
494 4 ▶ next unless exists $marc->[$i]{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field} 0 4 4
495 4 ▶ next unless exists $marc->[$i]{$field}{$subfield};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marc->[$i]{$field}{$subfield} 0 4 4
496 4 my $x=$marc->[$i]{$field}{$subfield};
497 0 ▶ if (not($x)) {push (@results,$i); next}
Branch: 1 branch
BranchTrueFalseTotal
unless ($x) 0 4 4
498 4 foreach my $y (@$x) {
499 4 ▶ if (eval qq("$$y" =~ $notregex)) {$flag=1}
Branch: 1 branch
BranchTrueFalseTotal
if (eval qq["$$y" =~ $notregex]) 2 2 4
500 }
501 4 ▶ if (not($flag)) {push (@results,$i)}
Branch: 1 branch
BranchTrueFalseTotal
unless ($flag) 2 2 4
502 }
503 }
504 36 return @results;
505 }
506  
507 ####################################################################
508  
509 # getfirstvalue() will return the first value of a field or subfield
510 # or indicator or i12 in a particular record found in the MARC
511 # object. It does not depend on the index being up to date.
512  
513 ####################################################################
514 0 ▶sub getfirstvalue { # rec
Subroutine getfirstvalue: not called CC=4 SCAR=30.0
Pod documented
515 0 my $marc= shift;
516 0 my $template=shift;
517 0 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 0 0
518 0 my $record = $template->{record};
519 0 ▶ if (not($record)) {mycarp "You must specify a record"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 0 0 0
520 0 ▶ if ($record > $#{$marc}) {mycarp "Invalid record specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
if ($record > $#{$marc;}) 0 0 0
521 0 my $marcrec = $marc->[$record];
522 0 return $marcrec->getfirstvalue($template);
523  
524 }
525  
526 ####################################################################
527 # getvalue() will return the value of a field or subfield in a #
528 # particular record found in the MARC object #
529 ####################################################################
530 63 ▶sub getvalue { # rec
Subroutine getvalue: called CC=4 SCAR=15.7
Pod documented
531 63 my $marc = shift;
532 63 my $template=shift;
533 63 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 63 63
534 63 my $params = _params($template,@_);
535 63 my $record = $params->{record};
536 0 ▶ if (not($record)) {mycarp "You must specify a record"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 0 63 63
537 0 ▶ if ($record > $#{$marc}) {mycarp "Invalid record specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
if ($record > $#{$marc;}) 0 63 63
538
539 63 return $marc->[$record]->getvalue($params);
540 }
541  
542 ####################################################################
543 #Returns LDR at $record. #
544 ####################################################################
545 4 ▶sub ldr { # rec
Subroutine ldr: called CC=1 SCAR=0.0
Pod undocumented
546 4 my ($self,$record)=@_;
547 4 return $self->[$record]->ldr();
548 }
549  
550  
551 ####################################################################
552 #Takes a record number and returns a hash of fields. #
553 #Needed to determine the format (BOOK, VIS, etc) of #
554 #the record. #
555 #Folk also like to know what Ctrl, Desc etc are. #
556 ####################################################################
557 2 ▶sub unpack_ldr { # rec
Subroutine unpack_ldr: called CC=1 SCAR=0.0
Pod documented
558 2 my ($self,$record) = @_;
559 2 return $self->[$record]->unpack_ldr();
560 }
561  
562
563 0 ▶sub _unpack_ldr { # rec
Subroutine _unpack_ldr: not called CC=1 SCAR=6.9
564 0 my ($self,$ldr)=@_;
565 0 return $self->[0]{proto_rec}->unpack_ldr($ldr);
566 }
567  
568  
569 ####################################################################
570 #Takes a record number. #
571 #Returns the unpacked ldr as a ref to hash from the ref in $self. #
572 #Does not overwrite hash from ldr. #
573 ####################################################################
574 0 ▶sub get_hash_ldr { # rec
Subroutine get_hash_ldr: not called CC=1 SCAR=6.9
Pod documented
575 0 my ($self,$record)=@_;
576 0 return $self->[$record]->get_hash_ldr();
577 }
578  
579 ####################################################################
580 # Takes a record number and updates the corresponding ldr if there
581 # is a hashed form. Returns undef unless there is a hash. Else
582 # returns $ldr.
583 ####################################################################
584 1 ▶sub pack_ldr { # rec
Subroutine pack_ldr: called CC=1 SCAR=0.0
Pod documented
585 1 my ($self,$record)=@_;
586 1 return $self->[$record]->pack_ldr();
587 }
588  
589 ####################################################################
590 #Takes a ref to hash version of the LDR and returns a string #
591 # version #
592 ####################################################################
593 4 ▶sub _pack_ldr { # rec
Subroutine _pack_ldr: called CC=1 SCAR=0.0
594 4 my ($self,$rhldr) = @_;
595 4 return $self->[0]{proto_rec}->_pack_ldr($rhldr);
596 }
597  
598 ####################################################################
599 #Takes a string record number. #
600 #Returns a the format necessary to pack/unpack 008 fields correctly#
601 ####################################################################
602 0 ▶sub bib_format { # rec
Subroutine bib_format: not called CC=1 SCAR=6.9
Pod documented
603 0 my ($self,$record)=@_;
604 0 return $self->[$record]->bib_format();
605 }
606  
607 0 ▶sub _bib_format { # rec
Subroutine _bib_format: not called CC=1 SCAR=6.9
608 0 my ($self,$ldr)=@_;
609 0 return $self->[0]{proto_rec}->_bib_format($ldr);
610 }
611  
612 ####################################################################
613 #Takes a record number. #
614 #Returns the unpacked 008 as a ref to hash. Installs ref in $self. #
615 ####################################################################
616 2 ▶sub unpack_008 { # rec
Subroutine unpack_008: called CC=1 SCAR=0.0
Pod documented
617 2 my ($self,$record) = @_;
618 2 return $self->[$record]->unpack_008();
619 }
620  
621 0 ▶sub _unpack_008 { # rec
Subroutine _unpack_008: not called CC=1 SCAR=6.9
622 0 my ($self,$ff_string,$bib_format) = @_;
623 0 return $self->[0]{proto_rec}->_unpack_008($ff_string,$bib_format);
624 }
625  
626 ####################################################################
627 #Takes a record number. #
628 #Returns the unpacked 008 as a ref to hash from the ref in $self. #
629 #Does not overwrite hash from 008 field. #
630 ####################################################################
631 0 ▶sub get_hash_008 { # rec
Subroutine get_hash_008: not called CC=1 SCAR=6.9
Pod documented
632 0 my ($self,$record)=@_;
633 0 return $self->[$record]->get_hash_008();
634 }
635  
636 ####################################################################
637 #Takes a record number. Flushes hashes to 008 and ldr. #
638 #Updates the 008 field from an installed fixed field hash.
639 #Returns undef unless there is a hash, else returns the 008 field #
640 ####################################################################
641 1 ▶sub pack_008 { # rec
Subroutine pack_008: called CC=1 SCAR=0.0
Pod documented
642 1 my ($self,$record) = @_;
643 1 return $self->[$record]->pack_008();
644 }
645  
646 ####################################################################
647 #Takes LDR and ref to hash of unpacked 008 #
648 #Returns string version of 008 *without* newlines. #
649 ####################################################################
650 2 ▶sub _pack_008 { # rec
Subroutine _pack_008: called CC=1 SCAR=0.0
651 2 my ($self,$ldr,$rhff) = @_;
652 2 return $self->[0]{proto_rec}->_pack_008($ldr,$rhff);
653 }
654  
655 ####################################################################
656 # _joinfield() is an internal subroutine for creating a string out #
657 # of an array of subfields. It takes an optional delimiter #
658 # parameter which will print out subfields if defined #
659 ####################################################################
660 8 ▶sub _joinfield { # rec
Subroutine _joinfield: called CC=1 SCAR=0.0
661 8 return MARC::Rec->_joinfield(@_);
662 }
663  
664 ####################################################################
665  
666 # _make_005 is a function: it returns the time formatted for the 005
667 # field.
668  
669 ####################################################################
670 31 ▶sub _make_005 {
Subroutine _make_005: called CC=2 SCAR=6.9
671 31 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
672 # 1. Official specs for 005 are at
673 # lcweb.loc.gov/marc/bibliographic/ecbdcntr.html
674 # They refer to X3.30 ansi; a copy of that would be of interest.
675 # 2. Checked out some examples for existing practice.
676 31 $year += 1900;
677 31 $mon++; #$mon is counted from 1 when talking to humans.
678 31 ▶ return "19960221075055.7" if $MARC::TEST;
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::TEST 4 27 31
679 27 return sprintf("%0.4d%0.2d%0.2d%0.2d%0.2d%0.2d.0",$year,$mon,$mday,$hour,$min,$sec);
680 }
681  
682 ####################################################################
683  
684 # add_005s takes a template and adds current 005s to the elements of
685 # $marc mentioned in $template->{records}
686  
687 ####################################################################
688 31 ▶sub add_005s {
Subroutine add_005s: called CC=4 SCAR=15.2
Pod documented
689 31 my $marc=shift;
690 31 my $args = shift;
691 31 my @records;
692 31 @records= (1..$#$marc);
693 31 ▶ if ($args && $args->{'records'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args and $args->{"records"}) 0 31 31
Condition33%1/3
$args and $args->{"records"}
!ll&&!rl&&r
0310
MC/DC0%0/2
$args and $args->{"records"}
$args $args->{"records"}
Truth table33%1/3
A and B
A= $argsB= $args->{"records"}
ABresult
0X0
100
111
694 0 @records =@{$args->{'records'}};
695 }
696  
697 31 my $time = MARC::_make_005() ;
698 31 foreach my $i (@records) {
699 86 $marc->[$i]->add_005($time);
700 }
701 }
702
703 ####################################################################
704 # output() will call the appropriate output method using the marc #
705 # object and desired format parameters. #
706 ####################################################################
707 39 ▶sub output {
Subroutine output: called CC=23 SCAR=31.7
Pod documented
708 39 my $marc=shift;
709 39 my $args=shift;
710 39 my $output = "";
711 39 ▶ my $newline = $args->{'lineterm'} || "\n";
Condition50%1/2
$args->{'lineterm'} || "\n"
l!l
039
MC/DC0%0/1
$args->{'lineterm'} || "\n"
$args->{'lineterm'}
Truth table50%1/2
A
A= $args->{'lineterm'}
Aresult
11
00
712  
713 39 ▶ $marc->add_005s($args) if ($args->{'file'} or $args->{'add_005s'});
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"file"} or $args->{"add_005s"} 31 8 39
Condition66%2/3
$args->{"file"} or $args->{"add_005s"}
l!l&&r!l&&!r
3108
MC/DC50%1/2
$args->{"file"} or $args->{"add_005s"}
$args->{"file"} $args->{"add_005s"}
Truth table66%2/3
A or B
A= $args->{"file"}B= $args->{"add_005s"}
ABresult
1X1
011
000
714  
715 39 ▶ unless (exists $args->{'format'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"format"}) 1 38 39
716 # everything to string
717 1 $args->{'format'} = "marc";
718 1 $args->{'lineterm'} = $newline;
719 }
720 39 ▶ if ($args->{'format'} =~ /marc$/oi) {
Branch: 11 branches
BranchTrueFalseTotal
if ($args->{'format'} =~ /marc$/io) { } 2 37 39
elsif ($args->{'format'} =~ /marcmaker$/io) { } 6 31 37
elsif ($args->{'format'} =~ /ascii$/io) { } 6 25 31
elsif ($args->{'format'} =~ /html$/io) { } 8 17 25
elsif ($args->{'format'} =~ /html_header$/io) { } 2 15 17
elsif ($args->{'format'} =~ /html_start$/io) { } 4 11 15
elsif ($args->{'format'} =~ /html_body$/io) { } 2 9 11
elsif ($args->{'format'} =~ /html_footer$/io) { } 3 6 9
elsif ($args->{'format'} =~ /urls$/io) { } 2 4 6
elsif ($args->{'format'} =~ /isbd$/io) { } 2 2 4
elsif ($args->{'format'} =~ /xml/io) { } 2 0 2
721 2 $output = _writemarc($marc,$args);
722 }
723 elsif ($args->{'format'} =~ /marcmaker$/oi) {
724 6 $output = _marcmaker($marc,$args);
725 }
726 elsif ($args->{'format'} =~ /ascii$/oi) {
727 6 $output = _marc2ascii($marc,$args);
728 }
729 elsif ($args->{'format'} =~ /html$/oi) {
730 8 $output .= "<html><body>";
731 8 $output .= _marc2html($marc,$args);
732 8 $output .="$newline</body></html>$newline";
733 }
734 elsif ($args->{'format'} =~ /html_header$/oi) {
735 2 $output = "Content-type: text/html\015\012\015\012";
736 }
737 elsif ($args->{'format'} =~ /html_start$/oi) {
738 4 ▶ if ($args->{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'title'}) { } 1 3 4
739 1 $output = "<html><head><title>$args->{'title'}</title></head>";
740 1 $output .= "$newline<body>";
741 }
742 else {
743 3 $output = "<html><body>";
744 }
745 }
746 elsif ($args->{'format'} =~ /html_body$/oi) {
747 2 $output =_marc2html($marc,$args);
748 }
749 elsif ($args->{'format'} =~ /html_footer$/oi) {
750 3 $output = "$newline</body></html>$newline";
751 }
752 elsif ($args->{'format'} =~ /urls$/oi) {
753 2 ▶ my $title = $args->{'title'} || "Untitled URLs";
Condition50%1/2
$args->{'title'} || 'Untitled URLs'
l!l
02
MC/DC0%0/1
$args->{'title'} || 'Untitled URLs'
$args->{'title'}
Truth table50%1/2
A
A= $args->{'title'}
Aresult
11
00
754 2 $output .= "<html><head><title>$title</title></head>$newline<body>$newline";
755 2 $output .= _urls($marc,$args);
756 2 $output .="</body></html>";
757 }
758 elsif ($args->{'format'} =~ /isbd$/oi) {
759 2 $output = _isbd($marc,$args);
760 }
761 elsif ($args->{'format'} =~ /xml/oi) {
762 2 ▶ mycarp "XML formats are now handled by MARC::XML" if ($^W);
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 2 2
763 2 return;
764 }
765 37 ▶ if ($args->{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'file'}) { } 29 8 37
766 29 ▶ if ($args->{'file'} !~ /^>/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"file"} =~ /^>/) 0 29 29
767 0 mycarp "Don't forget to use > or >> with output file name";
768 0 return;
769 }
770 29 ▶ open (OUT, "$args->{file}") || mycarp "Couldn't open file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, qq[$args->{"file"}] or mycarp("Couldn't open file: $!") 29 0 29
771 #above quote is bad if {file} is tainted. Is probably unecessary.dgl.
772 29 binmode OUT;
773 29 print OUT $output;
774 29 ▶ close OUT || mycarp "Couldn't close file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close OUT or mycarp("Couldn't close file: $!") 29 0 29
775 29 return 1;
776 }
777 #if no filename was specified return the output so it can be grabbed
778 else {
779 8 return $output;
780 }
781 }
782  
783 ####################################################################
784 # _records unpacks it hashref arg or defaults to the entire list
785 ####################################################################
786 36 ▶sub _records {
Subroutine _records: called CC=5 SCAR=16.1
787 36 my ($marc,$args)=@_;
788 36 my $trecs =[];
789 36 my @records = ();
790 36 ▶ $trecs= [$args->{record}] if exists($args->{record});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"record"} 5 31 36
791 36 ▶ $trecs= $args->{records} if $args->{records};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"records"} 0 36 36
792  
793 36 ▶ @records = @$trecs if @$trecs;
Branch: 1 branch
BranchTrueFalseTotal
if @$trecs 5 31 36
794 36 ▶ @records = (1..$#$marc) unless @$trecs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$trecs 31 5 36
795  
796 36 return @records;
797 }
798  
799 ####################################################################
800  
801 # params takes a hashref and does a one level deep copy of it.
802 # It uses the rest of the args to override elements of the hashref.
803 # Returns a hashref so that caller does'nt have to worry about
804 # crypto-context.
805  
806 ####################################################################
807  
808 107 ▶sub _params {
Subroutine _params: called CC=1 SCAR=0.0
809 107 return MARC::Rec::_params(@_);
810 }
811  
812 ####################################################################
813 # _writemarc() takes a MARC object as its input and returns the #
814 # the USMARC equivalent of the object as a string #
815 ####################################################################
816 2 ▶sub _writemarc { #rec
Subroutine _writemarc: called CC=2 SCAR=6.9
817 2 my $marc=shift;
818 2 my $args=shift;
819 #Read in each individual MARC record in the file
820 2 my @records = _records($marc,$args);
821  
822 2 my $marcrecord="";
823 2 foreach my $i (@records) {
824 16 my $record = $marc->[$i];
825 16 $marcrecord .= $record->_writemarc($args);
826 }
827 2 return $marcrecord;
828 }
829  
830  
831 ####################################################################
832 # _marc2ascii() takes a MARC object as its input and returns the #
833 # ASCII equivalent of the object (field names, indicators, field #
834 # values and line-breaks) #
835 ####################################################################
836 6 ▶sub _marc2ascii { # rec
Subroutine _marc2ascii: called CC=3 SCAR=11.0
837 6 my $marc=shift;
838 6 my $args=shift;
839 6 my @records = _records($marc,$args);
840 6 ▶ $args->{'lineterm'} ||= "\n";
Condition50%1/2
$args->{"lineterm"} ||= "\n"
l!l
06
MC/DC0%0/1
$args->{"lineterm"} ||= "\n"
$args->{"lineterm"}
Truth table50%1/2
A
A= $args->{"lineterm"}
Aresult
11
00
841 6 my $output = "";
842 6 for my $i (@records) { #cycle through each record
843 18 my $record=$marc->[$i];
844 18 $output .= $record->_marc2ascii($args);
845 }
846 6 return $output;
847 }
848  
849 ####################################################################
850 # _marcmaker() takes a MARC object as its input and converts it #
851 # into MARCMaker format, which is returned as a string #
852 ####################################################################
853 6 ▶sub _marcmaker { # rec
Subroutine _marcmaker: called CC=2 SCAR=6.9
854 6 my @output = ();
855 6 my $marc=shift;
856 6 my $args=shift;
857 6 $args->{'proto_rec'} = $marc->[0]{'proto_rec'};
858 6 my @records = _records($marc,$args);
859  
860 6 local $^W = 0; # no warnings
861 6 my $breaker = "";
862 6 for my $i (@records) { #cycle through each record
863 24 my $record=$marc->[$i];
864 24 $breaker .= $record->_marcmaker($args);
865 }
866 6 return $breaker;
867 }
868  
869 0 ▶sub _char2maker { # rec
Subroutine _char2maker: not called CC=1 SCAR=6.9
870 0 return MARC::Rec::_char2maker(@_);
871 }
872  
873 0 ▶sub ustext_default { # rec
Subroutine ustext_default: not called CC=1 SCAR=6.9
Pod documented
874 0 return MARC::Rec::ustext_default(@_);
875 }
876  
877 ####################################################################
878 # _marc2html takes a MARC object as its input and converts it into #
879 # HTML. It is possible to specify which field you want to output #
880 # as well as field labels to be used instead of the MARC codes. #
881 # The HTML is returned as a string #
882 ####################################################################
883 10 ▶sub _marc2html {
Subroutine _marc2html: called CC=3 SCAR=11.0
884 10 my $marc = shift;
885 10 my $args = shift;
886 10 ▶ my $newline = $args->{'lineterm'} || "\n";
Condition50%1/2
$args->{'lineterm'} || "\n"
l!l
010
MC/DC0%0/1
$args->{'lineterm'} || "\n"
$args->{'lineterm'}
Truth table50%1/2
A
A= $args->{'lineterm'}
Aresult
11
00
887  
888 10 my @records = _records($marc,$args);
889 10 my $output = "";
890 10 foreach my $i (@records) {
891 20 my $marcrec=$marc->[$i];
892 20 $output.= $marcrec->_marc2html($args);
893 }
894 10 return $output;
895 }
896  
897  
898 ####################################################################
899 # _urls() takes a MARC object as its input, and then extracts the #
900 # control# (MARC 001) and URLs (MARC 856) and outputs them as #
901 # hypertext links in an HTML page. This could then be used with a #
902 # link checker to determine what URLs are broken. #
903 ####################################################################
904 2 ▶sub _urls { # rec
Subroutine _urls: called CC=2 SCAR=6.9
905 2 my $marc = shift;
906 2 my $args = shift;
907  
908 2 my $output = "";
909 2 my @records = _records($marc,$args);
910  
911 2 local $^W = 0; # no warnings
912 2 foreach my $i (@records) {
913 4 my $marcrec=$marc->[$i];
914 4 $output .= $marcrec->_urls($args);
915 }
916 2 return $output;
917 }
918  
919 ####################################################################
920 # isbd() attempts to create a quasi ISBD output format #
921 ####################################################################
922 2 ▶sub _isbd { # rec
Subroutine _isbd: called CC=3 SCAR=11.0
923 2 my $marc=shift;
924 2 my $args=shift;
925 2 ▶ my $newline = $args->{'lineterm'} || "\n";
Condition50%1/2
$args->{'lineterm'} || "\n"
l!l
02
MC/DC0%0/1
$args->{'lineterm'} || "\n"
$args->{'lineterm'}
Truth table50%1/2
A
A= $args->{'lineterm'}
Aresult
11
00
926 2 my @records = _records($marc,$args);
927 2 my $output ="";
928 2 for my $i (@records) { #cycle through each record
929 4 my $record=$marc->[$i];
930 4 $output .= $record->_isbd($args);
931 }
932 2 return $output;
933 }
934  
935 ####################################################################
936 # createrecord() appends a new record to the MARC object #
937 # and initializes the '000' field #
938 ####################################################################
939 0 ▶sub createrecord { # rec
Subroutine createrecord: not called CC=2 SCAR=17.9
Pod documented
940 0 my $marc=shift;
941 0 local $^W = 0; # no warnings
942 0 my $params=shift;
943 0 ▶ my $leader=$params->{'leader'} || "00000nam 2200000 a 4500";
Condition0%0/2
$params->{'leader'} || '00000nam 2200000 a 4500'
l!l
00
MC/DC0%0/1
$params->{'leader'} || '00000nam 2200000 a 4500'
$params->{'leader'}
Truth table0%0/2
A
A= $params->{'leader'}
Aresult
11
00
944 #default leader see MARC documentation http://lcweb.loc.gov/marc
945 0 my $number=$#$marc + 1;
946 0 my $marcrec = $marc->[0]{'proto_rec'}->createrecord($leader);
947 0 push @$marc, $marcrec;
948 0 return $number;
949 }
950  
951 ####################################################################
952 # addfield() appends/inserts a new field into an existing record #
953 ####################################################################
954  
955 19 ▶sub addfield {
Subroutine addfield: called CC=25 SCAR=37.2
Pod documented
956 19 my $marc=shift;
957 19 my $params=shift;
958 19 local $^W = 0; # no warnings
959 19 my $record=$params->{'record'};
960 0 ▶ unless ($record) {mycarp "You must specify a record"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 0 19 19
961 0 ▶ if ($record > $#{$marc}) {mycarp "Invalid record specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
if ($record > $#{$marc;}) 0 19 19
962 19 my $field = $params->{'field'};
963 0 ▶ unless ($field) {mycarp "You must specify a field"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 19 19
964 0 ▶ unless ($field =~ /^\d{3}$/) {mycarp "Invalid field specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field =~ /^\d{3}$/) 0 19 19
965  
966 19 my $i1=$params->{'i1'};
967 19 ▶ $i1 = ' ' unless (defined $i1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i1 10 9 19
968 19 my $i2=$params->{'i2'};
969 19 ▶ $i2 = ' ' unless (defined $i2);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i2 10 9 19
970 19 ▶ my @value=$params->{'value'} || @_;
Condition66%2/3
$params->{'value'} || @_
l!l&&r!l&&!r
9100
MC/DC0%0/2
$params->{'value'} || @_
$params->{'value'} @_
Truth table66%2/3
A || B
A= $params->{'value'}B= @_
ABresult
1X1
011
000
971 19 ▶ if (ref($params->{'value'}) eq "ARRAY") { @value = @{$params->{'value'}}; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{"value"} eq "ARRAY") 9 10 19
972 0 ▶ unless (defined $value[0]) {mycarp "No value specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value[0]) 0 19 19
973  
974 19 ▶ if ($field >= 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field >= 10) 19 0 19
975 19 ▶ if ($value[0] eq 'i1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value[0] eq "i1") 10 9 19
976 10 shift @value;
977 10 $i1 = shift @value;
978 }
979 19 ▶ unless (1 == length($i1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (1 == length $i1) 0 19 19
980 0 mycarp "invalid \'i1\' specified";
981 0 return;
982 }
983 19 ▶ if ($value[0] eq 'i2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value[0] eq "i2") 10 9 19
984 10 shift @value;
985 10 $i2 = shift @value;
986 }
987 19 ▶ unless (1 == length($i2)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (1 == length $i2) 0 19 19
988 0 mycarp "invalid \'i2\' specified";
989 0 return;
990 }
991 }
992  
993 19 ▶ my $ordered=$params->{'ordered'} || "y";
Condition100%2/2
$params->{'ordered'} || 'y'
l!l
181
MC/DC100%1/1
$params->{'ordered'} || 'y'
$params->{'ordered'}
Truth table100%2/2
A
A= $params->{'ordered'}
Aresult
11
00
994 19 my $insertorder = $#{$marc->[$record]{array}} + 1;
995 #if necessary figure out the insert order to preserve tag order
996 19 ▶ if ($ordered=~/y/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ordered =~ /y/i) 16 3 19
997 16 ▶ for (my $i=0; $i<=$#{$marc->[$record]{array}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$marc[$record]{"array"};}) 249 0 249
998 249 ▶ if ($marc->[$record]{array}[$i][0] > $field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($marc->[$record]{"array"}[$i][0] > $field) 16 233 249
999 16 $insertorder=$i;
1000 16 last;
1001 }
1002 0 ▶ if ($insertorder==0) {$insertorder=1}
Branch: 1 branch
BranchTrueFalseTotal
if ($insertorder == 0) 0 233 233
1003 }
1004 }
1005 19 my @field;
1006 19 ▶ if ($field<10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field < 10) { } 0 19 19
1007 0 push (@field, $field, $value[0]);
1008 0 ▶ if ($ordered=~/y/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ordered =~ /y/i) { } 0 0 0
1009 0 splice @{$marc->[$record]{array}},$insertorder,0,\@field;
1010 }
1011 else {
1012 0 push (@{$marc->[$record]{array}},\@field);
1013 }
1014 }
1015 else {
1016 19 push (@field, $field, $i1, $i2);
1017 19 my ($sub_id, $subfield);
1018 19 ▶ while ($sub_id = shift @value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_id = shift @value) 32 19 51
1019 32 ▶ last if ($sub_id eq "\036");
Branch: 1 branch
BranchTrueFalseTotal
if $sub_id eq "\36" 0 32 32
1020 32 $subfield = shift @value;
1021 32 push (@field, $sub_id, $subfield);
1022 }
1023 19 ▶ if ($ordered=~/y/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ordered =~ /y/i) { } 16 3 19
1024 16 splice @{$marc->[$record]{array}},$insertorder,0,\@field;
1025 }
1026 else {
1027 3 push (@{$marc->[$record]{array}},\@field);
1028 }
1029 }
1030 19 $marc->add_map($record,\@field);
1031 }
1032  
1033 ####################################################################
1034  
1035 # getfields() takes a template and returns an array of fieldrefs from
1036 # $marc->[$recnum]{'array'} including all with the appropriate tag
1037 # and having the property that they are a contiguous group. (So may
1038 # include fields with other tags.)
1039  
1040 ####################################################################
1041 0 ▶sub getfields { # rec
Subroutine getfields: not called CC=3 SCAR=24.8
Pod documented
1042 0 my $marc=shift;
1043 0 my $params=shift;
1044 0 my $record=$params->{'record'};
1045 0 ▶ unless ($record) {mycarp "You must specify a record"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 0 0 0
1046 0 ▶ if ($record > $#{$marc}) {mycarp "Invalid record specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
if ($record > $#{$marc;}) 0 0 0
1047 0 return $marc->[$record]->getfields($params);
1048  
1049 }
1050  
1051 ####################################################################
1052 # getupdate() returns an array of key,value pairs formatted to #
1053 # pass to addfield(). For repeated tags, a "\036" element is used #
1054 # to delimit data for separate addfield() commands #
1055 ####################################################################
1056 32 ▶sub getupdate {
Subroutine getupdate: called CC=12 SCAR=27.9
Pod documented
1057 32 my @output;
1058 32 my $marc=shift;
1059 32 my $params=shift;
1060 32 my $record=$params->{'record'};
1061 0 ▶ unless ($record) {mycarp "You must specify a record"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 0 32 32
1062 0 ▶ if ($record > $#{$marc}) {mycarp "Invalid record specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
if ($record > $#{$marc;}) 0 32 32
1063 32 my $field = $params->{'field'};
1064 0 ▶ unless ($field) {mycarp "You must specify a field"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 32 32
1065 0 ▶ unless ($field =~ /^\d{3}$/) {mycarp "Invalid field specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field =~ /^\d{3}$/) 0 32 32
1066  
1067 32 foreach my $fields (@{$marc->[$record]{array}}) { #cycle each field
1068 738 ▶ next unless ($field eq $fields->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless $field eq $fields->[0] 698 40 738
1069 40 ▶ if ($field<10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field < 10) { } 5 35 40
1070 5 push @output,$fields->[1];
1071 }
1072 else {
1073 35 push @output,'i1',$fields->[1],'i2',$fields->[2];
1074 35 my @subfields = @{$fields}[3..$#{$fields}];
1075 35 ▶ while (@subfields) { #cycle through subfields incl. refs
Branch: 1 branch
BranchTrueFalseTotal
if (@subfields) 100 35 135
1076 100 my $subfield = shift @subfields;
1077 100 ▶ last unless defined $subfield;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subfield 0 100 100
1078 100 ▶ if (ref($subfield) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subfield eq 'ARRAY') { } 0 100 100
1079 0 foreach my $subsub (@{$subfield}) {
1080 0 push @output, $subsub;
1081 }
1082 }
1083 else {
1084 100 push @output, $subfield;
1085 }
1086 } #finish cycling through subfields
1087 } #finish tag test < 10
1088 40 push @output,"\036";
1089 }
1090 32 return @output;
1091 }
1092 ####################################################################
1093  
1094 # deletefirst() takes a template and a boolean $do_rebuild_map to
1095 # rebuild the map. It deletes the field data for a first match, using
1096 # the template and leaves the rest alone. If the template has a
1097 # subfield element it deletes based on the subfield information in the
1098 # template. If the last subfield of a field is deleted, deletefirst()
1099 # also deletes the field. It complains about attempts to delete
1100 # indicators. If there is no match, it does nothing. Deletefirst also
1101 # rebuilds the map if $do_rebuild_map. Deletefirst returns the number
1102 # of matches deleted (that would be 0 or 1), or undef if it feels
1103 # grumpy (i.e. carps).
1104  
1105 ####################################################################
1106  
1107 2 ▶sub deletefirst { # rec
Subroutine deletefirst: called CC=3 SCAR=12.3
Pod documented
1108 2 ▶ my $marc = shift || return;
Condition50%1/2
shift() || (return)
l!l
20
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1109 2 my $template = shift;
1110 2 my $recnum = $template->{'record'};
1111 0 ▶ if (!$recnum) {mycarp "Need a record to confine my destructive tendencies"; return undef}
Branch: 1 branch
BranchTrueFalseTotal
unless ($recnum) 0 2 2
1112 2 return $marc->[$recnum]->deletefirst($template);
1113 }
1114  
1115 ####################################################################
1116  
1117 # field_is_empty takes a ref to an array formatted like
1118 # an element of $marc->[$recnum]{array}. It returns 1 if there are
1119 # no "significant" elements of the array (e.g. nothing but indicators
1120 # if $tag>10), else 0. Override this if you want to delete fields
1121 # that have "insignificant" subfields inside deletefirst.
1122  
1123 ####################################################################
1124 0 ▶sub field_is_empty { # rec
Subroutine field_is_empty: not called CC=1 SCAR=6.9
Pod undocumented
1125 0 my ($marc,$rfield) = @_;
1126 0 return $marc->[0]{proto_rec}->field_is_empty($rfield);
1127 }
1128  
1129 ####################################################################
1130  
1131 # field_updatehook takes a ref to an array formatted like
1132 # $marc->[$recnum]{'array'}. It is there so that
1133 # subclasses can override it to do something before calling
1134 # addfield(), e.g. store field-specific information in the affected
1135 # field or log information in an external file/database. One notes that
1136 # since this is a method, it can ignore its arguments and log global
1137 # information about $marc, e.g. order information in $marc->[$rnum]{'array'}
1138  
1139 ####################################################################
1140  
1141 0 ▶sub field_updatehook { # rec
Subroutine field_updatehook: not called CC=1 SCAR=6.9
Pod undocumented
1142 0 my ($marc,$rfield)=@_;
1143 0 $marc->[0]{'proto_rec'}->field_updatehook($rfield);
1144 }
1145  
1146 ####################################################################
1147  
1148 # updatefirst() takes a template, a request to rebuild the index, and
1149 # an array from $marc->[recnum]{array}. It replaces/creates the field
1150 # data for a first match, using the template, and leaves the rest
1151 # alone. If the template has a subfield element, (this includes
1152 # indicators) it ignores all other information in the array and only
1153 # updates/creates based on the subfield information in the array. If
1154 # the template has no subfield information then indicators are left
1155 # untouched unless a new field needs to be created, in which case they
1156 # are left blank.
1157  
1158 ####################################################################
1159  
1160 8 ▶sub updatefirst { # rec
Subroutine updatefirst: called CC=6 SCAR=20.2
Pod documented
1161 8 ▶ my $marc = shift || return;
Condition50%1/2
shift() || (return)
l!l
80
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1162 8 my $template = shift;
1163 8 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 8 8
1164 8 ▶ return unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 8 8
1165 8 ▶ return if (defined $template->{'value'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $template->{"value"} 0 8 8
1166  
1167 8 my $recnum = $template->{'record'};
1168 0 ▶ if (!$recnum) {mycarp "Need a record to confine my changing needs."; return undef}
Branch: 1 branch
BranchTrueFalseTotal
unless ($recnum) 0 8 8
1169 8 return $marc->[$recnum]->updatefirst($template,@_);
1170 }
1171  
1172 ####################################################################
1173  
1174 # updatefields() takes a template which specifies recnum, a
1175 # $do_rebuild_map and a field (needs the field in case $rafields->[0]
1176 # is empty). It also takes a ref to an array of fieldrefs formatted
1177 # like the output of getfields(), and replaces/creates the field
1178 # data. It assumes that it should remove the fields with the first tag
1179 # in the fieldrefs. It calls rebuild_map() if $do_rebuild_map.
1180  
1181 ####################################################################
1182 0 ▶sub updatefields { # rec
Subroutine updatefields: not called CC=2 SCAR=17.9
Pod documented
1183 0 ▶ my $marc = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1184 0 my $template = shift;
1185  
1186 0 my $rafieldrefs = shift;
1187 0 my $recnum = $template->{'record'};
1188 0 return $marc->[$recnum]->updatefields($template,$rafieldrefs);
1189 }
1190  
1191 ####################################################################
1192  
1193 # getmatch() takes a subfield code (can be an indicator) and a fieldref
1194 # Returns 0 or a ref to the value to be updated.
1195  
1196 ####################################################################
1197 0 ▶sub getmatch { # rec
Subroutine getmatch: not called CC=2 SCAR=17.9
Pod documented
1198 0 ▶ my $marc = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1199 0 return $marc->[0]{proto_rec}->getmatch(@_);
1200 }
1201  
1202 ####################################################################
1203  
1204 # deletesubfield() takes a subfield code (can not be an indicator) and a
1205 # fieldref. Deletes the subfield code and its value in the fieldref at
1206 # the first match on subfield code. Assumes there is an exact
1207 # subfield match in $fieldref.
1208  
1209 ####################################################################
1210 0 ▶sub deletesubfield { # rec
Subroutine deletesubfield: not called CC=2 SCAR=17.9
Pod undocumented
1211 0 ▶ my $marc = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1212 0 return $marc->[0]{proto_rec}->deletesubfield(@_);
1213 }
1214  
1215 ####################################################################
1216  
1217 # insertpos() takes a subfield code (can not be an indicator), a
1218 # value, and a fieldref. Updates the fieldref with the first
1219 # place that the fieldref can match. Assumes there is no exact
1220 # subfield match in $fieldref.
1221  
1222 ####################################################################
1223 0 ▶sub insertpos { # rec
Subroutine insertpos: not called CC=2 SCAR=17.9
Pod documented
1224 0 ▶ my $marc = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1225 0 return $marc->[0]{proto_rec}->insertpos(@_);
1226 }
1227
1228  
1229 ####################################################################
1230 # updaterecord() takes an array of key/value pairs, formatted like #
1231 # the output of getupdate(), and replaces/creates the field data. #
1232 # For repeated tags, a "\036" element is used to delimit data into #
1233 # separate addfield() commands. #
1234 ####################################################################
1235 0 ▶sub updaterecord {
Subroutine updaterecord: not called CC=9 SCAR=45.0
Pod documented
1236 0 ▶ my $marc = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1237 0 my $template = shift;
1238 0 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 0 0
1239 0 ▶ return unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
1240 0 ▶ return if (defined $template->{'value'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $template->{"value"} 0 0 0
1241 0 my $count = 0;
1242 0 my @records = ();
1243 0 ▶ unless ($marc->deletemarc($template)) {mycarp "not deleted\n"; return;}
Branch: 1 branch
BranchTrueFalseTotal
unless ($marc->deletemarc($template)) 0 0 0
1244 0 foreach my $y1 (@_) {
1245 0 ▶ unless ($y1 eq "\036") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y1 eq "\36") 0 0 0
1246 0 push @records, $y1;
1247 0 next;
1248 }
1249 0 ▶ unless ($marc->addfield($template, @records)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($marc->addfield($template, @records)) 0 0 0
1250 0 mycarp "not added\n";
1251 0 return;
1252 }
1253 0 @records = ();
1254 0 $count++;
1255 }
1256 0 return $count;
1257 }
1258  
1259 ####################################################################
1260 # _offset is an internal subroutine used by writemarc to offset #
1261 # number ie. making "34" into "00034". #
1262 ####################################################################
1263 0 ▶sub _offset{
Subroutine _offset: not called CC=1 SCAR=6.9
1264 0 return MARC::Rec::_offset(@_);
1265 }
1266  
1267 ####################################################################
1268  
1269 # MARC::Rec is responsible for the methods and representation of
1270 # a single MARC record. Its protocol is very close to that of MARC:
1271 # in fact, most MARC methods have been moved here without the record
1272 # number and re-implemented in standard form by delegation.
1273  
1274 ####################################################################
1275  
1276 package MARC::Rec;
1277 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1278 5 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS
Subroutine BEGIN: called CC=1 SCAR=0.0
1279 @LDR_FIELDS $LDR_TEMPLATE %FF_FIELDS %FF_TEMPLATE
1280 5 );
1281  
1282 5 $VERSION = $MARC::VERSION;
1283  
1284 5 @ISA = qw(Exporter);
1285 5 @EXPORT= qw();
1286 5 @EXPORT_OK= qw();
1287  
1288 #### Not using these yet
1289  
1290 #### %EXPORT_TAGS = (USTEXT => [qw( marc2ustext )]);
1291 #### Exporter::export_ok_tags('USTEXT');
1292 #### $EXPORT_TAGS{ALL} = \@EXPORT_OK;
1293  
1294 # gotta know where to find leader information....
1295  
1296 5 @LDR_FIELDS = qw(rec_len RecStat Type BLvl Ctrl Undefldr base_addr
1297 ELvl Desc ln_rec len_len_field len_start_char len_impl Undef2ldr);
1298  
1299 5 $LDR_TEMPLATE = "a5aaaaa3a5aaaaaaa";
1300  
1301 #...And the 008 field has a special place in Librarians' hearts.
1302 5 %FF_FIELDS = (
1303 BOOKS =>
1304 [qw(Entered DtSt Date1 Date2 Ctry Ills Audn Form Cont
1305 GPub Conf Fest Indx Undef1 Fict Biog Lang MRec Srce)],
1306 COMPUTER_FILES =>
1307 [qw(Entered DtSt Date1 Date2 Ctry Undef1 Audn Undef2
1308 File Undef3 GPub Undef4 Lang MRec Srce)],
1309 MAPS =>
1310 [qw(Entered DtSt Date1 Date2 Ctry Relf Proj Prme CrTp
1311 Undef1 GPub Undef2 Indx Undef3 SpFm Lang MRec Srce)],
1312 MUSIC =>
1313 [qw(Entered DtSt Date1 Date2 Ctry Comp FMus Undef1 Audn
1314 Form AccM LTxt Undef2 Lang MRec Srce)],
1315 SERIALS =>
1316 [qw(Entered DtSt Date1 Date2 Ctry Freq Regl ISSN SrTp
1317 Orig Form EntW Cont GPub Conf Undef1 Alph S_L Lang MRec Srce)],
1318 VIS =>
1319 [qw(Entered DtSt Date1 Date2 Ctry Time Undef1
1320 Audn AccM GPub Undef2 TMat Tech Lang MRec Srce)],
1321 MIX =>
1322 [qw(Entered DtSt Date1 Date2
1323 Ctry Undef1 Form Undef2 Lang MRec Srce)]
1324 );
1325  
1326 5 %FF_TEMPLATE = (
1327 BOOKS => "a6a1a4a4a3a4a1a1a4a1a1a1a1a1a1a1a3a1a1",
1328 COMPUTER_FILES => "a6a1a4a4a3a4a1a3a1a1a1a6a3a1a1",
1329 MAPS => "a6a1a4a4a3a4a2a1a1a2a1a2a1a1a2a3a1a1",
1330 MUSIC => "a6a1a4a4a3a2a1a1a1a1a6a2a3a3a1a1",
1331 SERIALS => "a6a1a4a4a3a1a1a1a1a1a1a1a3a1a1a3a1a1a3a1a1",
1332 VIS => "a6a1a4a4a3a3a1a1a5a1a4a1a1a3a1a1",
1333 MIX => "a6a1a4a4a3a5a1a11a3a1a1"
1334 );
1335  
1336 # Preloaded methods go here.
1337 ####################################################################
1338 # _offset is an internal subroutine used by writemarc to offset #
1339 # number ie. making "34" into "00034". #
1340 ####################################################################
1341 592 ▶sub _offset{
Subroutine _offset: called CC=2 SCAR=7.0
1342 592 my $value=shift;
1343 592 my $digits=shift;
1344 592 ▶ print "DEBUG: _offset value = $value, digits = $digits\n" if $MARC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::DEBUG 0 592 592
1345 592 my $x=length($value);
1346 592 $x=$digits-$x;
1347 592 $x="0"x$x."$value";
1348 }
1349  
1350 0 ▶sub mycarp { # rec
Subroutine mycarp: not called CC=2 SCAR=17.9
1351 0 ▶ Carp::carp (@_) unless $MARC::TEST;
Branch: 1 branch
BranchTrueFalseTotal
unless $MARC::TEST 0 0 0
1352 }
1353  
1354 ####################################################################
1355  
1356 # This is the constructor method that creates the MARC::Rec object. It
1357 # sets up references and gets out. Any file it knows about will be an
1358 # already opened filehandle: do error checking and binmode on the file
1359 # outside MARC::Rec.
1360  
1361 ####################################################################
1362 5 ▶sub new { # rec
Subroutine new: called CC=5 SCAR=17.0
1363 5 my $proto = shift;
1364 5 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
1365 5 my $filehandle = shift;
1366 5 my $marcrec = {};
1367 5 bless ($marcrec, $class);
1368 5 ▶ my $format = shift || "usmarc";
Condition50%1/2
shift() || 'usmarc'
l!l
05
MC/DC0%0/1
shift() || 'usmarc'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1369  
1370 5 ▶ $marcrec->{'handle'} ||= \*filehandle;
Condition50%1/2
$marcrec->{"handle"} ||= \*filehandle
l!l
05
MC/DC0%0/1
$marcrec->{"handle"} ||= \*filehandle
$marcrec->{"handle"}
Truth table50%1/2
A
A= $marcrec->{"handle"}
Aresult
11
00
1371 5 $marcrec->{'format'}=$format;
1372 5 ▶ $marcrec->{'lineterm'}="\015\012" if $format eq 'marcmaker';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "marcmaker" 0 5 5
1373 # MS-DOS default for MARCMaker
1374 5 return $marcrec;
1375 }
1376  
1377 ####################################################################
1378  
1379 # Copy_struct returns a copy of the marcrec ($proto) without
1380 # {array} and map information. The copy shares references to
1381 # {handle} by design.
1382  
1383 ####################################################################
1384 23 ▶sub copy_struct {
Subroutine copy_struct: called CC=3 SCAR=11.0
1385 23 my $proto = shift;
1386 23 my $class = ref($proto);
1387 23 my $newrec;
1388 23 for (keys %$proto) {
1389 72 ▶ $newrec->{$_} = $proto->{$_} if /^(handle|format|proto_rec)$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(handle|format|proto_rec)$/ 46 26 72
1390 }
1391 23 return bless $newrec,$class;
1392 }
1393  
1394 ####################################################################
1395  
1396 # clone returns a new MARC::Rec object with copies of the data.
1397 # Admin information remains linked to original.
1398  
1399 ####################################################################
1400 0 ▶sub clone {
Subroutine clone: not called CC=3 SCAR=24.8
1401 0 my $marcrec=shift;
1402 0 my $ldr = $marcrec->ldr();
1403 0 my $ans = $marcrec->createrecord($ldr);
1404 0 for (@{$marcrec->{array}}) {
1405 0 ▶ next if $_->[0] eq '000';
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq "000" 0 0 0
1406 0 my @field = @$_;
1407 0 my $rfield = \@field;
1408 0 push @{$ans->{array}}, $rfield;
1409 0 $ans->add_map($rfield);
1410 }
1411 0 return $ans;
1412 }
1413  
1414 ####################################################################
1415  
1416 # field_is_empty takes a ref to an array formatted like
1417 # an element of $marc->[$recnum]{array}. It returns 1 if there are
1418 # no "significant" elements of the array (e.g. nothing but indicators
1419 # if $tag>10), else 0. Override this if you want to delete fields
1420 # that have "insignificant" subfields inside deletefirst.
1421  
1422 ####################################################################
1423 1 ▶sub field_is_empty { # rec
Subroutine field_is_empty: called CC=5 SCAR=18.4
1424 1 my ($marcrec,$rfield) = @_;
1425  
1426 1 my $tag = $rfield->[0];
1427 1 my @field = @$rfield;
1428 1 ▶ return 1 if ($tag > 10 and !defined($field[3]));
Branch: 1 branch
BranchTrueFalseTotal
if $tag > 10 and not defined $field[3] 0 1 1
Condition33%1/3
$tag > 10 and not defined $field[3]
!ll&&!rl&&r
010
MC/DC0%0/2
$tag > 10 and not defined $field[3]
$tag > 10 not defined $field[3]
Truth table33%1/3
A and not(B)
A= $tag > 10B= not defined $field[3]
ABresult
0X0
100
111
1429 1 ▶ return 1 if ($tag < 10 and !defined($field[1]) );
Branch: 1 branch
BranchTrueFalseTotal
if $tag < 10 and not defined $field[1] 0 1 1
Condition33%1/3
$tag < 10 and not defined $field[1]
!ll&&!rl&&r
100
MC/DC0%0/2
$tag < 10 and not defined $field[1]
$tag < 10 not defined $field[1]
Truth table33%1/3
A and not(B)
A= $tag < 10B= not defined $field[1]
ABresult
0X0
100
111
1430 1 return 0;
1431 }
1432  
1433 ####################################################################
1434  
1435 # field_updatehook echos the version in MARC without the recordnum.
1436  
1437 ####################################################################
1438 117 ▶sub field_updatehook { # rec
Subroutine field_updatehook: called CC=1 SCAR=0.0
1439 # nothing. Subclass may want to handle this.
1440 }
1441  
1442  
1443 ####################################################################
1444  
1445 # getfields() takes a template and returns an array of fieldrefs from
1446 # $marc->[$recnum]{'array'} including all with the appropriate tag
1447 # and having the property that they are a contiguous group. (So may
1448 # include fields with other tags.)
1449  
1450 ####################################################################
1451 113 ▶sub getfields { # rec
Subroutine getfields: called CC=8 SCAR=21.2
1452  
1453 113 my $marcrec=shift;
1454 113 my $params=shift;
1455  
1456 113 my $field = $params->{'field'};
1457 0 ▶ unless ($field) {mycarp "You must specify a field"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 113 113
1458 0 ▶ unless ($field =~ /^\d{3}$/) {mycarp "Invalid field specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field =~ /^\d{3}$/) 0 113 113
1459  
1460 113 my @ans=();
1461 113 my $first = undef;
1462 113 my $last = $first;
1463 113 my $pos = 0;
1464 113 for (@{$marcrec->{'array'}}) {
1465 2383 ▶ $first = $pos if ($_->[0] eq $field && !defined($first)) ;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq $field and not defined $first 106 2277 2383
Condition100%3/3
$_->[0] eq $field and not defined $first
!ll&&!rl&&r
225423106
MC/DC100%2/2
$_->[0] eq $field and not defined $first
$_->[0] eq $field not defined $first
Truth table100%3/3
A and not(B)
A= $_->[0] eq $fieldB= not defined $first
ABresult
0X0
100
111
1466 2383 ▶ $last = $pos if $_->[0] eq $field;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq $field 129 2254 2383
1467 2383 $pos++;
1468 }
1469 113 ▶ return () unless defined($first);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 7 106 113
1470 106 return @{$marcrec->{'array'}}[$first..$last]; # array slice. Look it up.
1471 }
1472  
1473 ####################################################################
1474  
1475 # deletefirst() takes a template and a boolean $do_rebuild_map to
1476 # rebuild the map. It deletes the field data for a first match, using
1477 # the template and leaves the rest alone. If the template has a
1478 # subfield element it deletes based on the subfield information in the
1479 # template. If the last subfield of a field is deleted, deletefirst()
1480 # also deletes the field. It complains about attempts to delete
1481 # indicators. If there is no match, it does nothing. Deletefirst also
1482 # rebuilds the map if $do_rebuild_map. Deletefirst returns the number
1483 # of matches deleted (that would be 0 or 1), or undef if it feels
1484 # grumpy (i.e. carps).
1485  
1486 ####################################################################
1487  
1488 18 ▶sub deletefirst { # rec
Subroutine deletefirst: called CC=16 SCAR=29.3
1489 18 ▶ my $marcrec = shift || return;
Condition50%1/2
shift() || (return)
l!l
180
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1490 18 my $template = shift;
1491 18 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 18 18
1492 18 ▶ return if (defined $template->{'value'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $template->{"value"} 0 18 18
1493  
1494 18 my $field = $template->{'field'};
1495  
1496 18 my $subfield = $template->{'subfield'};
1497 18 my $do_rebuild_map = $template->{'rebuild_map'};
1498 0 ▶ if (defined($subfield) and $subfield =~/^i[12]$/) {mycarp "Cannot delete indicators"; return undef}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subfield and $subfield =~ /^i[12]$/) 0 18 18
Condition66%2/3
defined $subfield and $subfield =~ /^i[12]$/
!ll&&!rl&&r
1620
MC/DC0%0/2
defined $subfield and $subfield =~ /^i[12]$/
defined $subfield $subfield =~ /^i[12]$/
Truth table66%2/3
A and B
A= defined $subfieldB= $subfield =~ /^i[12]$/
ABresult
0X0
100
111
1499 #I know that $marc->{$field}{field} is this information
1500 #But I don't want to depend on the map being up-to-date allways.
1501  
1502 18 my @fieldrefs = $marcrec->getfields($template); #helps with cjk.
1503  
1504 18 ▶ return 0 unless scalar(@fieldrefs);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @fieldrefs 4 14 18
1505
1506 14 ▶ if ($field and not($subfield)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field and not $subfield) 12 2 14
Condition66%2/3
$field and not $subfield
!ll&&!rl&&r
0212
MC/DC50%1/2
$field and not $subfield
$field not $subfield
Truth table66%2/3
A and not(B)
A= $fieldB= not $subfield
ABresult
0X0
100
111
1507 12 shift @fieldrefs;
1508 12 $marcrec->updatefields($template,\@fieldrefs);
1509 12 ▶ $marcrec->rebuild_map($field) if $do_rebuild_map;
Branch: 1 branch
BranchTrueFalseTotal
if $do_rebuild_map 0 12 12
1510 12 return 1;
1511 }
1512  
1513  
1514 #Linear search for the field where deletion happens and the position
1515 #in that field.
1516 2 my $rvictim=0;
1517 2 my $fieldnum = 0;
1518 2 foreach my $fieldref (@fieldrefs) {
1519 2 ▶ if ($marcrec->getmatch($subfield,$fieldref)){
Branch: 1 branch
BranchTrueFalseTotal
if ($marcrec->getmatch($subfield, $fieldref)) 1 1 2
1520 1 $rvictim=$fieldref;
1521 1 last;
1522 }
1523 1 $fieldnum++;
1524 }
1525 2 ▶ if (!$rvictim) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rvictim) 1 1 2
1526 1 ▶ $marcrec->rebuild_map($field) if $do_rebuild_map;
Branch: 1 branch
BranchTrueFalseTotal
if $do_rebuild_map 0 1 1
1527 1 return 0;
1528 }
1529  
1530 #Now we know that we have a field and subfield with a match.
1531 #Find the first one and kill it. Kill the enclosing field
1532 #if it is the last one.
1533 1 $marcrec->deletesubfield($subfield,$rvictim);
1534 1 $marcrec->field_updatehook($rvictim);
1535 1 ▶ if ($marcrec->field_is_empty($rvictim)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($marcrec->field_is_empty($rvictim)) 0 1 1
1536 0 splice @fieldrefs,$fieldnum,1;
1537 0 $marcrec->updatefields($template,\@fieldrefs);
1538 }
1539 #here we don't need to directly touch $marc->{array}
1540 # since we are not changing its structure.
1541 1 ▶ $marcrec->rebuild_map($field) if $do_rebuild_map;
Branch: 1 branch
BranchTrueFalseTotal
if $do_rebuild_map 0 1 1
1542 1 return 1;
1543 }
1544  
1545 170 ▶sub _params {
Subroutine _params: called CC=2 SCAR=6.9
1546 170 my $template =shift;
1547 170 ▶ return {} unless ref $template eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 3 167 170
1548 167 my %params = %$template;
1549 167 %params = (%params,@_);
1550 167 return \%params;
1551 }
1552  
1553 ####################################################################
1554 # _writemarc() takes a MARC object as its input and returns the #
1555 # the USMARC equivalent of the object as a string #
1556 ####################################################################
1557 16 ▶sub _writemarc { # rec
Subroutine _writemarc: called CC=5 SCAR=16.1
1558 16 my $marcrec=shift;
1559 16 my $args=shift;
1560 16 my (@record, $fieldbase, $fielddata, $fieldlength, $fieldposition,
1561 $marcrecord, $recordlength);
1562
1563 16 my $record = $marcrec;
1564 #Reset variables
1565 16 my $position=0; my $directory=""; my $fieldstream="";
1566 16 my $leader=$record->{'000'}[1];
1567 16 foreach my $field (@{$record->{'array'}}) {
1568 296 my $tag = $field->[0];
1569 296 ▶ if ($tag eq '000') {next}; #don't output the directory!
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "000") 16 280 296
1570 280 my $fielddata="";
1571 280 ▶ if ($tag < 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) { } 64 216 280
1572 64 $fielddata=$field->[1];
1573 }
1574 else {
1575 216 $fielddata.=$field->[1].$field->[2]; #add on indicators
1576 216 my @subfields=@{$field}[3..$#{$field}];
1577 216 ▶ while (@subfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subfields) 426 216 642
1578 426 $fielddata.="\037".shift(@subfields); #shift off subfield delimiter
1579 426 $fielddata.=shift(@subfields); #shift off subfield value
1580 }
1581 }
1582 280 $fielddata.="\036";
1583 280 $fieldlength=_offset(length($fielddata),4);
1584 280 $fieldposition=_offset($position,5);
1585 280 $directory.=$tag.$fieldlength.$fieldposition;
1586 280 $position+=$fieldlength;
1587 280 $fieldstream.=$fielddata;
1588 }
1589 16 $directory.="\036";
1590 16 $fieldstream.="\035";
1591 16 $fieldbase=24+length($directory);
1592 16 $fieldbase=_offset($fieldbase,5);
1593 16 $recordlength=24+length($directory)+length($fieldstream);
1594 16 $recordlength=_offset($recordlength,5);
1595 16 $leader=~s/^.{5}(.{7}).{5}(.{7})/$recordlength$1$fieldbase$2/;
1596  
1597 16 $marcrecord ="$leader$directory$fieldstream";
1598  
1599 16 $record->{'000'}[1] = $leader; # save recomputed version
1600 16 return $marcrecord;
1601 }
1602
1603 ####################################################################
1604 # _marc2ascii() takes a MARC object as its input and returns the #
1605 # ASCII equivalent of the object (field names, indicators, field #
1606 # values and line-breaks) #
1607 ####################################################################
1608 18 ▶sub _marc2ascii {
Subroutine _marc2ascii: called CC=6 SCAR=17.9
1609  
1610 18 my $marcrec=shift;
1611 18 my $args=shift;
1612 18 ▶ my $newline = $args->{'lineterm'} || "\n";
Condition50%1/2
$args->{'lineterm'} || "\n"
l!l
180
MC/DC0%0/1
$args->{'lineterm'} || "\n"
$args->{'lineterm'}
Truth table50%1/2
A
A= $args->{'lineterm'}
Aresult
11
00
1613 18 my $output = "";
1614 18 my $record=$marcrec;
1615 18 foreach my $fields (@{$record->{'array'}}) { #cycle each field
1616 373 my $tag=$fields->[0];
1617 373 ▶ print "ASCII: tag = $tag\n" if $MARC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::DEBUG 0 373 373
1618 373 ▶ if ($tag<10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) { } 95 278 373
1619 95 $output.="$fields->[0] $fields->[1]";
1620 }
1621 else {
1622 278 $output.="$tag $fields->[1]$fields->[2] ";
1623 278 my @subfields = @{$fields}[3..$#{$fields}];
1624 278 ▶ while (@subfields) { #cycle through subfields
Branch: 1 branch
BranchTrueFalseTotal
if (@subfields) 495 278 773
1625 495 $output .= "\$".shift(@subfields).shift(@subfields);
1626 } #finish cycling through subfields
1627 } #finish tag test < 10
1628 373 $output .= $newline; #put a newline at the end of the field
1629 }
1630 18 $output.=$newline; #put an extra newline to separate records
1631 18 return $output;
1632 }
1633  
1634 ####################################################################
1635 # _marcmaker() takes a MARC object as its input and converts it #
1636 # into MARCMaker format, which is returned as a string #
1637 ####################################################################
1638 24 ▶sub _marcmaker { # rec
Subroutine _marcmaker: called CC=14 SCAR=26.4
1639 24 my @output = ();
1640 24 my $marcrec=shift;
1641 24 my $args=shift;
1642 24 my $proto_rec=$args->{'proto_rec'};
1643 24 ▶ unless (exists $args->{'charset'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"charset"}) 6 18 24
1644 6 ▶ unless (exists $proto_rec->{'brkrchar'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $proto_rec->{"brkrchar"}) 5 1 6
1645 5 $proto_rec->{'brkrchar'} = ustext_default(); # hash ref
1646 }
1647 6 $args->{'charset'} = $proto_rec->{'brkrchar'};
1648 6 $proto_rec->{'charset'} = $proto_rec->{'brkrchar'};
1649 }
1650 24 local $^W = 0; # no warnings
1651  
1652 24 my $record=$marcrec;
1653 24 foreach my $fields (@{$record->{'array'}}) { #cycle each field
1654 476 my $tag=$fields->[0];
1655 476 ▶ print "OUT: tag = $tag\n" if $MARC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::DEBUG 0 476 476
1656 476 ▶ if ($tag eq '000') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq '000') { } 24 452 476
elsif ($tag < 10) { } 100 352 452
1657 24 my $value=$fields->[1];
1658 24 $value=~s/ /\\/go;
1659 24 push @output, "=LDR $value";
1660 }
1661 elsif ($tag<10) {
1662 100 my $value = _char2maker($fields->[1], $args->{'charset'});
1663 100 $value=~s/ /\\/go;
1664 100 push @output, "=$tag $value";
1665 }
1666 else {
1667 352 my $indicator1=$fields->[1];
1668 352 $indicator1=~s/ /\\/;
1669 352 my $indicator2=$fields->[2];
1670 352 $indicator2=~s/ /\\/;
1671 352 my $output="=$tag $indicator1$indicator2";
1672 352 my @subfields = @{$fields}[3..$#{$fields}];
1673 352 ▶ while (@subfields) { #cycle through subfields
Branch: 1 branch
BranchTrueFalseTotal
if (@subfields) 650 352 1002
1674 650 my $subfield_id = shift(@subfields);
1675 my $subfield = _char2maker( shift(@subfields),
1676 650 $args->{'charset'} );
1677 650 $output .= "\$$subfield_id$subfield";
1678 } #finish cycling through subfields
1679 352 push @output, $output;
1680 } #finish tag test < 10
1681 }
1682 24 push @output,""; #put an extra blank line to separate records
1683
1684 24 ▶ my $newline = $args->{'lineterm'} || "\015\012";
Condition50%1/2
$args->{'lineterm'} || "\r\n"
l!l
024
MC/DC0%0/1
$args->{'lineterm'} || "\r\n"
$args->{'lineterm'}
Truth table50%1/2
A
A= $args->{'lineterm'}
Aresult
11
00
1685 24 ▶ if ($args->{'nolinebreak'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"nolinebreak"}) 8 16 24
1686 8 my $breaker1 = join ($newline, @output) . $newline;
1687 8 return $breaker1;
1688 }
1689 # linebreak on by default
1690 16 my @output2 = ();
1691 16 foreach my $outline (@output) {
1692 344 ▶ if (length($outline) < 66) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $outline < 66) { } 302 42 344
1693 302 push @output2, $outline;
1694 302 next;
1695 }
1696 else {
1697 42 my @words = split (/\s{1,1}/, $outline);
1698 42 my $outline2 = shift @words;
1699 42 foreach my $word (@words) {
1700 2276 ▶ if (length($outline2) + length($word) < 66) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($outline2) + length($word) < 66) { } 2032 244 2276
1701 2032 $outline2 .= " $word";
1702 }
1703 else {
1704 244 push @output2, $outline2;
1705 244 $outline2 = " $word";
1706 }
1707 }
1708 42 push @output2, $outline2;
1709 }
1710 }
1711 16 my $breaker = join ($newline, @output2);
1712 16 return $breaker;
1713 }
1714  
1715 750 ▶sub _char2maker {
Subroutine _char2maker: called CC=2 SCAR=6.9
1716 750 my @marc_string = split (//, shift);
1717 750 my $charmap = shift;
1718 750 my $maker_string = join ('', map { ${$charmap}{$_} } @marc_string);
1719 750 ▶ while ($maker_string =~ s/(&)([^ ]{1,7}?)(;)/{$2}/o) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($maker_string =~ s/(&)([^ ]{1,7}?)(;)/{$2}/o) 2 750 752
1720 750 return $maker_string;
1721 }
1722  
1723 5 ▶sub ustext_default {
Subroutine ustext_default: called CC=4 SCAR=13.9
1724 5 my @hexchar = (0x00..0x1a,0x1c,0x7f..0x8c,0x8f..0xa0,0xaf,0xbb,
1725 0xbe,0xbf,0xc7..0xdf,0xfc,0xfd,0xff);
1726 5 my %outchar = map {chr($_), sprintf ("{%2.2X}",int $_)} @hexchar;
1727  
1728 5 my @ascchar = map {chr($_)} (0x20..0x23,0x25..0x7a,0x7c,0x7e);
1729 5 foreach my $asc (@ascchar) { $outchar{$asc} = $asc; }
1730  
1731 5 $outchar{chr(0x1b)} = '{esc}'; # escape
1732 5 $outchar{chr(0x24)} = '{dollar}'; # dollar sign
1733 5 $outchar{chr(0x5c)} = '{bsol}'; # back slash (reverse solidus)
1734 5 $outchar{chr(0x7b)} = '{lcub}'; # opening curly brace
1735 5 $outchar{chr(0x7d)} = '{rcub}'; # closing curly brace
1736 5 $outchar{chr(0x8d)} = '{joiner}'; # zero width joiner
1737 5 $outchar{chr(0x8e)} = '{nonjoin}'; # zero width non-joiner
1738 5 $outchar{chr(0xa1)} = '{Lstrok}'; # latin capital letter l with stroke
1739 5 $outchar{chr(0xa2)} = '{Ostrok}'; # latin capital letter o with stroke
1740 5 $outchar{chr(0xa3)} = '{Dstrok}'; # latin capital letter d with stroke
1741 5 $outchar{chr(0xa4)} = '{THORN}'; # latin capital letter thorn (icelandic)
1742 5 $outchar{chr(0xa5)} = '{AElig}'; # latin capital letter AE
1743 5 $outchar{chr(0xa6)} = '{OElig}'; # latin capital letter OE
1744 5 $outchar{chr(0xa7)} = '{softsign}'; # modifier letter soft sign
1745 5 $outchar{chr(0xa8)} = '{middot}'; # middle dot
1746 5 $outchar{chr(0xa9)} = '{flat}'; # musical flat sign
1747 5 $outchar{chr(0xaa)} = '{reg}'; # registered sign
1748 5 $outchar{chr(0xab)} = '{plusmn}'; # plus-minus sign
1749 5 $outchar{chr(0xac)} = '{Ohorn}'; # latin capital letter o with horn
1750 5 $outchar{chr(0xad)} = '{Uhorn}'; # latin capital letter u with horn
1751 5 $outchar{chr(0xae)} = '{mlrhring}'; # modifier letter right half ring (alif)
1752 5 $outchar{chr(0xb0)} = '{mllhring}'; # modifier letter left half ring (ayn)
1753 5 $outchar{chr(0xb1)} = '{lstrok}'; # latin small letter l with stroke
1754 5 $outchar{chr(0xb2)} = '{ostrok}'; # latin small letter o with stroke
1755 5 $outchar{chr(0xb3)} = '{dstrok}'; # latin small letter d with stroke
1756 5 $outchar{chr(0xb4)} = '{thorn}'; # latin small letter thorn (icelandic)
1757 5 $outchar{chr(0xb5)} = '{aelig}'; # latin small letter ae
1758 5 $outchar{chr(0xb6)} = '{oelig}'; # latin small letter oe
1759 5 $outchar{chr(0xb7)} = '{hardsign}'; # modifier letter hard sign
1760 5 $outchar{chr(0xb8)} = '{inodot}'; # latin small letter dotless i
1761 5 $outchar{chr(0xb9)} = '{pound}'; # pound sign
1762 5 $outchar{chr(0xba)} = '{eth}'; # latin small letter eth
1763 5 $outchar{chr(0xbc)} = '{ohorn}'; # latin small letter o with horn
1764 5 $outchar{chr(0xbd)} = '{uhorn}'; # latin small letter u with horn
1765 5 $outchar{chr(0xc0)} = '{deg}'; # degree sign
1766 5 $outchar{chr(0xc1)} = '{scriptl}'; # latin small letter script l
1767 5 $outchar{chr(0xc2)} = '{phono}'; # sound recording copyright
1768 5 $outchar{chr(0xc3)} = '{copy}'; # copyright sign
1769 5 $outchar{chr(0xc4)} = '{sharp}'; # sharp
1770 5 $outchar{chr(0xc5)} = '{iquest}'; # inverted question mark
1771 5 $outchar{chr(0xc6)} = '{iexcl}'; # inverted exclamation mark
1772 5 $outchar{chr(0xe0)} = '{hooka}'; # combining hook above
1773 5 $outchar{chr(0xe1)} = '{grave}'; # combining grave
1774 5 $outchar{chr(0xe2)} = '{acute}'; # combining acute
1775 5 $outchar{chr(0xe3)} = '{circ}'; # combining circumflex
1776 5 $outchar{chr(0xe4)} = '{tilde}'; # combining tilde
1777 5 $outchar{chr(0xe5)} = '{macr}'; # combining macron
1778 5 $outchar{chr(0xe6)} = '{breve}'; # combining breve
1779 5 $outchar{chr(0xe7)} = '{dot}'; # combining dot above
1780 5 $outchar{chr(0xe8)} = '{uml}'; # combining diaeresis (umlaut)
1781 5 $outchar{chr(0xe9)} = '{caron}'; # combining hacek
1782 5 $outchar{chr(0xea)} = '{ring}'; # combining ring above
1783 5 $outchar{chr(0xeb)} = '{llig}'; # combining ligature left half
1784 5 $outchar{chr(0xec)} = '{rlig}'; # combining ligature right half
1785 5 $outchar{chr(0xed)} = '{rcommaa}'; # combining comma above right
1786 5 $outchar{chr(0xee)} = '{dblac}'; # combining double acute
1787 5 $outchar{chr(0xef)} = '{candra}'; # combining candrabindu
1788 5 $outchar{chr(0xf0)} = '{cedil}'; # combining cedilla
1789 5 $outchar{chr(0xf1)} = '{ogon}'; # combining ogonek
1790 5 $outchar{chr(0xf2)} = '{dotb}'; # combining dot below
1791 5 $outchar{chr(0xf3)} = '{dbldotb}'; # combining double dot below
1792 5 $outchar{chr(0xf4)} = '{ringb}'; # combining ring below
1793 5 $outchar{chr(0xf5)} = '{dblunder}'; # combining double underscore
1794 5 $outchar{chr(0xf6)} = '{under}'; # combining underscore
1795 5 $outchar{chr(0xf7)} = '{commab}'; # combining comma below
1796 5 $outchar{chr(0xf8)} = '{rcedil}'; # combining right cedilla
1797 5 $outchar{chr(0xf9)} = '{breveb}'; # combining breve below
1798 5 $outchar{chr(0xfa)} = '{ldbltil}'; # combining double tilde left half
1799 5 $outchar{chr(0xfb)} = '{rdbltil}'; # combining double tilde right half
1800 5 $outchar{chr(0xfe)} = '{commaa}'; # combining comma above
1801 5 ▶ if ($MARC::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::DEBUG) 0 5 5
1802 0 foreach my $num (sort keys %outchar) {
1803 0 printf "%x = %s\n", ord($num), $outchar{$num};
1804 }
1805 }
1806 5 return \%outchar;
1807 }
1808  
1809 ####################################################################
1810 # _marc2html takes a MARC object as its input and converts it into #
1811 # HTML. It is possible to specify which field you want to output #
1812 # as well as field labels to be used instead of the MARC codes. #
1813 # The HTML is returned as a string #
1814 ####################################################################
1815 20 ▶sub _marc2html { # rec
Subroutine _marc2html: called CC=8 SCAR=21.1
1816 20 my $marcrec = shift;
1817 20 my $args = shift;
1818 20 ▶ my $newline = $args->{'lineterm'} || "\n";
Condition50%1/2
$args->{'lineterm'} || "\n"
l!l
020
MC/DC0%0/1
$args->{'lineterm'} || "\n"
$args->{'lineterm'}
Truth table50%1/2
A
A= $args->{'lineterm'}
Aresult
11
00
1819 20 my $output = "";
1820 20 my $outputall = 1;
1821  
1822 20 my @tags =();
1823 20 @tags = grep /^[0-9]/, sort(keys(%{$args}));
1824  
1825 20 ▶ $outputall = 0 if (scalar(@tags));
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tags 16 4 20
1826 20 ▶ if (defined $args->{'fields'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"fields"}) 0 20 20
1827 0 ▶ if ($args->{'fields'} =~ /all$/oi) {$outputall=1} ## still needed ?????
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"fields"} =~ /all$/io) 0 0 0
1828 }
1829  
1830  
1831 20 my %tags =();
1832  
1833 20 %tags = map {$_=>1} @tags;
1834 20 ▶ %tags = map {$_->[0]=>1} @{$marcrec->{'array'}} if $outputall;
Branch: 1 branch
BranchTrueFalseTotal
if $outputall 4 16 20
1835 #if 'all' fields are specified then set $outputall flag to yes
1836 20 local $^W = 0; # no warnings
1837  
1838 20 my $j=$marcrec;
1839 20 $output.= $newline."<p>";
1840
1841 20 foreach my $rfield (@{$j->{'array'}}) {
1842 $output.= $rfield->[0]." ".$j->_joinfield($rfield,$rfield->[0])."<br>".$newline
1843 450 ▶ if $tags{$rfield->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $tags{$rfield->[0]} 106 344 450
1844 }
1845 20 $output.="</p>";
1846 20 return $output;
1847 }
1848  
1849  
1850 ####################################################################
1851 # _urls() takes a MARC object as its input, and then extracts the #
1852 # control# (MARC 001) and URLs (MARC 856) and outputs them as #
1853 # hypertext links in an HTML page. This could then be used with a #
1854 # link checker to determine what URLs are broken. #
1855 ####################################################################
1856 4 ▶sub _urls {
Subroutine _urls: called CC=7 SCAR=19.5
1857 4 my $marcrec = shift;
1858 4 my $args = shift;
1859 4 ▶ my $newline = $args->{'lineterm'} || "\n";
Condition50%1/2
$args->{'lineterm'} || "\n"
l!l
04
MC/DC0%0/1
$args->{'lineterm'} || "\n"
$args->{'lineterm'}
Truth table50%1/2
A
A= $args->{'lineterm'}
Aresult
11
00
1860 4 my $output = "";
1861
1862 4 my $controlnum=undef;
1863 4 foreach my $rfield (@{$marcrec->{'array'}}) {
1864 90 ▶ if ($rfield->[0] eq "001") {
Branch: 2 branches
BranchTrueFalseTotal
if ($rfield->[0] eq '001') { } 4 86 90
elsif ($rfield->[0] eq '856') { } 2 84 86
1865 4 $controlnum= $rfield->[1];
1866 }
1867 elsif ($rfield->[0] eq "856") {
1868 2 ▶ for (my $k=3; $k< $#$rfield; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $rfield->$#*) 10 2 12
1869 10 ▶ if ($rfield->[$k] eq "u") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rfield->[$k] eq "u") 2 8 10
1870 2 $output.=qq{<a href="$rfield->[$k+1]">$controlnum :}.
1871 qq{$rfield->[$k+1]</a><br>$newline};
1872 }
1873 }
1874 }
1875 }
1876 4 return $output;
1877 }
1878  
1879 ####################################################################
1880 # isbd() attempts to create a quasi ISBD output format #
1881 ####################################################################
1882 4 ▶sub _isbd { # rec
Subroutine _isbd: called CC=12 SCAR=28.7
1883 4 my $marcrec=shift;
1884 4 my $args=shift;
1885  
1886 4 my $output = "";
1887 4 ▶ my $newline = $args->{'lineterm'} || "\n";
Condition50%1/2
$args->{'lineterm'} || "\n"
l!l
04
MC/DC0%0/1
$args->{'lineterm'} || "\n"
$args->{'lineterm'}
Truth table50%1/2
A
A= $args->{'lineterm'}
Aresult
11
00
1888  
1889 90 my @reporting_fields = grep {$_->[0] =~/020|245|250|260|300|440|490|5../}
1890 4 @{$marcrec->{'array'}}; # optimization.
1891 4 my %tagfields = (); # This will allow random access to fields based on tags
1892 4 foreach my $rfield (@reporting_fields) {
1893 30 push @{$tagfields{$rfield->[0]}},$rfield;
1894 }
1895 4 $output .= $marcrec->_joinfield($tagfields{245}[0],"245");
1896 4 for (qw/250 260 300/) {
1897 12 ▶ $output .= " -- ". $marcrec->_joinfield($tagfields{$_}[0],$_) if $tagfields{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $tagfields{$_} 6 6 12
1898 }
1899 4 ▶ if ($tagfields{'440'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagfields{440}) 0 4 4
1900 0 $output .= " -- ";
1901 0 foreach my $rfield (@{$tagfields{'440'}}) {
1902 0 $output .= "(".$marcrec->_joinfield($rfield,"440").") ";
1903 }
1904 }
1905 4 ▶ if ($tagfields{'490'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagfields{490}) 0 4 4
1906 0 ▶ $output .= " -- " unless $tagfields{'440'};
Branch: 1 branch
BranchTrueFalseTotal
unless $tagfields{440} 0 0 0
1907 0 foreach my $rfield (@{$tagfields{'490'}}) {
1908 0 $output .= "(".$marcrec->_joinfield($rfield,"490").") ";
1909 }
1910 }
1911 4 my @f500s = grep {$_->[0] =~/5../} @reporting_fields;
1912 4 foreach my $rfield (@f500s) {
1913 20 $output .= $newline.$marcrec->_joinfield($rfield,$rfield->[0]);
1914 }
1915 4 ▶ if ($tagfields{'020'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagfields{"020"}) 0 4 4
1916 0 $output .= $newline.$marcrec->_joinfield($tagfields{'020'}[0]);
1917 }
1918 4 $output .= $newline.$newline;
1919 4 return $output;
1920 }
1921  
1922 ####################################################################
1923  
1924 # createrecord takes a string leader and returns a new record with
1925 # leader information at the appropriate place.
1926  
1927 ####################################################################
1928 22 ▶sub createrecord { # rec
Subroutine createrecord: called CC=2 SCAR=6.9
1929 22 my $marcrec = shift;
1930 22 local $^W = 0; # no warnings
1931 22 ▶ my $leader=shift || "00000nam 2200000 a 4500";
Condition50%1/2
shift() || '00000nam 2200000 a 4500'
l!l
220
MC/DC0%0/1
shift() || '00000nam 2200000 a 4500'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1932 22 my $newrec = $marcrec->copy_struct();
1933 #default leader see MARC documentation http://lcweb.loc.gov/marc
1934 22 my @ldrfield = ('000',$leader);
1935 22 $newrec->field_updatehook(\@ldrfield);
1936 22 push (@{$newrec->{'000'}},@ldrfield); #create map
1937 22 push(@{$newrec->{'array'}},$newrec->{'000'});
1938 22 return $newrec;
1939 }
1940  
1941 ####################################################################
1942 # nextrec() will read in a record from a filehandle
1943 # already been opened with openmarc(). the increment can be #
1944 # adjusted if necessary by passing a new value as a parameter. the #
1945 # new records will be APPENDED to the MARC object #
1946 ####################################################################
1947 0 ▶sub nextrec {
Subroutine nextrec: not called CC=4 SCAR=30.0
1948 0 my $marcrec=shift;
1949 0 ▶ if (not($marcrec->{'handle'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($marcrec->{"handle"}) 0 0 0
1950 0 mycarp "There isn't a MARC file open";
1951 0 return;
1952 }
1953 0 ▶ if ($marcrec->{'format'} =~ /usmarc/oi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($marcrec->{'format'} =~ /usmarc/io) { } 0 0 0
elsif ($marcrec->{'format'} =~ /marcmaker/io) { } 0 0 0
1954 0 return _readmarc($marcrec);
1955 }
1956 elsif ($marcrec->{'format'} =~ /marcmaker/oi) {
1957 0 return _readmarcmaker($marcrec);
1958 }
1959 0 else {return (undef,-3)}
1960 }
1961  
1962 ####################################################################
1963  
1964 # Add_map is the rec equivalent of MARC::add_map (as usual, without
1965 # the record number).
1966  
1967 ####################################################################
1968 460 ▶sub add_map { # rec
Subroutine add_map: called CC=4 SCAR=13.9
1969 460 my $marcrec=shift;
1970 460 my $rafield = shift;
1971 460 my $tag = $rafield->[0];
1972 460 ▶ return undef if $tag eq '000'; #currently handle ldr yourself...
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "000" 0 460 460
1973 460 my @tmp = @$rafield;
1974 460 my $field_len = $#tmp;
1975 460 my $record = $marcrec;
1976 460 ▶ if ($tag > 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag > 10) 365 95 460
1977 365 my $i1 = $rafield->[1];
1978 365 my $i2 = $rafield->[2];
1979 365 my $i12 = $i1.$i2;
1980  
1981 365 ▶ for(my $i=3;$i<$field_len;$i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $field_len) 652 365 1017
1982 652 my $subf_code = $rafield->[$i];
1983 652 push(@{$record->{$tag}{$subf_code}}, \$rafield->[$i+1]);
1984 }
1985 365 push(@{$record->{$tag}{'i1'}{$i1}},$rafield);
1986 365 push(@{$record->{$tag}{'i2'}{$i2}},$rafield);
1987 365 push(@{$record->{$tag}{'i12'}{$i12}},$rafield);
1988 }
1989 460 push(@{$record->{$tag}{field}},$rafield);
1990 }
1991  
1992 ####################################################################
1993  
1994 # rebuild_map() is the ::Rec version of MARC::rebuild_map().
1995  
1996 ####################################################################
1997 7 ▶sub rebuild_map { # rec
Subroutine rebuild_map: called CC=3 SCAR=11.0
1998 7 my $marcrec=shift;
1999 7 my $tag = shift;
2000 7 ▶ return undef if $tag eq '000'; #currently ldr is different...
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "000" 0 7 7
2001 7 my @tagrefs = grep {$_->[0] eq $tag} @{$marcrec->{'array'}};
2002 7 delete $marcrec->{$tag};
2003 7 for (@tagrefs) {$marcrec->add_map($_)};
2004 }
2005  
2006 ####################################################################
2007  
2008 # rebuild_map_all() is the ::Rec version of MARC::rebuild_map_all()
2009  
2010 ####################################################################
2011 0 ▶sub rebuild_map_all { # rec
Subroutine rebuild_map_all: not called CC=2 SCAR=17.9
2012 0 my $marcrec=shift;
2013 0 my %tags=();
2014 0 map {$tags{$_->[0]}++} @{$marcrec->{'array'}};
2015 0 foreach my $tag (keys %tags) {$marcrec->rebuild_map($tag)};
2016 }
2017  
2018  
2019  
2020 ####################################################################
2021  
2022 # Reads the next record out of the handle. Returns a pair (new
2023 # record,status). Status is 1 in the generic case. Status is -1 if
2024 # lengths do not match -2 if leader size is not numeric, undef if at
2025 # the last record. New record is undef if there is an error or at the
2026 # last record.
2027  
2028 ####################################################################
2029 17 ▶sub _readmarc { # rec
Subroutine _readmarc: called CC=12 SCAR=24.9
2030 17 my $marcrec = shift;
2031 17 my $handle = $marcrec->{'handle'};
2032 17 my $string = shift;
2033 17 local $/ = "\035"; # cf. TPJ #14
2034 17 local $^W = 0; # no warnings
2035 17 my $line;
2036 17 ▶ $line = $string if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 0 17 17
2037 17 ▶ $line = <$handle> if $handle and !defined($string);
Branch: 1 branch
BranchTrueFalseTotal
if $handle and not defined $string 17 0 17
Condition33%1/3
$handle and not defined $string
!ll&&!rl&&r
0017
MC/DC0%0/2
$handle and not defined $string
$handle not defined $string
Truth table33%1/3
A and not(B)
A= $handleB= not defined $string
ABresult
0X0
100
111
2038 17 my $recordlength = substr($line,0,5);
2039 17 my $octets = length ($line);
2040 17 $line=~s/[\n\r\cZ]//og;
2041 17 ▶ return (undef,undef) unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 5 12 17
2042 12 ▶ if ($recordlength =~ /\d{5}/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recordlength =~ /\d{5}/o) { } 11 1 12
2043 11 ▶ print "recordlength = $recordlength, length = $octets\n"
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::DEBUG 0 11 11
2044 if $MARC::DEBUG;
2045 11 ▶ return (undef,-1) unless $recordlength == $octets;
Branch: 1 branch
BranchTrueFalseTotal
unless $recordlength == $octets 1 10 11
2046 } else {
2047 1 return (undef,-2);
2048 }
2049 10 my @d = ();
2050 10 $line=~/^(.{24})([^\036]*)\036(.*)/o;
2051 10 my $leader=$1; my $dir=$2; my $data=$3;
2052 10 my $record = $marcrec->createrecord($leader);
2053  
2054 10 @d=$dir=~/(.{12})/go;
2055 10 for my $d(@d) {
2056 215 my @field=();
2057 215 my $tag=substr($d,0,3);
2058 215 chop(my $field=substr($data,substr($d,7,5),substr($d,3,4)));
2059 215 ▶ if ($tag<10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) { } 45 170 215
2060 45 @field=($tag,$field);
2061 }
2062 else {
2063 170 my ($indi1, $indi2, $field_data) = unpack ("a1a1a*", $field);
2064
2065 170 push (@field, $tag, $indi1, $indi2);
2066
2067 170 my @subfields = split(/\037/,$field_data);
2068 170 foreach (@subfields) {
2069 450 my $delim = substr($_,0,1);
2070 450 ▶ next unless $delim;
Branch: 1 branch
BranchTrueFalseTotal
unless $delim 170 280 450
2071 280 my $subfield_data = substr($_,1);
2072 280 push(@field, $delim, $subfield_data);
2073
2074 } #end parsing subfields
2075 } #end testing tag number
2076 215 push(@{$record->{'array'}},\@field);
2077 215 $record-> add_map(\@field);
2078 } #end processing this field
2079 10 return ($record,1);
2080 }
2081  
2082 ###################################################################
2083 # readmarcmaker() reads a marcmaker file into the MARC object #
2084 ###################################################################
2085 15 ▶sub _readmarcmaker { # rec
Subroutine _readmarcmaker: called CC=13 SCAR=25.9
2086 15 my $marcrec = shift;
2087 15 my $handle = $marcrec->{'handle'};
2088 15 my $string = shift;
2089 15 my $record;
2090  
2091 15 ▶ unless (exists $marcrec->{'makerchar'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $marcrec->{"makerchar"}) 0 15 15
2092 0 $marcrec->{'makerchar'} = usmarc_default(); # hash ref
2093 }
2094 15 my $charset = $marcrec->{makerchar};
2095 15 ▶ my $lineterm = $marcrec->{'lineterm'} || "\015\012";
Condition50%1/2
$marcrec->{'lineterm'} || "\r\n"
l!l
015
MC/DC0%0/1
$marcrec->{'lineterm'} || "\r\n"
$marcrec->{'lineterm'}
Truth table50%1/2
A
A= $marcrec->{'lineterm'}
Aresult
11
00
2096 # MS-DOS file default for MARCMaker
2097  
2098 #Set the file input separator to "\r\n\r\n", which is the same as
2099 #a blank line. A single blank line separates individual MARC records
2100 #in the MARCMakr format.
2101 15 local $/ = "$lineterm$lineterm"; # cf. TPJ #14
2102 15 local $^W = 0; # no warnings
2103 15 ▶ $record = $string if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 0 15 15
2104 15 ▶ $record = <$handle> if $handle and !defined($string);
Branch: 1 branch
BranchTrueFalseTotal
if $handle and not defined $string 15 0 15
Condition33%1/3
$handle and not defined $string
!ll&&!rl&&r
0015
MC/DC0%0/2
$handle and not defined $string
$handle not defined $string
Truth table33%1/3
A and not(B)
A= $handleB= not defined $string
ABresult
0X0
100
111
2105  
2106 15 ▶ return (undef,undef) unless $record;
Branch: 1 branch
BranchTrueFalseTotal
unless $record 1 14 15
2107 #Split each record on the "\n=" into the @fields array
2108 14 my @lines=split "$lineterm=",$record;
2109 14 my $leader = shift @lines;
2110 14 ▶ unless ($leader =~ /^=LDR /o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($leader =~ /^=LDR /o) 2 12 14
2111 2 return (undef, -1);
2112 }
2113
2114 12 $leader=~s/^=LDR //o; #Remove "=LDR "
2115 12 $leader=~s/[\n\r]//og;
2116 12 $leader=~s/\\/ /go; # substitute " " for \
2117 12 my $rec = $marcrec->createrecord($leader);
2118 12 foreach my $line (@lines) {
2119 #Remove newlines from @fields ; and also substitute " " for \
2120 214 $line=~s/[\n\r]//og;
2121 214 $line=~s/\\/ /go;
2122 #get the tag name
2123 214 my $tag = substr($line,0,3);
2124 214 my @field=(); #this will be added to $marcrec and the map updated.
2125 #if the tag is less than 010 (has no indicators or subfields)
2126 #then push the data into @$field
2127 214 ▶ if ($tag < 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) { } 48 166 214
2128 48 my $value = _maker2char (substr($line,5), $charset);
2129 48 @field=($tag,$value);
2130 }
2131 else {
2132 #elseif the tag is greater than 010 (has indicators and
2133 #subfields then add the data to the $marc object
2134 166 my $field_data=substr($line,7);
2135 166 my $i1=substr($line,5,1);
2136 166 my $i2=substr($line,6,1);
2137 166 @field = ($tag,$i1,$i2);
2138
2139 166 my @subfields=split /\$/, $field_data; #get the subfields
2140 166 foreach my $subfield (@subfields) {
2141 495 my $delim=substr($subfield,0,1); #extract subfield delimiter
2142 495 ▶ next unless $delim;
Branch: 1 branch
BranchTrueFalseTotal
unless $delim 166 329 495
2143 329 my $subfield_data= MARC::_maker2char (substr($subfield,1),
2144 $charset);
2145 #extract subfield value
2146 329 push (@field, $delim, $subfield_data);
2147 } #end parsing subfields
2148 } #end tag>10
2149 214 ▶ print "DEBUG: tag = $tag\n" if $MARC::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::DEBUG 0 214 214
2150 214 push @{$rec->{'array'}},\@field;
2151 214 $rec -> add_map(\@field);
2152 } #end reading this line
2153 12 return ($rec,1);
2154 } #end reading this record
2155  
2156 377 ▶sub _maker2char { # rec
Subroutine _maker2char: called CC=3 SCAR=11.0
2157 377 my $marc_string = shift;
2158 377 my $charmap = shift;
2159 377 ▶ while ($marc_string =~ /{(\w{1,8}?)}/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($marc_string =~ /{(\w{1,8}?)}/o) 189 377 566
2160 189 ▶ if (exists ${$charmap}{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $charmap }->{$1}) { } 186 3 189
2161 186 $marc_string = join ('', $`, ${$charmap}{$1}, $');
2162 }
2163 else {
2164 3 $marc_string = join ('', $`, '&', $1, ';', $');
2165 }
2166 }
2167 # closing curly brace - part 2, permits {lcub}text{rcub} in input
2168 377 $marc_string =~ s/\&rcub;/\x7d/go;
2169 377 return $marc_string;
2170 }
2171  
2172 1 ▶sub usmarc_default { # rec
Subroutine usmarc_default: called CC=3 SCAR=11.0
2173 1 my @hexchar = (0x00..0x1a,0x1c,0x7f..0x8c,0x8f..0xa0,0xaf,0xbb,
2174 0xbe,0xbf,0xc7..0xdf,0xfc,0xfd,0xff);
2175 1 my %inchar = map {sprintf ("%2.2X",int $_), chr($_)} @hexchar;
2176  
2177 1 $inchar{esc} = chr(0x1b); # escape
2178 1 $inchar{dollar} = chr(0x24); # dollar sign
2179 1 $inchar{curren} = chr(0x24); # dollar sign - alternate
2180 1 $inchar{24} = chr(0x24); # dollar sign - alternate
2181 1 $inchar{bsol} = chr(0x5c); # back slash (reverse solidus)
2182 1 $inchar{lcub} = chr(0x7b); # opening curly brace
2183 1 $inchar{rcub} = "&rcub;"; # closing curly brace - part 1
2184 1 $inchar{joiner} = chr(0x8d); # zero width joiner
2185 1 $inchar{nonjoin} = chr(0x8e); # zero width non-joiner
2186 1 $inchar{Lstrok} = chr(0xa1); # latin capital letter l with stroke
2187 1 $inchar{Ostrok} = chr(0xa2); # latin capital letter o with stroke
2188 1 $inchar{Dstrok} = chr(0xa3); # latin capital letter d with stroke
2189 1 $inchar{THORN} = chr(0xa4); # latin capital letter thorn (icelandic)
2190 1 $inchar{AElig} = chr(0xa5); # latin capital letter AE
2191 1 $inchar{OElig} = chr(0xa6); # latin capital letter OE
2192 1 $inchar{softsign} = chr(0xa7); # modifier letter soft sign
2193 1 $inchar{middot} = chr(0xa8); # middle dot
2194 1 $inchar{flat} = chr(0xa9); # musical flat sign
2195 1 $inchar{reg} = chr(0xaa); # registered sign
2196 1 $inchar{plusmn} = chr(0xab); # plus-minus sign
2197 1 $inchar{Ohorn} = chr(0xac); # latin capital letter o with horn
2198 1 $inchar{Uhorn} = chr(0xad); # latin capital letter u with horn
2199 1 $inchar{mlrhring} = chr(0xae); # modifier letter right half ring (alif)
2200 1 $inchar{mllhring} = chr(0xb0); # modifier letter left half ring (ayn)
2201 1 $inchar{lstrok} = chr(0xb1); # latin small letter l with stroke
2202 1 $inchar{ostrok} = chr(0xb2); # latin small letter o with stroke
2203 1 $inchar{dstrok} = chr(0xb3); # latin small letter d with stroke
2204 1 $inchar{thorn} = chr(0xb4); # latin small letter thorn (icelandic)
2205 1 $inchar{aelig} = chr(0xb5); # latin small letter ae
2206 1 $inchar{oelig} = chr(0xb6); # latin small letter oe
2207 1 $inchar{hardsign} = chr(0xb7); # modifier letter hard sign
2208 1 $inchar{inodot} = chr(0xb8); # latin small letter dotless i
2209 1 $inchar{pound} = chr(0xb9); # pound sign
2210 1 $inchar{eth} = chr(0xba); # latin small letter eth
2211 1 $inchar{ohorn} = chr(0xbc); # latin small letter o with horn
2212 1 $inchar{uhorn} = chr(0xbd); # latin small letter u with horn
2213 1 $inchar{deg} = chr(0xc0); # degree sign
2214 1 $inchar{scriptl} = chr(0xc1); # latin small letter script l
2215 1 $inchar{phono} = chr(0xc2); # sound recording copyright
2216 1 $inchar{copy} = chr(0xc3); # copyright sign
2217 1 $inchar{sharp} = chr(0xc4); # sharp
2218 1 $inchar{iquest} = chr(0xc5); # inverted question mark
2219 1 $inchar{iexcl} = chr(0xc6); # inverted exclamation mark
2220 1 $inchar{hooka} = chr(0xe0); # combining hook above
2221 1 $inchar{grave} = chr(0xe1); # combining grave
2222 1 $inchar{acute} = chr(0xe2); # combining acute
2223 1 $inchar{circ} = chr(0xe3); # combining circumflex
2224 1 $inchar{tilde} = chr(0xe4); # combining tilde
2225 1 $inchar{macr} = chr(0xe5); # combining macron
2226 1 $inchar{breve} = chr(0xe6); # combining breve
2227 1 $inchar{dot} = chr(0xe7); # combining dot above
2228 1 $inchar{diaer} = chr(0xe8); # combining diaeresis
2229 1 $inchar{uml} = chr(0xe8); # combining umlaut
2230 1 $inchar{caron} = chr(0xe9); # combining hacek
2231 1 $inchar{ring} = chr(0xea); # combining ring above
2232 1 $inchar{llig} = chr(0xeb); # combining ligature left half
2233 1 $inchar{rlig} = chr(0xec); # combining ligature right half
2234 1 $inchar{rcommaa} = chr(0xed); # combining comma above right
2235 1 $inchar{dblac} = chr(0xee); # combining double acute
2236 1 $inchar{candra} = chr(0xef); # combining candrabindu
2237 1 $inchar{cedil} = chr(0xf0); # combining cedilla
2238 1 $inchar{ogon} = chr(0xf1); # combining ogonek
2239 1 $inchar{dotb} = chr(0xf2); # combining dot below
2240 1 $inchar{dbldotb} = chr(0xf3); # combining double dot below
2241 1 $inchar{ringb} = chr(0xf4); # combining ring below
2242 1 $inchar{dblunder} = chr(0xf5); # combining double underscore
2243 1 $inchar{under} = chr(0xf6); # combining underscore
2244 1 $inchar{commab} = chr(0xf7); # combining comma below
2245 1 $inchar{rcedil} = chr(0xf8); # combining right cedilla
2246 1 $inchar{breveb} = chr(0xf9); # combining breve below
2247 1 $inchar{ldbltil} = chr(0xfa); # combining double tilde left half
2248 1 $inchar{rdbltil} = chr(0xfb); # combining double tilde right half
2249 1 $inchar{commaa} = chr(0xfe); # combining comma above
2250 1 ▶ if ($MARC::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::DEBUG) 0 1 1
2251 0 foreach my $str (sort keys %inchar) {
2252 0 printf "%s = %x\n", $str, ord($inchar{$str});
2253 }
2254 }
2255 1 return \%inchar;
2256 }
2257  
2258 ####################################################################
2259  
2260 # updatefirst() takes a template, a request to rebuild the index, and
2261 # an array from $marc->[recnum]{array}. It replaces/creates the field
2262 # data for a first match, using the template, and leaves the rest
2263 # alone. If the template has a subfield element, (this includes
2264 # indicators) it ignores all other information in the array and only
2265 # updates/creates based on the subfield information in the array. If
2266 # the template has no subfield information then indicators are left
2267 # untouched unless a new field needs to be created, in which case they
2268 # are left blank.
2269  
2270 ####################################################################
2271  
2272 95 ▶sub updatefirst { # rec
Subroutine updatefirst: called CC=29 SCAR=37.1
2273 95 ▶ my $marcrec = shift || return;
Condition50%1/2
shift() || (return)
l!l
950
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2274 95 my $template = shift;
2275 95 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 95 95
2276 95 ▶ return unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 95 95
2277 95 ▶ return if (defined $template->{'value'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $template->{"value"} 0 95 95
2278  
2279  
2280 95 my @ufield = @_;
2281 95 my $field = $template->{'field'};
2282 95 my $subfield = $template->{'subfield'};
2283 95 my $do_rebuild_map = $template->{'rebuild_map'};
2284  
2285 95 $ufield[0]= $field;
2286 95 my $ufield_lt_10_value = $ufield[1];
2287 95 my $ftemplate = {field=>$field};
2288 0 ▶ if (!$field) {mycarp "Need a field to configure my changing needs."; return undef}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 95 95
2289  
2290 95 my @fieldrefs = $marcrec->getfields($template);
2291  
2292 # An invariant is that at most one element of @fieldrefs is affected.
2293 95 ▶ if ($field and not($subfield)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field and not $subfield) 91 4 95
Condition66%2/3
$field and not $subfield
!ll&&!rl&&r
0491
MC/DC50%1/2
$field and not $subfield
$field not $subfield
Truth table66%2/3
A and not(B)
A= $fieldB= not $subfield
ABresult
0X0
100
111
2294 #save the indicators! Yes! Yes!
2295 91 my ($i1,$i2) = (" "," ");
2296 91 ▶ if (defined($fieldrefs[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fieldrefs[0]) 89 2 91
2297 89 $i1 = $fieldrefs[0][1];
2298 89 $i2 = $fieldrefs[0][2];
2299 }
2300 91 $ufield[1]=$i1;
2301 91 $ufield[2]=$i2;
2302 91 ▶ if ($field <10) {@ufield = ($field,$ufield_lt_10_value)}
Branch: 1 branch
BranchTrueFalseTotal
if ($field < 10) 87 4 91
2303 91 my $rafieldrefs = \@fieldrefs;
2304 91 $marcrec->field_updatehook(\@ufield);
2305 91 $rafieldrefs->[0] = \@ufield;
2306 91 ▶ if (!scalar(@fieldrefs)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @fieldrefs) 0 91 91
2307 0 $marcrec->updatefields($template,$rafieldrefs);
2308 0 return;
2309 }
2310 91 $fieldrefs[0]=\@ufield;
2311 #There is no issue with $fieldrefs being taken over by the splice in updatefields.
2312 # in current testing. Perl may change its behavior later...
2313 91 $marcrec->updatefields($template,\@fieldrefs);
2314 91 return;
2315 } #end field.
2316 # The case of adding first subfields is hard. (Not too bad with
2317 # indicators since every non-control field has them.)
2318 # OK, we have field, and subfield.
2319 4 ▶ if ($field and $subfield) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field and $subfield) 4 0 4
Condition33%1/3
$field and $subfield
!ll&&!rl&&r
004
MC/DC0%0/2
$field and $subfield
$field $subfield
Truth table33%1/3
A and B
A= $fieldB= $subfield
ABresult
0X0
100
111
2320 0 ▶ if ($field <10) {croak "Cannot update subfields of control fields"; return undef}
Branch: 1 branch
BranchTrueFalseTotal
if ($field < 10) 1 3 4
2321  
2322 3 my $rvictim=0;
2323 3 my $fieldnum = 0;
2324 3 my $rval = 0;
2325 3 foreach my $fieldref (@fieldrefs) {
2326 7 $rval = $marcrec->getmatch($subfield,$fieldref);
2327 7 ▶ if ($rval){
Branch: 1 branch
BranchTrueFalseTotal
if ($rval) 1 6 7
2328 1 $rvictim=$fieldref;
2329 1 last;
2330 }
2331 6 $fieldnum++;
2332 }
2333 # At this stage we have the number of the field $fieldnum,
2334 # whether there is a match, $rvictim,
2335 # and what to update if there is, $rval.
2336  
2337 3 ▶ if (!$rvictim and $subfield =~/^i[12]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rvictim and $subfield =~ /^i[12]$/) 0 3 3
Condition66%2/3
not $rvictim and $subfield =~ /^i[12]$/
!ll&&!rl&&r
120
MC/DC0%0/2
not $rvictim and $subfield =~ /^i[12]$/
not $rvictim $subfield =~ /^i[12]$/
Truth table66%2/3
not(A) and B
A= not $rvictimB= $subfield =~ /^i[12]$/
ABresult
0X0
100
111
2338 0 mycarp "Field $field does not exist. Can only add indicator $subfield to existing fields.";
2339 0 return undef;
2340 }
2341 #Now we need to find first match in @ufield.
2342 3 my $usub = undef;
2343 3 ▶ $usub=$ufield[1] if $subfield eq 'i1';
Branch: 1 branch
BranchTrueFalseTotal
if $subfield eq "i1" 0 3 3
2344 3 ▶ $usub=$ufield[2] if $subfield eq 'i2';
Branch: 1 branch
BranchTrueFalseTotal
if $subfield eq "i2" 0 3 3
2345  
2346 3 ▶ for(my $i=3;$i<@ufield;$i = $i+2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ufield) 5 0 5
2347 5 my $sub = $ufield[$i];
2348 5 ▶ if ($sub eq $subfield) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub eq $subfield) 3 2 5
2349 3 $usub = $ufield[$i+1];
2350 3 last;
2351 }
2352 }
2353 3 ▶ mycarp(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $usub 0 3 3
2354 "Did not find $subfield in spec (".
2355 join " ",@ufield . ")"
2356 ) if !defined($usub);
2357  
2358 3 ▶ if (!scalar(@fieldrefs)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @fieldrefs) 0 3 3
2359 0 my @newfield = ($field, ' ',' ', $subfield =>$usub);
2360 0 my $rafields;
2361 0 $marcrec->field_updatehook(\@newfield);
2362 0 $rafields->[0] = \@newfield;
2363 0 return $marcrec->updatefields($template,$rafields);
2364 }
2365 #The general insert case.
2366 3 ▶ if (!$rvictim and scalar(@fieldrefs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rvictim and scalar @fieldrefs) 2 1 3
Condition100%2/2
not $rvictim and scalar @fieldrefs
!ll
12
MC/DC100%1/1
not $rvictim and scalar @fieldrefs
not $rvictim
Truth table100%2/2
not(A)
A= not $rvictim
Aresult
00
11
2367 2 $rvictim = $fieldrefs[0];
2368 2 $marcrec->insertpos($subfield,$usub,$rvictim);
2369 2 $marcrec->field_updatehook($rvictim);
2370 2 ▶ $marcrec->rebuild_map($field) if $do_rebuild_map;
Branch: 1 branch
BranchTrueFalseTotal
if $do_rebuild_map 0 2 2
2371 2 return 1; # $rvictim is now defined, so can't depend on future
2372 # control logic.
2373 }
2374 #The general replace case.
2375 1 ▶ if ($rvictim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rvictim) 1 0 1
2376 1 $$rval = $usub;
2377 1 $marcrec->field_updatehook($rvictim);
2378  
2379 # The following line is unecessary for this class:
2380 # everything updates due to hard-coded ref
2381 # relationships in the index. Left so that subclasses
2382 # can do their thing with less over-ruling.
2383  
2384 1 ▶ $marcrec->rebuild_map($field) if $do_rebuild_map;
Branch: 1 branch
BranchTrueFalseTotal
if $do_rebuild_map 0 1 1
2385 1 return 1;
2386 }
2387 } #end $field and $subfield
2388 }
2389  
2390 ####################################################################
2391  
2392 # updatefields() takes a template which specifies a
2393 # $do_rebuild_map and a field (needs the field in case $rafields->[0]
2394 # is empty). It also takes a ref to an array of fieldrefs formatted
2395 # like the output of getfields(), and replaces/creates the field
2396 # data. It assumes that it should remove the fields with the first tag
2397 # in the fieldrefs. It calls rebuild_map() if $do_rebuild_map.
2398  
2399 ####################################################################
2400 103 ▶sub updatefields { # rec
Subroutine updatefields: called CC=13 SCAR=25.7
2401 103 ▶ my $marcrec = shift || return;
Condition50%1/2
shift() || (return)
l!l
1030
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2402 103 my $template = shift;
2403  
2404 103 my $do_rebuild_map = $template->{'rebuild_map'};
2405 103 my $tag = $template->{'field'};
2406 103 my $rafieldrefs = shift;
2407 103 my @fieldrefs = @$rafieldrefs;
2408  
2409  
2410 103 my $pos = 0;
2411 103 my $first=undef;
2412 103 my $last = $first; # Should be "Let the first be last". Misbegotten Perl syntax.
2413 103 my $firstpast = undef;
2414 103 my $len = 0;
2415 103 my @mfields = @{$marcrec->{'array'}};
2416 103 my $insertpos = undef;
2417 103 for (@mfields) {
2418 2182 ▶ $first = $pos if ($_->[0] eq $tag and !defined($first)) ;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq $tag and not defined $first 101 2081 2182
Condition100%3/3
$_->[0] eq $tag and not defined $first
!ll&&!rl&&r
206219101
MC/DC100%2/2
$_->[0] eq $tag and not defined $first
$_->[0] eq $tag not defined $first
Truth table100%3/3
A and not(B)
A= $_->[0] eq $tagB= not defined $first
ABresult
0X0
100
111
2419 2182 ▶ $last = $pos if $_->[0] eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq $tag 120 2062 2182
2420 2182 ▶ $firstpast = $pos if ($_->[0] >= $tag and !defined($firstpast)) ;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] >= $tag and not defined $firstpast 103 2079 2182
Condition100%3/3
$_->[0] >= $tag and not defined $firstpast
!ll&&!rl&&r
4271652103
MC/DC100%2/2
$_->[0] >= $tag and not defined $firstpast
$_->[0] >= $tag not defined $firstpast
Truth table100%3/3
A and not(B)
A= $_->[0] >= $tagB= not defined $firstpast
ABresult
0X0
100
111
2421 2182 $pos++;
2422 }
2423 103 ▶ $len = $last - $first +1 if defined($first);
Branch: 1 branch
BranchTrueFalseTotal
if defined $first 101 2 103
2424 103 ▶ $insertpos = scalar(@mfields) if !defined($firstpast);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $firstpast 0 103 103
2425 103 ▶ $insertpos = $first if (defined($first));
Branch: 1 branch
BranchTrueFalseTotal
if defined $first 101 2 103
2426 103 ▶ $insertpos = $firstpast unless $insertpos;
Branch: 1 branch
BranchTrueFalseTotal
unless $insertpos 2 101 103
2427 103 splice @{$marcrec->{'array'}},$insertpos,$len,@fieldrefs;
2428 103 ▶ $marcrec->rebuild_map($tag) if $do_rebuild_map;
Branch: 1 branch
BranchTrueFalseTotal
if $do_rebuild_map 0 103 103
2429 }
2430  
2431 ####################################################################
2432 # output() will call the appropriate output method using the marc #
2433 # object and desired format parameters. #
2434 ####################################################################
2435 0 ▶sub output {
Subroutine output: not called CC=22 SCAR=62.3
2436 0 my $marcrec=shift;
2437 0 my $args=shift;
2438 0 my $output = "";
2439 0 ▶ my $newline = $args->{'lineterm'} || "\n";
Condition0%0/2
$args->{'lineterm'} || "\n"
l!l
00
MC/DC0%0/1
$args->{'lineterm'} || "\n"
$args->{'lineterm'}
Truth table0%0/2
A
A= $args->{'lineterm'}
Aresult
11
00
2440  
2441 0 ▶ $marcrec->add_005($args) if ($args->{'file'} or $args->{'add_005s'});
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"file"} or $args->{"add_005s"} 0 0 0
Condition0%0/3
$args->{"file"} or $args->{"add_005s"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"file"} or $args->{"add_005s"}
$args->{"file"} $args->{"add_005s"}
Truth table0%0/3
A or B
A= $args->{"file"}B= $args->{"add_005s"}
ABresult
1X1
011
000
2442  
2443 0 ▶ unless (exists $args->{'format'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"format"}) 0 0 0
2444 # everything to string
2445 0 $args->{'format'} = "usmarc";
2446 0 $args->{'lineterm'} = $newline;
2447 }
2448 0 ▶ if ($args->{'format'} =~ /marc$/oi) {
Branch: 11 branches
BranchTrueFalseTotal
if ($args->{'format'} =~ /marc$/io) { } 0 0 0
elsif ($args->{'format'} =~ /marcmaker$/io) { } 0 0 0
elsif ($args->{'format'} =~ /ascii$/io) { } 0 0 0
elsif ($args->{'format'} =~ /html$/io) { } 0 0 0
elsif ($args->{'format'} =~ /html_header$/io) { } 0 0 0
elsif ($args->{'format'} =~ /html_start$/io) { } 0 0 0
elsif ($args->{'format'} =~ /html_body$/io) { } 0 0 0
elsif ($args->{'format'} =~ /html_footer$/io) { } 0 0 0
elsif ($args->{'format'} =~ /urls$/io) { } 0 0 0
elsif ($args->{'format'} =~ /isbd$/io) { } 0 0 0
elsif ($args->{'format'} =~ /xml/io) { } 0 0 0
2449 0 $output = _writemarc($marcrec,$args);
2450 }
2451 elsif ($args->{'format'} =~ /marcmaker$/oi) {
2452 0 $output = _marcmaker($marcrec,$args);
2453 }
2454 elsif ($args->{'format'} =~ /ascii$/oi) {
2455 0 $output = _marc2ascii($marcrec,$args);
2456 }
2457 elsif ($args->{'format'} =~ /html$/oi) {
2458 0 $output .= _marc2html($marcrec,$args);
2459 }
2460 elsif ($args->{'format'} =~ /html_header$/oi) {
2461 0 $output = "Content-type: text/html\015\012\015\012";
2462 }
2463 elsif ($args->{'format'} =~ /html_start$/oi) {
2464 0 ▶ if ($args->{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'title'}) { } 0 0 0
2465 0 $output = "<html><head><title>$args->{'title'}</title></head>";
2466 0 $output .= "$newline<body>";
2467 }
2468 else {
2469 0 $output = "<html><body>";
2470 }
2471 }
2472 elsif ($args->{'format'} =~ /html_body$/oi) {
2473 0 $output =_marc2html($marcrec,$args);
2474 }
2475 elsif ($args->{'format'} =~ /html_footer$/oi) {
2476 0 $output = "$newline</body></html>$newline";
2477 }
2478 elsif ($args->{'format'} =~ /urls$/oi) {
2479 0 $output .= _urls($marcrec,$args);
2480 }
2481 elsif ($args->{'format'} =~ /isbd$/oi) {
2482 0 $output = _isbd($marcrec,$args);
2483 }
2484 elsif ($args->{'format'} =~ /xml/oi) {
2485 0 ▶ mycarp "XML formats are now handled by MARC::XML" if ($^W);
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
2486 0 return;
2487 }
2488 0 ▶ if ($args->{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'file'}) { } 0 0 0
2489 0 ▶ if ($args->{'file'} !~ /^>/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"file"} =~ /^>/) 0 0 0
2490 0 mycarp "Don't forget to use > or >> with output file name";
2491 0 return;
2492 }
2493 0 ▶ open (OUT, $args->{file}) || mycarp "Couldn't open file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, $args->{"file"} or mycarp("Couldn't open file: $!") 0 0 0
2494 #above quote is bad if {file} is tainted. Is probably unecessary.dgl.
2495 0 binmode OUT;
2496 0 print OUT $output;
2497 0 ▶ close OUT || mycarp "Couldn't close file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close OUT or mycarp("Couldn't close file: $!") 0 0 0
2498 0 return 1;
2499 }
2500 #if no filename was specified return the output so it can be grabbed
2501 else {
2502 0 return $output;
2503 }
2504 }
2505  
2506 ####################################################################
2507  
2508 # add_005s takes a template and adds current 005s to the elements of
2509 # $marc mentioned in $template->{records}
2510  
2511 ####################################################################
2512 86 ▶sub add_005 {
Subroutine add_005: called CC=1 SCAR=0.0
2513 86 my $marcrec=shift;
2514 86 my $time = shift;
2515 86 my @m005 = ('005', $time );
2516 86 $marcrec->updatefirst({field=>'005'},@m005);
2517 }
2518  
2519 ##############################################################
2520 163 ▶sub _joinfield { # rec
Subroutine _joinfield: called CC=6 SCAR=17.9
2521 163 my $marcrec=shift;
2522 163 my ($rfield,$field,$delim)=@_;
2523 163 my $result;
2524 163 ▶ return $rfield->[1] if $field<10;
Branch: 1 branch
BranchTrueFalseTotal
if $field < 10 38 125 163
2525  
2526 125 ▶ if ($delim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delim) 2 123 125
2527 2 ▶ foreach (my $i=3; $i<$#$rfield; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rfield->$#*) 3 2 5
2528 3 $result.=$delim.$rfield->[$i].$rfield->[$i+1];
2529 }
2530 2 return $result;
2531 }
2532  
2533 123 ▶ for (my $i=4; $i<=$#$rfield; $i=$i+2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $rfield->$#*) 225 123 348
2534 225 $result.=$rfield->[$i];
2535 225 ▶ $result.=" " unless $result=~/ $/;
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ / $/ 225 0 225
2536 }
2537 123 return $result;
2538 }
2539  
2540 ####################################################################
2541  
2542 # getmatch() takes a subfield code (can be an indicator) and a fieldref
2543 # Returns 0 or a ref to the value to be updated.
2544  
2545 ####################################################################
2546 9 ▶sub getmatch { # rec
Subroutine getmatch: called CC=7 SCAR=20.5
2547 9 ▶ my $marcrec = shift || return;
Condition50%1/2
shift() || (return)
l!l
90
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2548 9 my $subf = shift;
2549 9 my $rfield = shift;
2550 9 my $tag = $rfield->[0];
2551 0 ▶ if ($tag < 10) {mycarp "can't find subfields or indicators for control fields"; return undef}
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) 0 9 9
2552 9 ▶ return \$rfield->[1] if $subf eq 'i1';
Branch: 1 branch
BranchTrueFalseTotal
if $subf eq "i1" 0 9 9
2553 9 ▶ return \$rfield->[2] if $subf eq 'i2';
Branch: 1 branch
BranchTrueFalseTotal
if $subf eq "i2" 0 9 9
2554  
2555 9 ▶ for (my $i=3;$i<@$rfield;$i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rfield) 11 7 18
2556 11 ▶ return \$rfield->[$i+1] if $rfield->[$i] eq $subf;
Branch: 1 branch
BranchTrueFalseTotal
if $rfield->[$i] eq $subf 2 9 11
2557 }
2558 7 return 0;
2559 }
2560  
2561 ####################################################################
2562  
2563 # deletesubfield() takes a subfield code (can not be an indicator) and a
2564 # fieldref. Deletes the subfield code and its value in the fieldref at
2565 # the first match on subfield code. Assumes there is an exact
2566 # subfield match in $fieldref.
2567  
2568 ####################################################################
2569 1 ▶sub deletesubfield { # rec
Subroutine deletesubfield: called CC=6 SCAR=19.9
2570 1 ▶ my $marcrec = shift || return;
Condition50%1/2
shift() || (return)
l!l
10
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2571 1 my $subf = shift;
2572 1 my $rfield = shift;
2573 1 my $tag = $rfield->[0];
2574 0 ▶ if ($tag < 10) {mycarp "Can't use subfields or indicators for control fields"; return undef}
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) 0 1 1
2575  
2576 0 ▶ if ($subf =~/i[12]/) {mycarp "Can't delete an indicator."; return undef}
Branch: 1 branch
BranchTrueFalseTotal
if ($subf =~ /i[12]/) 0 1 1
2577 1 my $i=3;
2578 1 ▶ for ($i=3;$i<@$rfield;$i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rfield) 2 0 2
2579 2 ▶ last if $rfield->[$i] eq $subf;
Branch: 1 branch
BranchTrueFalseTotal
if $rfield->[$i] eq $subf 1 1 2
2580 }
2581 1 splice @$rfield,$i,2;
2582
2583 }
2584  
2585 ####################################################################
2586  
2587 # insertpos() takes a subfield code (can not be an indicator), a
2588 # value, and a fieldref. Updates the fieldref with the first
2589 # place that the fieldref can match. Assumes there is no exact
2590 # subfield match in $fieldref.
2591  
2592 ####################################################################
2593 2 ▶sub insertpos { # rec
Subroutine insertpos: called CC=6 SCAR=19.7
2594 2 ▶ my $marcrec = shift || return;
Condition50%1/2
shift() || (return)
l!l
20
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2595 2 my $subf = shift;
2596 2 my $value = shift;
2597 2 my $rfield = shift;
2598 2 my $tag = $rfield->[0];
2599 0 ▶ if ($tag < 10) {mycarp "Can't use subfields or indicators for control fields"; return undef}
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) 0 2 2
2600  
2601 0 ▶ if ($subf =~/i[12]/) {mycarp "Can't insert past an indicator."; return undef}
Branch: 1 branch
BranchTrueFalseTotal
if ($subf =~ /i[12]/) 0 2 2
2602 2 my $i=3;
2603 2 ▶ for ($i=3;$i<@$rfield;$i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rfield) 3 2 5
2604 3 ▶ last if $rfield->[$i] gt $subf;
Branch: 1 branch
BranchTrueFalseTotal
if $rfield->[$i] gt $subf 0 3 3
2605 }
2606 2 splice @$rfield,$i,0,$subf,$value;
2607 }
2608  
2609 ####################################################################
2610  
2611 # getfirstvalue() will return the first value of a field or subfield
2612 # or indicator or i12 in a particular record found in the MARC
2613 # object. It does not depend on the index being up to date.
2614  
2615 ####################################################################
2616 2 ▶sub getfirstvalue { # rec
Subroutine getfirstvalue: called CC=15 SCAR=41.8
2617 2 my $marcrec= shift;
2618 2 my $template=shift;
2619 2 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 2 2
2620 2 my $field = $template->{'field'};
2621 2 my $delim = $template->{'delimiter'};
2622 2 my $subfield;
2623 2 ▶ $subfield = $template->{'subfield'} if $template->{'subfield'};
Branch: 1 branch
BranchTrueFalseTotal
if $template->{"subfield"} 0 2 2
2624
2625 0 ▶ if (not($field)) {mycarp "You must specify a field"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 2 2
2626 0 ▶ unless ($field =~ /^\d{3}$/) {mycarp "Invalid field specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field =~ /^\d{3}$/) 0 2 2
2627 2 my @fieldrefs = grep {$_->[0] eq $field} @{$marcrec->{'array'}};
2628 2 ▶ return unless @fieldrefs;
Branch: 1 branch
BranchTrueFalseTotal
unless @fieldrefs 0 2 2
2629 2 ▶ if ($field and not $subfield) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field and not $subfield) { } 2 0 2
elsif ($field and $subfield) { } 0 0 0
Condition33%1/3
$field and not $subfield
!ll&&!rl&&r
002
MC/DC0%0/2
$field and not $subfield
$field not $subfield
Truth table33%1/3
A and not(B)
A= $fieldB= not $subfield
ABresult
0X0
100
111
2630 2 return $marcrec->_joinfield($fieldrefs[0],$field,$delim);
2631 ▶ } elsif ($field and $subfield) {
Condition0%0/3
$field and $subfield
!ll&&!rl&&r
000
MC/DC0%0/2
$field and $subfield
$field $subfield
Truth table0%0/3
A and B
A= $fieldB= $subfield
ABresult
0X0
100
111
2632 0 ▶ if ($field <10) {mycarp "There are no subfields or indicators for control fields";return}
Branch: 1 branch
BranchTrueFalseTotal
if ($field < 10) 0 0 0
2633 0 ▶ return $fieldrefs[0][1].$fieldrefs[0][2] if $subfield eq 'i12';
Branch: 1 branch
BranchTrueFalseTotal
if $subfield eq "i12" 0 0 0
2634 0 my $rsubf = undef;
2635 0 foreach my $fieldref (@fieldrefs) {
2636 0 $rsubf =$marcrec->getmatch($subfield,$fieldref);
2637 0 ▶ return $$rsubf if $rsubf;
Branch: 1 branch
BranchTrueFalseTotal
if $rsubf 0 0 0
2638 }
2639 0 ▶ return undef unless $rsubf;
Branch: 1 branch
BranchTrueFalseTotal
unless $rsubf 0 0 0
2640 }
2641 }
2642 ####################################################################
2643 # getvalue() will return the value of a field or subfield in a #
2644 # particular record found in the MARC object #
2645 ####################################################################
2646 63 ▶sub getvalue { # rec
Subroutine getvalue: called CC=20 SCAR=30.8
2647 63 my $marcrec = shift;
2648 63 my $template=shift;
2649 63 ▶ return unless (ref($template) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template eq "HASH" 0 63 63
2650 63 my $params = _params($template,@_);
2651  
2652 63 my $field = $params->{field};
2653 0 ▶ if (not($field)) {mycarp "You must specify a field"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 63 63
2654 0 ▶ unless ($field =~ /^\d{3}$/) {mycarp "Invalid field specified"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($field =~ /^\d{3}$/) 0 63 63
2655 63 my $subfield = $params->{subfield};
2656 63 my $delim = $params->{delimiter};
2657 63 my @values;
2658 63 ▶ if ($field and not($subfield)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field and not $subfield) { } 25 38 63
elsif ($field and $subfield) { } 38 0 38
Condition66%2/3
$field and not $subfield
!ll&&!rl&&r
03825
MC/DC50%1/2
$field and not $subfield
$field not $subfield
Truth table66%2/3
A and not(B)
A= $fieldB= not $subfield
ABresult
0X0
100
111
2659 25 ▶ return unless exists $marcrec->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marcrec->{$field} 5 20 25
2660 20 ▶ if ($field eq '000') { return $marcrec->{'000'}[1] };
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "000") 3 17 20
2661 17 foreach my $rfield (@{$marcrec->{$field}{field}}) {
2662 17 push @values,
2663 $marcrec->_joinfield($rfield,$field,$delim);
2664 }
2665 17 return @values;
2666 }
2667 ▶ elsif ($field and $subfield) {
Condition33%1/3
$field and $subfield
!ll&&!rl&&r
0038
MC/DC0%0/2
$field and $subfield
$field $subfield
Truth table33%1/3
A and B
A= $fieldB= $subfield
ABresult
0X0
100
111
2668 38 ▶ return unless exists $marcrec->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marcrec->{$field} 0 38 38
2669 38 ▶ return unless exists $marcrec->{$field}{$subfield};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marcrec->{$field}{$subfield} 4 34 38
2670 34 ▶ if ($subfield eq "i1" || $subfield eq "i2" || $subfield eq "i12") {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfield eq "i1" or $subfield eq "i2" or $subfield eq "i12") 15 19 34
Condition100%6/6
$subfield eq "i1" or $subfield eq "i2" or $subfield eq "i12"
l!l&&r!l&&!r
10519
$subfield eq "i1" or $subfield eq "i2"
l!l&&r!l&&!r
5524
MC/DC100%3/3
$subfield eq "i1" or $subfield eq "i2" or $subfield eq "i12"
$subfield eq "i1" $subfield eq "i2" $subfield eq "i12"
Truth table100%4/4
A or B or C
A= $subfield eq "i1"B= $subfield eq "i2"C= $subfield eq "i12"
ABCresult
1XX1
01X1
0011
0000
2671 15 my @shortone = @{$marcrec->{$field}{field}};
2672 15 foreach my $rfield (@shortone) {
2673 19 ▶ if ($subfield eq 'i1') {
Branch: 2 branches
BranchTrueFalseTotal
if ($subfield eq 'i1') { } 5 14 19
elsif ($subfield eq 'i2') { } 5 9 14
2674 5 push @values, $rfield->[1];
2675 }
2676 elsif ($subfield eq 'i2') {
2677 5 push @values, $rfield->[2];
2678 }
2679 else {
2680 9 push @values, $rfield->[1].$rfield->[2];
2681 }
2682 }
2683 15 return @values;
2684 }
2685 19 foreach my $rval (@{$marcrec->{$field}{$subfield}}) {
2686 27 push @values, $$rval;
2687 }
2688 19 return @values;
2689 }
2690 }
2691  
2692 ####################################################################
2693 #Returns LDR at $record. #
2694 ####################################################################
2695 9 ▶sub ldr { # rec
Subroutine ldr: called CC=1 SCAR=0.0
2696 9 my $marcrec = shift;
2697 9 return $marcrec->{array}[0][1];
2698 }
2699  
2700  
2701 ####################################################################
2702 #Takes a record number and returns a hash of fields. #
2703 #Needed to determine the format (BOOK, VIS, etc) of #
2704 #the record. #
2705 #Folk also like to know what Ctrl, Desc etc are. #
2706 ####################################################################
2707 2 ▶sub unpack_ldr { # rec
Subroutine unpack_ldr: called CC=1 SCAR=0.0
2708 2 my $marcrec = shift;
2709  
2710 2 my $ldr = $marcrec->ldr();
2711 2 my $rhldr = $marcrec->_unpack_ldr($ldr);
2712 2 $marcrec->{unp_ldr}=$rhldr;
2713 2 return $rhldr;
2714 }
2715  
2716
2717 7 ▶sub _unpack_ldr { # rec
Subroutine _unpack_ldr: called CC=2 SCAR=6.9
2718 7 my ($marcrec,$ldr) = @_;
2719  
2720 7 my %ans=();
2721  
2722 7 my @fields=unpack($LDR_TEMPLATE,$ldr);
2723 7 for (@LDR_FIELDS) {
2724 98 $ans{$_}=shift @fields;
2725 }
2726 7 return \%ans;
2727 }
2728  
2729  
2730 ####################################################################
2731 #Takes a record number. #
2732 #Returns the unpacked ldr as a ref to hash from the ref in $self. #
2733 #Does not overwrite hash from ldr. #
2734 ####################################################################
2735 0 ▶sub get_hash_ldr { # rec
Subroutine get_hash_ldr: not called CC=2 SCAR=17.9
2736 0 my $marcrec = shift;
2737 0 ▶ return undef unless exists($marcrec->{unp_ldr});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marcrec->{"unp_ldr"} 0 0 0
2738 0 return $marcrec->{unp_ldr};
2739 }
2740  
2741 ####################################################################
2742 # Takes a record number and updates the corresponding ldr if there
2743 # is a hashed form. Returns undef unless there is a hash. Else
2744 # returns $ldr.
2745 ####################################################################
2746 4 ▶sub pack_ldr { # rec
Subroutine pack_ldr: called CC=2 SCAR=7.0
2747 4 my $marcrec = shift;
2748 4 ▶ return undef unless exists($marcrec->{unp_ldr});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marcrec->{"unp_ldr"} 0 4 4
2749 4 my $rhldr = $marcrec->{unp_ldr};
2750 4 my $ldr = $marcrec -> _pack_ldr($rhldr);
2751 4 $marcrec->{array}[0][1] = $ldr;
2752 4 return $ldr;
2753 }
2754  
2755 ####################################################################
2756 #Takes a ref to hash version of the LDR and returns a string #
2757 # version #
2758 ####################################################################
2759 8 ▶sub _pack_ldr { # rec
Subroutine _pack_ldr: called CC=2 SCAR=6.9
2760  
2761 8 my ($marcrec,$rhldr) = @_;
2762 8 my @fields=();
2763  
2764 8 for (@LDR_FIELDS) {
2765 112 push @fields,$rhldr->{$_};
2766 }
2767 8 my $ans = pack($LDR_TEMPLATE,@fields);
2768 8 return $ans;
2769 }
2770  
2771 ####################################################################
2772 #Takes a string record number. #
2773 #Returns a the format necessary to pack/unpack 008 fields correctly#
2774 ####################################################################
2775 2 ▶sub bib_format { # rec
Subroutine bib_format: called CC=1 SCAR=0.0
2776 2 my ($marcrec)=@_;
2777 2 $marcrec->pack_ldr();
2778 2 my $ldr = $marcrec->ldr();
2779 2 return $marcrec->_bib_format($ldr);
2780 }
2781  
2782 5 ▶sub _bib_format { # rec
Subroutine _bib_format: called CC=15 SCAR=46.6
2783 5 my ($marcrec,$ldr)=@_;
2784 5 my $rldr=$marcrec->_unpack_ldr($ldr);
2785 5 my ($type,$bib_lvl) = ($rldr->{'Type'},$rldr->{'BLvl'});
2786 5 ▶ return "UNKNOWN (Type $type Bib_Lvl $bib_lvl)" unless ($type=~/[abcdefgijkmprot]/ &&
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /[abcdefgijkmprot]/ and $bib_lvl eq "" || $bib_lvl =~ /[abcdms]/ 0 5 5
Condition33%2/6
$type =~ /[abcdefgijkmprot]/ and $bib_lvl eq "" || $bib_lvl =~ /[abcdms]/
!ll&&!rl&&r
005
$bib_lvl eq "" or $bib_lvl =~ /[abcdms]/
l!l&&r!l&&!r
050
MC/DC0%0/3
$type =~ /[abcdefgijkmprot]/ and $bib_lvl eq "" || $bib_lvl =~ /[abcdms]/
$type =~ /[abcdefgijkmprot]/ $bib_lvl eq "" $bib_lvl =~ /[abcdms]/
Truth table25%1/4
A and B or C
A= $type =~ /[abcdefgijkmprot]/B= $bib_lvl eq ""C= $bib_lvl =~ /[abcdms]/
ABCresult
0XX0
1000
11X1
1011
2787 (($bib_lvl eq "") or
2788 $bib_lvl=~/[abcdms]/)
2789 );
2790  
2791 5 ▶ return "BOOKS" if (
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "a" and not $bib_lvl =~ /[bs]/ or $type eq "t" or $type eq "b" 5 0 5
Condition33%3/9
$type eq "a" and not $bib_lvl =~ /[bs]/ or $type eq "t" or $type eq "b"
l!l&&r!l&&!r
500
$type eq "a" and not $bib_lvl =~ /[bs]/ or $type eq "t"
l!l&&r!l&&!r
500
$type eq "a" and not $bib_lvl =~ /[bs]/
!ll&&!rl&&r
005
MC/DC0%0/4
$type eq "a" and not $bib_lvl =~ /[bs]/ or $type eq "t" or $type eq "b"
$type eq "a" not $bib_lvl =~ /[bs]/ $type eq "t" $type eq "b"
Truth table14%1/7
A and not(B) or C or D
A= $type eq "a"B= not $bib_lvl =~ /[bs]/C= $type eq "t"D= $type eq "b"
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
2792 (
2793 ($type eq "a") && !($bib_lvl =~/[bs]/)
2794 )
2795 or $type eq "t" or $type eq "b"
2796 ); #$type b is obsolete, 'tho.
2797 0 ▶ return "SERIALS" if (
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "a" and $bib_lvl =~ /[bs]/ 0 0 0
Condition0%0/3
$type eq "a" and $bib_lvl =~ /[bs]/
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "a" and $bib_lvl =~ /[bs]/
$type eq "a" $bib_lvl =~ /[bs]/
Truth table0%0/3
A and B
A= $type eq "a"B= $bib_lvl =~ /[bs]/
ABresult
0X0
100
111
2798 ($type eq "a") &&
2799 ($bib_lvl =~/[bs]/)
2800 );
2801 0 ▶ return "COMPUTER_FILES" if ($type =~/m/);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /m/ 0 0 0
2802 0 ▶ return "MAPS" if ($type =~/[ef]/);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /[ef]/ 0 0 0
2803 0 ▶ return "MUSIC" if ($type =~/[cdij]/);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /[cdij]/ 0 0 0
2804 0 ▶ return "VIS" if ($type =~/[gkro]/);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /[gkro]/ 0 0 0
2805 0 ▶ return "MIX" if ($type =~/p/);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /p/ 0 0 0
2806 0 return "UNKNOWN (Type $type Bib_Lvl $bib_lvl) ??"; # Shouldn't happen
2807 }
2808  
2809 ####################################################################
2810 #Takes a record number. #
2811 #Returns the unpacked 008 as a ref to hash. Installs ref in $self. #
2812 ####################################################################
2813 2 ▶sub unpack_008 { # rec
Subroutine unpack_008: called CC=1 SCAR=0.0
2814 2 my ($marcrec) = @_;
2815 2 my ($ff_string) = $marcrec->getfirstvalue({field=>'008'});
2816 2 my $bib_format = $marcrec->bib_format();
2817 2 my $rh008= $marcrec->_unpack_008($ff_string,$bib_format);
2818 2 $marcrec->{unp_008}=$rh008;
2819 2 return $rh008;
2820 }
2821  
2822 2 ▶sub _unpack_008 { # rec
Subroutine _unpack_008: called CC=3 SCAR=11.3
2823 2 my ($marcrec,$ff_string,$bib_format) = @_;
2824 2 my %ans=();
2825  
2826 2 my $ff_templ=$FF_TEMPLATE{$bib_format};
2827 2 my $raff_fields=$FF_FIELDS{$bib_format};
2828 2 ▶ if ($bib_format =~/UNKNOWN/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bib_format =~ /UNKNOWN/) 0 2 2
2829 0 mycarp "Format is $bib_format";
2830 0 return;
2831 }
2832 2 my @fields=unpack($ff_templ,$ff_string);
2833 2 for (@{$raff_fields}) {
2834 38 $ans{$_}=shift @fields;
2835 }
2836 2 return \%ans;
2837 }
2838  
2839 ####################################################################
2840 #Takes a record number. #
2841 #Returns the unpacked 008 as a ref to hash from the ref in $self. #
2842 #Does not overwrite hash from 008 field. #
2843 ####################################################################
2844 1 ▶sub get_hash_008 { # rec
Subroutine get_hash_008: called CC=2 SCAR=7.1
2845 1 my ($marcrec)=@_;
2846 1 ▶ return undef unless exists($marcrec->{unp_008});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $marcrec->{"unp_008"} 0 1 1
2847 1 return $marcrec->{unp_008};
2848 }
2849  
2850 ####################################################################
2851 #Takes a record number. Flushes hashes to 008 and ldr. #
2852 #Updates the 008 field from an installed fixed field hash.
2853 #Returns undef unless there is a hash, else returns the 008 field #
2854 ####################################################################
2855 1 ▶sub pack_008 { # rec
Subroutine pack_008: called CC=2 SCAR=7.0
2856 1 my ($marcrec) = @_;
2857 1 $marcrec->pack_ldr();
2858 1 my $ldr = $marcrec->ldr();
2859 1 my $rhff = $marcrec->get_hash_008();
2860 1 ▶ return undef unless $rhff;
Branch: 1 branch
BranchTrueFalseTotal
unless $rhff 0 1 1
2861 1 my $ff_string = $marcrec->_pack_008($ldr,$rhff);
2862 1 $marcrec->updatefirst({field=>'008'},$ff_string);
2863 1 return $ff_string;
2864 }
2865  
2866 ####################################################################
2867 #Takes LDR and ref to hash of unpacked 008 #
2868 #Returns string version of 008 *without* newlines. #
2869 ####################################################################
2870 3 ▶sub _pack_008 { # rec
Subroutine _pack_008: called CC=2 SCAR=6.9
2871 3 my ($marcrec,$ldr,$rhff) = @_;
2872 3 my $bib_format = $marcrec->_bib_format($ldr);
2873 3 my $ans = "";
2874 3 my @fields = ();
2875 3 for (@{$FF_FIELDS{$bib_format}}) {
2876 57 push @fields, $rhff->{$_};
2877 }
2878 3 $ans = pack($FF_TEMPLATE{$bib_format},@fields);
2879 3 return $ans;
2880 }
2881  
2882 ####################################################################
2883  
2884 # as_string returns a newline-\c^ separated version of the record.
2885 # Subclasses may need to override this. If so, to make Tie happy,
2886 # they should override from_string. 000 is ldr.
2887  
2888 ####################################################################
2889  
2890 1 ▶sub as_string {
Subroutine as_string: called CC=4 SCAR=13.9
2891 1 my $marcrec=shift;
2892 1 my $SEP = "\cJ"; #unix newline
2893 1 my $ans = "";
2894 1 for (@{$marcrec->{'array'}}) {
2895 21 my $tag = $_->[0];
2896 21 ▶ if ($tag < 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) 5 16 21
2897 5 $ans .= "$tag $_->[1]$SEP";
2898 5 next;
2899 }
2900 16 $ans .= "$tag $_->[1]$_->[2] ";
2901 16 ▶ foreach (my $i=3; $i<$#$_; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $_->$#*) 32 16 48
2902 32 $ans .="\c_$_->[$i]$_->[$i+1]";
2903 }
2904 16 $ans .=$SEP;
2905 }
2906 1 return $ans;
2907 }
2908  
2909 ####################################################################
2910  
2911 # from_string takes a newline-\c^ separated version of the record
2912 # and replaces the {array} information from that information.
2913 # Subclasses may need to override this. If so, to make Tie happy,
2914 # they should override as_string. 000 is ldr.
2915  
2916 ####################################################################
2917 1 ▶sub from_string {
Subroutine from_string: called CC=6 SCAR=18.0
2918 1 my $marcrec=shift;
2919 1 my $string = shift;
2920 1 my $do_rebuild_map = shift;
2921 1 my $SEP = "\cJ"; #unix newline
2922 1 my @lines = split /$SEP/,$string;
2923 1 @{$marcrec->{'array'}}=();
2924 1 for (@lines) {
2925 21 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 21 21
2926 21 my $tag = substr($_,0,3);
2927 21 ▶ if ($tag < 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag < 10) 5 16 21
2928 5 my $contents = substr($_,4);
2929 5 push @{$marcrec->{'array'}}, [$tag, $contents];
2930 5 next;
2931 }
2932 16 my ($i1,$i2,$sub_string) = (substr($_,4,1),substr($_,5,1),substr($_,7));
2933 16 my @field = ($tag,$i1,$i2);
2934 16 my @subfields = split /\c_(.)/,$sub_string;
2935 16 ▶ shift @subfields if $subfields[0] eq ''; # feature of split.
Branch: 1 branch
BranchTrueFalseTotal
if $subfields[0] eq "" 16 0 16
2936 16 push @field,@subfields;
2937 16 push @{$marcrec->{'array'}}, [@field];
2938 }
2939 1 ▶ $marcrec->rebuild_map_all() if $do_rebuild_map;
Branch: 1 branch
BranchTrueFalseTotal
if $do_rebuild_map 0 1 1
2940 }
2941  
2942 5 1; # so the require or use succeeds
2943  
2944 __END__
↑ Summary