File/USMARC.pm

filter: Statementstmt 92.6% 114 / 123 Branchbran 67.1% 43 / 64 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 93.3% 14 / 15 Podpod 100% 3 / 3 totaltotal 81.3% 183 / 225 CC 38 SCAR 31.6
CC
38
Cov
82%
decode
38.8
_build_tag_directory
6.3
encode
3.0
CRAP 23.6
↑ Summary File/MicroLIF.pm »
1 package MARC::File::USMARC;
2  
3 =head1 NAME
4  
5 MARC::File::USMARC - USMARC-specific file handling
6  
7 =cut
8  
9 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 27 ▶use vars qw( $ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 27 ▶use MARC::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use vars qw( @ISA ); @ISA = qw( MARC::File );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ## conditionally use this module for doing byte oriented
18 ## substr() and length() on utf8 data.
19 27 ▶eval( "use MARC::File::Utils" ) if MARC::File::utf8_safe();
Branch: 1 branch
BranchTrueFalseTotal
if MARC::File::utf8_safe() 0 27 27
20  
21 27 ▶use MARC::Record qw( LEADER_LEN );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 27 ▶use constant SUBFIELD_INDICATOR => "\x1F";
Subroutine BEGIN: called CC=1 SCAR=0.0
23 27 ▶use constant END_OF_FIELD => "\x1E";
Subroutine BEGIN: called CC=1 SCAR=0.0
24 27 ▶use constant END_OF_RECORD => "\x1D";
Subroutine BEGIN: called CC=1 SCAR=0.0
25 27 ▶use constant DIRECTORY_ENTRY_LEN => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =head1 SYNOPSIS
28  
29 use MARC::File::USMARC;
30  
31 my $file = MARC::File::USMARC->in( $filename );
32  
33 while ( my $marc = $file->next() ) {
34 # Do something
35 }
36 $file->close();
37 undef $file;
38  
39 =head1 EXPORT
40  
41 None.
42  
43 =head1 METHODS
44  
45 =cut
46  
47 sub _next {
48 157 ▶ my $self = shift;
Subroutine _next: called CC=2 SCAR=6.9
49 157 my $fh = $self->{fh};
50  
51 157 my $reclen;
52 157 ▶ return if eof($fh);
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 11 146 157
53  
54 146 local $/ = END_OF_RECORD;
55 146 my $usmarc = <$fh>;
56  
57 # remove illegal garbage that sometimes occurs between records
58 146 $usmarc =~ s/^[ \x00\x0a\x0d]+//;
59  
60 146 return $usmarc;
61 }
62  
63 =head2 decode( $string [, \&filter_func ] )
64  
65 Constructor for handling data from a USMARC file. This function takes care of
66 all the tag directory parsing & mangling.
67  
68 Any warnings or coercions can be checked in the C<warnings()> function.
69  
70 The C<$filter_func> is an optional reference to a user-supplied function
71 that determines on a tag-by-tag basis if you want the tag passed to it
72 to be put into the MARC record. The function is passed the tag number
73 and the raw tag data, and must return a boolean. The return of a true
74 value tells MARC::File::USMARC::decode that the tag should get put into
75 the resulting MARC record.
76  
77 For example, if you only want title and subject tags in your MARC record,
78 try this:
79  
80 sub filter {
81 my ($tagno,$tagdata) = @_;
82  
83 return ($tagno == 245) || ($tagno >= 600 && $tagno <= 699);
84 }
85  
86 my $marc = MARC::File::USMARC->decode( $string, \&filter );
87  
88 Why would you want to do such a thing? The big reason is that creating
89 fields is processor-intensive, and if your program is doing read-only
90 data analysis and needs to be as fast as possible, you can save time by
91 not creating fields that you'll be ignoring anyway.
92  
93 Another possible use is if you're only interested in printing certain
94 tags from the record, then you can filter them when you read from disc
95 and not have to delete unwanted tags yourself.
96  
97 =cut
98  
99 sub decode {
100  
101 152 ▶ my $text;
Subroutine decode: called CC=30 SCAR=36.6
Pod documented
102 152 my $location = '';
103  
104 ## decode can be called in a variety of ways
105 ## $object->decode( $string )
106 ## MARC::File::USMARC->decode( $string )
107 ## MARC::File::USMARC::decode( $string )
108 ## this bit of code covers all three
109  
110 152 my $self = shift;
111 152 ▶ if ( ref($self) =~ /^MARC::File/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /^MARC::File/) { } 146 6 152
112 146 $location = 'in record '.$self->{recnum};
113 146 $text = shift;
114 } else {
115 6 $location = 'in record 1';
116 6 ▶ $text = $self=~/MARC::File/ ? shift : $self;
Branch: 1 branch
BranchTrueFalseTotal
$self =~ /MARC::File/ ? : 3 3 6
117 }
118  
119 152 my $filter_func = shift;
120  
121 152 my $marc = MARC::Record->new();
122  
123 # Check for an all-numeric record length
124 152 ▶ ($text =~ /^(\d{5})/)
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /^(\d{5})/ or return $marc->_warn("Record length \"", substr($text, 0, 5), qq[" is not numeric $location]) 3 149 152
125 or return $marc->_warn( "Record length \"", substr( $text, 0, 5 ), "\" is not numeric $location" );
126  
127 149 my $reclen = $1;
128 149 ▶ my $realLength = MARC::File::utf8_safe()
Branch: 1 branch
BranchTrueFalseTotal
MARC::File::utf8_safe() ? : 0 149 149
129 ? MARC::File::Utils::byte_length($text)
130 : length( $text );
131 149 ▶ $marc->_warn( "Invalid record length $location: Leader says $reclen " .
Branch: 1 branch
BranchTrueFalseTotal
unless $reclen == $realLength 0 149 149
132 "bytes but it's actually $realLength" ) unless $reclen == $realLength;
133  
134 149 ▶ (substr($text, -1, 1) eq END_OF_RECORD)
Branch: 1 branch
BranchTrueFalseTotal
substr($text, -1, 1) eq "\35" or $marc->_warn("Invalid record terminator $location") 0 149 149
135 or $marc->_warn( "Invalid record terminator $location" );
136  
137 149 $marc->leader( substr( $text, 0, LEADER_LEN ) );
138  
139 # bytes 12 - 16 of leader give offset to the body of the record
140 149 ▶ my $data_start = 0 + MARC::File::utf8_safe()
Branch: 1 branch
BranchTrueFalseTotal
0 + MARC::File::utf8_safe() ? : 0 149 149
141 ? MARC::File::Utils::byte_substr( $text, 12, 5 )
142 : substr( $text, 12, 5 );
143  
144 # immediately after the leader comes the directory (no separator)
145 149 my $dir = substr( $text, LEADER_LEN, $data_start - LEADER_LEN - 1 ); # -1 to allow for \x1e at end of directory
146  
147 # character after the directory must be \x1e
148 149 ▶ (substr($text, $data_start-1, 1) eq END_OF_FIELD)
Branch: 1 branch
BranchTrueFalseTotal
substr($text, $data_start - 1, 1) eq "\36" or $marc->_warn("No directory found $location") 1 148 149
149 or $marc->_warn( "No directory found $location" );
150  
151 # all directory entries 12 bytes long, so length % 12 must be 0
152 149 ▶ (length($dir) % DIRECTORY_ENTRY_LEN == 0)
Branch: 1 branch
BranchTrueFalseTotal
length($dir) % 12 == 0 or $marc->_warn("Invalid directory length $location") 0 149 149
153 or $marc->_warn( "Invalid directory length $location" );
154  
155  
156 # go through all the fields
157 149 my $nfields = length($dir)/DIRECTORY_ENTRY_LEN;
158 149 ▶ for ( my $n = 0; $n < $nfields; $n++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $nfields) 2490 149 2639
159 2490 my ( $tagno, $len, $offset ) = unpack( "A3 A4 A5", substr($dir, $n*DIRECTORY_ENTRY_LEN, DIRECTORY_ENTRY_LEN) );
160  
161 # Check directory validity
162 2490 ▶ ($tagno =~ /^[0-9A-Za-z]{3}$/)
Branch: 1 branch
BranchTrueFalseTotal
$tagno =~ /^[0-9A-Za-z]{3}$/ or $marc->_warn(qq[Invalid tag in directory ${location}: "$tagno"]) 0 2490 2490
163 or $marc->_warn( "Invalid tag in directory $location: \"$tagno\"" );
164  
165 2490 ▶ ($len =~ /^\d{4}$/)
Branch: 1 branch
BranchTrueFalseTotal
$len =~ /^\d{4}$/ or $marc->_warn(qq[Invalid length in directory $location tag ${tagno}: "$len"]) 0 2490 2490
166 or $marc->_warn( "Invalid length in directory $location tag $tagno: \"$len\"" );
167  
168 2490 ▶ ($offset =~ /^\d{5}$/)
Branch: 1 branch
BranchTrueFalseTotal
$offset =~ /^\d{5}$/ or $marc->_warn(qq[Invalid offset in directory $location tag ${tagno}: "$offset"]) 0 2490 2490
169 or $marc->_warn( "Invalid offset in directory $location tag $tagno: \"$offset\"" );
170  
171 2490 ▶ ($offset + $len <= $reclen)
Branch: 1 branch
BranchTrueFalseTotal
$offset + $len <= $reclen or $marc->_warn("Directory entry $location runs off the end of the record tag $tagno") 0 2490 2490
172 or $marc->_warn( "Directory entry $location runs off the end of the record tag $tagno" );
173  
174 2490 ▶ my $tagdata = MARC::File::utf8_safe()
Branch: 1 branch
BranchTrueFalseTotal
MARC::File::utf8_safe() ? : 0 2490 2490
175 ? MARC::File::Utils::byte_substr($text,$data_start+$offset,$len)
176 : substr( $text, $data_start+$offset, $len );
177  
178 2490 ▶ $marc->_warn( "Invalid length in directory for tag $tagno $location" )
Branch: 2 branches
BranchTrueFalseTotal
unless $len == MARC::File::utf8_safe() ? MARC::File::Utils::byte_length($tagdata) : length $tagdata 0 2490 2490
$len == MARC::File::utf8_safe() ? : 0 2490 2490
179 unless ( $len == MARC::File::utf8_safe()
180 ? MARC::File::Utils::byte_length($tagdata)
181 : length($tagdata) );
182  
183 2490 ▶ if ( substr($tagdata, -1, 1) eq END_OF_FIELD ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($tagdata, -1, 1) eq "\036") { } 2490 0 2490
184 # get rid of the end-of-tag character
185 2490 chop $tagdata;
186 2490 --$len;
187 } else {
188 0 $marc->_warn( "field does not end in end of field character in tag $tagno $location" );
189 }
190  
191 2490 ▶ warn "Specs: ", join( "|", $tagno, $len, $offset, $tagdata ), "\n" if $MARC::Record::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $MARC::Record::DEBUG 0 2490 2490
192  
193  
194 2490 ▶ if ( $filter_func ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter_func) 167 2323 2490
195 167 ▶ next unless $filter_func->( $tagno, $tagdata );
Branch: 1 branch
BranchTrueFalseTotal
unless &$filter_func($tagno, $tagdata) 154 13 167
196 }
197  
198  
199 2336 ▶ if ( ($tagno =~ /^\d+$/) && ($tagno < 10) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagno =~ /^\d+$/ and $tagno < 10) { } 506 1830 2336
Condition100%3/3
$tagno =~ /^\d+$/ and $tagno < 10
!ll&&!rl&&r
11829506
MC/DC100%2/2
$tagno =~ /^\d+$/ and $tagno < 10
$tagno =~ /^\d+$/ $tagno < 10
Truth table100%3/3
A and B
A= $tagno =~ /^\d+$/B= $tagno < 10
ABresult
0X0
100
111
200 506 $marc->append_fields( MARC::Field->new( $tagno, $tagdata ) );
201 } else {
202 1830 my @subfields = split( SUBFIELD_INDICATOR, $tagdata );
203 1830 my $indicators = shift @subfields;
204 1830 my ($ind1, $ind2);
205  
206 1830 ▶ if ( length( $indicators ) > 2 or length( $indicators ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $indicators > 2 or length $indicators == 0) { } 0 1830 1830
Condition33%1/3
length $indicators > 2 or length $indicators == 0
l!l&&r!l&&!r
001830
MC/DC0%0/2
length $indicators > 2 or length $indicators == 0
length $indicators > 2 length $indicators == 0
Truth table33%1/3
A or B
A= length $indicators > 2B= length $indicators == 0
ABresult
1X1
011
000
207 0 $marc->_warn( "Invalid indicators \"$indicators\" forced to blanks $location for tag $tagno\n" );
208 0 ($ind1,$ind2) = (" ", " ");
209 } else {
210 1830 $ind1 = substr( $indicators,0, 1 );
211 1830 $ind2 = substr( $indicators,1, 1 );
212 }
213  
214 # Split the subfield data into subfield name and data pairs
215 1830 my @subfield_data;
216 1830 for ( @subfields ) {
217 3050 ▶ if ( length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > 0) { } 3050 0 3050
218 3050 push( @subfield_data, substr($_,0,1),substr($_,1) );
219 } else {
220 0 $marc->_warn( "Entirely empty subfield found in tag $tagno" );
221 }
222 }
223  
224 1830 ▶ if ( !@subfield_data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subfield_data) 0 1830 1830
225 0 $marc->_warn( "no subfield data found $location for tag $tagno" );
226 0 next;
227 }
228  
229 1830 my $field = MARC::Field->new($tagno, $ind1, $ind2, @subfield_data );
230 1830 ▶ if ( $field->warnings() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->warnings) 2 1828 1830
231 2 $marc->_warn( $field->warnings() );
232 }
233 1830 $marc->append_fields( $field );
234 }
235 } # looping through all the fields
236  
237  
238 149 return $marc;
239 }
240  
241 =head2 update_leader()
242  
243 If any changes get made to the MARC record, the first 5 bytes of the
244 leader (the length) will be invalid. This function updates the
245 leader with the correct length of the record as it would be if
246 written out to a file.
247  
248 =cut
249  
250 sub update_leader() {
251 0 ▶ my $self = shift;
Subroutine update_leader: not called CC=1 SCAR=6.9
Pod documented
252  
253 0 my (undef,undef,$reclen,$baseaddress) = $self->_build_tag_directory();
254  
255 0 $self->_set_leader_lengths( $reclen, $baseaddress );
256 }
257  
258 =head2 _build_tag_directory()
259  
260 Function for internal use only: Builds the tag directory that gets
261 put in front of the data in a MARC record.
262  
263 Returns two array references, and two lengths: The tag directory, and the data fields themselves,
264 the length of all data (including the Leader that we expect will be added),
265 and the size of the Leader and tag directory.
266  
267 =cut
268  
269 sub _build_tag_directory {
270 8 ▶ my $marc = shift;
Subroutine _build_tag_directory: called CC=6 SCAR=18.4
271 8 ▶ $marc = shift if (ref($marc)||$marc) =~ /^MARC::File/;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $marc || $marc) =~ /^MARC::File/ 0 8 8
Condition33%1/3
ref $marc || $marc
l!l&&r!l&&!r
800
MC/DC0%0/2
ref $marc || $marc
ref $marc $marc
Truth table33%1/3
A || B
A= ref $marcB= $marc
ABresult
1X1
011
000
272 8 ▶ die "Wanted a MARC::Record but got a ", ref($marc) unless ref($marc) eq "MARC::Record";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $marc eq "MARC::Record" 0 8 8
273  
274 8 my @fields;
275 my @directory;
276  
277 8 my $dataend = 0;
278 8 for my $field ( $marc->fields() ) {
279 # Dump data into proper format
280 42 my $str = $field->as_usmarc;
281 42 push( @fields, $str );
282  
283 # Create directory entry
284 42 ▶ my $len = MARC::File::utf8_safe()
Branch: 1 branch
BranchTrueFalseTotal
MARC::File::utf8_safe() ? : 0 42 42
285 ? MARC::File::Utils::byte_length( $str )
286 : length( $str );
287  
288 42 my $direntry = sprintf( "%03s%04d%05d", $field->tag, $len, $dataend );
289 42 push( @directory, $direntry );
290 42 $dataend += $len;
291 }
292  
293 8 my $baseaddress =
294 LEADER_LEN + # better be 24
295 ( @directory * DIRECTORY_ENTRY_LEN ) +
296 # all the directory entries
297 1; # end-of-field marker
298  
299  
300 8 my $total =
301 $baseaddress + # stuff before first field
302 $dataend + # Length of the fields
303 1; # End-of-record marker
304  
305  
306  
307 8 return (\@fields, \@directory, $total, $baseaddress);
308 }
309  
310 =head2 encode()
311  
312 Returns a string of characters suitable for writing out to a USMARC file,
313 including the leader, directory and all the fields.
314  
315 =cut
316  
317 sub encode() {
318 8 ▶ my $marc = shift;
Subroutine encode: called CC=3 SCAR=11.0
Pod documented
319 8 ▶ $marc = shift if (ref($marc)||$marc) =~ /^MARC::File/;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $marc || $marc) =~ /^MARC::File/ 1 7 8
Condition66%2/3
ref $marc || $marc
l!l&&r!l&&!r
710
MC/DC0%0/2
ref $marc || $marc
ref $marc $marc
Truth table66%2/3
A || B
A= ref $marcB= $marc
ABresult
1X1
011
000
320  
321 8 my ($fields,$directory,$reclen,$baseaddress) = _build_tag_directory($marc);
322 8 $marc->set_leader_lengths( $reclen, $baseaddress );
323  
324 # Glomp it all together
325 8 return join("",$marc->leader, @$directory, END_OF_FIELD, @$fields, END_OF_RECORD);
326 }
327 27 1;
328  
329 __END__
↑ Summary File/MicroLIF.pm »