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