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