Batch.pm

filter: Statementstmt 90.3% 56 / 62 Branchbran 76.9% 20 / 26 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 81.8% 9 / 11 Podpod 100% 8 / 8 totaltotal 87.5% 98 / 112 CC 15 SCAR 18.1
CC
15
Cov
87%
next
11.4
new
3.0
warnings
3.0
CRAP 6.1
« Field.pm ↑ Summary Record.pm »
1 package MARC::Batch;
2  
3 =head1 NAME
4  
5 MARC::Batch - Perl module for handling files of MARC::Record objects
6  
7 =head1 SYNOPSIS
8  
9 MARC::Batch hides all the file handling of files of C<MARC::Record>s.
10 C<MARC::Record> still does the file I/O, but C<MARC::Batch> handles the
11 multiple-file aspects.
12  
13 use MARC::Batch;
14  
15 # If you have weird control fields...
16 use MARC::Field;
17 MARC::Field->allow_controlfield_tags('FMT', 'LDX');
18
19  
20 my $batch = MARC::Batch->new( 'USMARC', @files );
21 while ( my $marc = $batch->next ) {
22 print $marc->subfield(245,"a"), "\n";
23 }
24  
25 =head1 EXPORT
26  
27 None. Everything is a class method.
28  
29 =cut
30  
31 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 14 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 14 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =head1 METHODS
36  
37 =head2 new( $type, @files )
38  
39 Create a C<MARC::Batch> object that will process C<@files>.
40  
41 C<$type> must be either "USMARC" or "MicroLIF". If you want to specify
42 "MARC::File::USMARC" or "MARC::File::MicroLIF", that's OK, too. C<new()> returns a
43 new MARC::Batch object.
44  
45 C<@files> can be a list of filenames:
46  
47 my $batch = MARC::Batch->new( 'USMARC', 'file1.marc', 'file2.marc' );
48  
49 Your C<@files> may also contain filehandles. So if you've got a large
50 file that's gzipped you can open a pipe to F<gzip> and pass it in:
51  
52 my $fh = IO::File->new( 'gunzip -c marc.dat.gz |' );
53 my $batch = MARC::Batch->new( 'USMARC', $fh );
54  
55 And you can mix and match if you really want to:
56  
57 my $batch = MARC::Batch->new( 'USMARC', $fh, 'file1.marc' );
58  
59 =cut
60  
61 17 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
62 17 my $class = shift;
63 17 my $type = shift;
64  
65 17 ▶ my $marcclass = ($type =~ /^MARC::File/) ? $type : "MARC::File::$type";
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^MARC::File/ ? : 5 12 17
66  
67 17 eval "require $marcclass";
68 17 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 17 17
69  
70 17 my @files = @_;
71  
72 17 my $self = {
73 filestack => \@files,
74 filename => undef,
75 marcclass => $marcclass,
76 file => undef,
77 warnings => [],
78 'warn' => 1,
79 strict => 1,
80 };
81  
82 17 bless $self, $class;
83  
84 17 return $self;
85 } # new()
86  
87  
88 =head2 next()
89  
90 Read the next record from that batch, and return it as a MARC::Record
91 object. If the current file is at EOF, close it and open the next
92 one. C<next()> will return C<undef> when there is no more data to be
93 read from any batch files.
94  
95 By default, C<next()> also will return C<undef> if an error is
96 encountered while reading from the batch. If not checked for this can
97 cause your iteration to terminate prematurely. To alter this behavior,
98 see C<strict_off()>. You can retrieve warning messages using the
99 C<warnings()> method.
100  
101 Optionally you can pass in a filter function as a subroutine reference
102 if you are only interested in particular fields from the record. This
103 can boost performance.
104  
105 =cut
106  
107 303 ▶sub next {
Subroutine next: called CC=11 SCAR=24.3
Pod documented
108 303 my ( $self, $filter ) = @_;
109 303 ▶ if ( $filter and ref($filter) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter and ref $filter ne "CODE") 1 302 303
Condition100%3/3
$filter and ref $filter ne "CODE"
!ll&&!rl&&r
30021
MC/DC100%2/2
$filter and ref $filter ne "CODE"
$filter ref $filter ne "CODE"
Truth table100%3/3
A and B
A= $filterB= ref $filter ne "CODE"
ABresult
0X0
100
111
110 1 croak( "filter function in next() must be a subroutine reference" );
111 }
112  
113 302 ▶ if ( $self->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file"}) 285 17 302
114  
115 # get the next record
116 285 my $rec = $self->{file}->next( $filter );
117  
118 # collect warnings from MARC::File::* object
119 # we use the warnings() method here since MARC::Batch
120 # hides access to MARC::File objects, and we don't
121 # need to preserve the warnings buffer.
122 285 my @warnings = $self->{file}->warnings();
123 285 ▶ if ( @warnings ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@warnings) 0 285 285
124 0 $self->warnings( @warnings );
125 0 ▶ return if $self->{ strict };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} 0 0 0
126 }
127  
128 285 ▶ if ($rec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec) 271 14 285
129  
130 # collect warnings from the MARC::Record object
131 # IMPORTANT: here we don't use warnings() but dig
132 # into the the object to get at the warnings without
133 # erasing the buffer. This is so a user can call
134 # warnings() on the MARC::Record object and get back
135 # warnings for that specific record.
136 271 my @warnings = @{ $rec->{_warnings} };
137  
138 271 ▶ if (@warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if (@warnings) 5 266 271
139 5 $self->warnings( @warnings );
140 5 ▶ return if $self->{ strict };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} 1 4 5
141 }
142  
143 # return the MARC::Record object
144 270 return($rec);
145  
146 }
147  
148 }
149  
150 # Get the next file off the stack, if there is one
151 31 ▶ $self->{filename} = shift @{$self->{filestack}} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"filename"} = shift @{$$self{"filestack"};} or return 24 7 31
152  
153 # Instantiate a filename for it
154 24 my $marcclass = $self->{marcclass};
155 24 ▶ $self->{file} = $marcclass->in( $self->{filename} ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"file"} = $marcclass->in($self->{"filename"}) or return 24 0 24
156  
157 # call this method again now that we've got a file open
158 24 return( $self->next( $filter ) );
159  
160 }
161  
162 =head2 strict_off()
163  
164 If you would like C<MARC::Batch> to continue after it has encountered what
165 it believes to be bad MARC data then use this method to turn strict B<OFF>.
166 A call to C<strict_off()> always returns true (1).
167  
168 C<strict_off()> can be handy when you don't care about the quality of your
169 MARC data, and just want to plow through it. For safety, C<MARC::Batch>
170 strict is B<ON> by default.
171  
172 =cut
173  
174 3 ▶sub strict_off {
Subroutine strict_off: called CC=1 SCAR=0.0
Pod documented
175 3 my $self = shift;
176 3 $self->{ strict } = 0;
177 3 return(1);
178 }
179  
180 =head2 strict_on()
181  
182 The opposite of C<strict_off()>, and the default state. You shouldn't
183 have to use this method unless you've previously used C<strict_off()>, and
184 want it back on again. When strict is B<ON> calls to next() will return
185 undef when an error is encountered while reading MARC data. strict_on()
186 always returns true (1).
187  
188 =cut
189  
190 1 ▶sub strict_on {
Subroutine strict_on: called CC=1 SCAR=0.0
Pod documented
191 1 my $self = shift;
192 1 $self->{ strict } = 1;
193 1 return(1);
194 }
195  
196 =head2 warnings()
197  
198 Returns a list of warnings that have accumulated while processing a particular
199 batch file. As a side effect the warning buffer will be cleared.
200  
201 my @warnings = $batch->warnings();
202  
203 This method is also used internally to set warnings, so you probably don't
204 want to be passing in anything as this will set warnings on your batch object.
205  
206 C<warnings()> will return the empty list when there are no warnings.
207  
208 =cut
209  
210 9 ▶sub warnings {
Subroutine warnings: called CC=3 SCAR=11.0
Pod documented
211 9 my ($self,@new) = @_;
212 9 ▶ if ( @new ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@new) { } 5 4 9
213 5 push( @{ $self->{warnings} }, @new );
214 5 ▶ print STDERR join( "\n", @new ) . "\n" if $self->{'warn'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"warn"} 0 5 5
215 } else {
216 4 my @old = @{ $self->{warnings} };
217 4 $self->{warnings} = [];
218 4 return(@old);
219 }
220 }
221  
222  
223 =head2 warnings_off()
224  
225 Turns off the default behavior of printing warnings to STDERR. However, even
226 with warnings off the messages can still be retrieved using the warnings()
227 method if you wish to check for them.
228  
229 C<warnings_off()> always returns true (1).
230  
231 =cut
232  
233 4 ▶sub warnings_off {
Subroutine warnings_off: called CC=1 SCAR=0.0
Pod documented
234 4 my $self = shift;
235 4 $self->{ 'warn' } = 0;
236  
237 4 return 1;
238 }
239  
240 =head2 warnings_on()
241  
242 Turns on warnings so that diagnostic information is printed to STDERR. This
243 is on by default so you shouldn't have to use it unless you've previously
244 turned off warnings using warnings_off().
245  
246 warnings_on() always returns true (1).
247  
248 =cut
249  
250 0 ▶sub warnings_on {
Subroutine warnings_on: not called CC=1 SCAR=6.9
Pod documented
251 0 my $self = shift;
252 0 $self->{ 'warn' } = 1;
253 }
254  
255 =head2 filename()
256  
257 Returns the currently open filename or C<undef> if there is not currently a file
258 open on this batch object.
259  
260 =cut
261  
262 0 ▶sub filename {
Subroutine filename: not called CC=1 SCAR=6.9
Pod documented
263 0 my $self = shift;
264  
265 0 return $self->{filename};
266 }
267  
268  
269 14 1;
270  
271 __END__
« Field.pm ↑ Summary Record.pm »