File/USMARC.pm

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