File.pm

filter: Statementstmt 64.0% 48 / 75 Branchbran 31.8% 7 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 58.8% 10 / 17 Podpod 100% 9 / 9 totaltotal 60.1% 74 / 123 CC 12 SCAR 20.5
CC
12
Cov
57%
_gripe
30.0
skip
6.0
in
5.1
CRAP 7.7
« File/MicroLIF.pm ↑ Summary Batch.pm »
1 package MARC::File;
2  
3 =head1 NAME
4  
5 MARC::File - Base class for files of MARC records
6  
7 =cut
8  
9 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use MARC::File::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 30 ▶use vars qw( $ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 SYNOPSIS
16  
17 use MARC::File::USMARC;
18  
19 my $file = MARC::File::USMARC->in( $filename );
20  
21 while ( my $marc = $file->next() ) {
22 # Do something
23 }
24 $file->close();
25 undef $file;
26  
27 =head1 EXPORT
28  
29 None.
30  
31 =head1 METHODS
32  
33 =head2 in()
34  
35 Opens a file for import. Ordinarily you will use C<MARC::File::USMARC>
36 or C<MARC::File::MicroLIF> to do this.
37  
38 my $file = MARC::File::USMARC->in( 'file.marc' );
39  
40 Returns a C<MARC::File> object, or C<undef> on failure. If you
41 encountered an error the error message will be stored in
42 C<$MARC::File::ERROR>.
43  
44 Optionally you can also pass in a filehandle, and C<MARC::File>.
45 will "do the right thing".
46  
47 my $handle = IO::File->new( 'gunzip -c file.marc.gz |' );
48 my $file = MARC::File::USMARC->in( $handle );
49  
50 =cut
51  
52 sub in {
53 43 ▶ my $class = shift;
Subroutine in: called CC=5 SCAR=16.3
Pod documented
54 43 my $arg = shift;
55 43 my ( $filename, $fh );
56  
57 ## if a valid filehandle was passed in
58 30 ▶ my $ishandle = do { no strict; defined fileno($arg); };
Subroutine BEGIN: called CC=1 SCAR=0.0
59 43 ▶ if ( $ishandle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ishandle) { } 11 32 43
60 11 $filename = scalar( $arg );
61 11 $fh = $arg;
62 }
63  
64 ## otherwise check if it's a filename, and
65 ## return undef if we weren't able to open it
66 else {
67 32 $filename = $arg;
68 32 ▶ $fh = eval { local *FH; open( FH, $arg ) or die; *FH{IO}; };
Branch: 1 branch
BranchTrueFalseTotal
open FH, $arg or die 0 32 32
69 32 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 32 32
70 0 $MARC::File::ERROR = "Couldn't open $filename: $@";
71 0 return;
72 }
73 ## all file streams are assumed to be utf8 if we have a modern perl
74 }
75  
76 43 ▶ utf8_safe() ? binmode( $fh, ':utf8' ) : binmode( $fh );
Branch: 1 branch
BranchTrueFalseTotal
utf8_safe() ? : 0 43 43
77 43 my $self = {
78 filename => $filename,
79 fh => $fh,
80 recnum => 0,
81 warnings => [],
82 };
83  
84 43 return( bless $self, $class );
85  
86 } # new()
87  
88 sub out {
89 0 ▶ die "Not yet written";
Subroutine out: not called CC=1 SCAR=6.9
Pod documented
90 }
91  
92 =head2 next( [\&filter_func] )
93  
94 Reads the next record from the file handle passed in.
95  
96 The C<$filter_func> is a reference to a filtering function. Currently,
97 only USMARC records support this. See L<MARC::File::USMARC>'s C<decode()>
98 function for details.
99  
100 Returns a MARC::Record reference, or C<undef> on error.
101  
102 =cut
103  
104 sub next {
105 390 ▶ my $self = shift;
Subroutine next: called CC=2 SCAR=6.9
Pod documented
106 390 $self->{recnum}++;
107 390 ▶ my $rec = $self->_next() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->_next or return 19 371 390
108 371 return $self->decode($rec, @_);
109 }
110  
111 =head2 skip()
112  
113 Skips over the next record in the file. Same as C<next()>,
114 without the overhead of parsing a record you're going to throw away
115 anyway.
116  
117 Returns 1 or undef.
118  
119 =cut
120  
121 sub skip {
122 0 ▶ my $self = shift;
Subroutine skip: not called CC=2 SCAR=17.9
Pod documented
123 0 ▶ my $rec = $self->_next() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->_next or return 0 0 0
124 0 return 1;
125 }
126  
127 =head2 warnings()
128  
129 Simlilar to the methods in L<MARC::Record> and L<MARC::Batch>,
130 C<warnings()> will return any warnings that have accumulated while
131 processing this file; and as a side-effect will clear the warnings buffer.
132  
133 =cut
134  
135 sub warnings {
136 288 ▶ my $self = shift;
Subroutine warnings: called CC=1 SCAR=0.0
Pod documented
137 288 my @warnings = @{ $self->{warnings} };
138 288 $self->{warnings} = [];
139 288 return(@warnings);
140 }
141  
142 =head2 close()
143  
144 Closes the file, both from the object's point of view, and the actual file.
145  
146 =cut
147  
148 sub close {
149 12 ▶ my $self = shift;
Subroutine close: called CC=1 SCAR=0.0
Pod documented
150 12 close( $self->{fh} );
151 12 delete $self->{fh};
152 12 delete $self->{filename};
153 12 return;
154 }
155  
156 sub _unimplemented() {
157 0 ▶ my $self = shift;
Subroutine _unimplemented: not called CC=1 SCAR=6.9
158 0 my $method = shift;
159 0 warn "Method $method must be overridden";
160 }
161  
162 =head2 write()
163  
164 Writes a record to the output file. This method must be overridden
165 in your subclass.
166  
167 =head2 decode()
168  
169 Decodes a record into a USMARC format. This method must be overridden
170 in your subclass.
171  
172 =cut
173  
174 0 ▶sub write { $_[0]->_unimplemented("write"); }
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
175 0 ▶sub decode { $_[0]->_unimplemented("decode"); }
Subroutine decode: not called CC=1 SCAR=6.9
Pod documented
176  
177 # NOTE: _warn must be called as an object method
178  
179 sub _warn {
180 0 ▶ my ($self,$warning) = @_;
Subroutine _warn: not called CC=1 SCAR=6.9
181 0 push( @{ $self->{warnings} }, "$warning in record ".$self->{recnum} );
182 0 return( $self );
183 }
184  
185 # NOTE: _gripe can be called as an object method, or not. Your choice.
186 # NOTE: it's use is now depracated use _warn instead
187 sub _gripe(@) {
188 0 ▶ my @parms = @_;
Subroutine _gripe: not called CC=5 SCAR=34.0
189 0 ▶ if ( @parms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parms) 0 0 0
190 0 my $self = shift @parms;
191  
192 0 ▶ if ( ref($self) =~ /^MARC::File/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /^MARC::File/) { } 0 0 0
193 push( @parms, " at byte ", tell($self->{fh}) )
194 0 ▶ if $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"} 0 0 0
195 0 ▶ push( @parms, " in file ", $self->{filename} ) if $self->{filename};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"filename"} 0 0 0
196 } else {
197 0 unshift( @parms, $self );
198 }
199  
200 0 $ERROR = join( "", @parms );
201 0 warn $ERROR;
202 }
203  
204 0 return;
205 }
206  
207 =head2 utf8_safe()
208  
209 Tells whether the version of Perl we're using is UFT8-safe.
210  
211 =cut
212  
213 sub utf8_safe {
214 5392 ▶ return 0; ## XXX eventually we should be able to handle utf8
Subroutine utf8_safe: called CC=2 SCAR=14.0
Pod documented
215 0 ▶ return 1 if $] >= 5.008001;
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008001" 0 0 0
216  
217 0 return 0;
218 }
219  
220 30 1;
221  
222 __END__
« File/MicroLIF.pm ↑ Summary Batch.pm »