| Filename | /usr/share/perl5/MARC/File.pm |
| Statements | Executed 11 statements in 592µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 11µs | 22µs | MARC::File::BEGIN@9 |
| 1 | 1 | 1 | 7µs | 17µs | MARC::File::BEGIN@62 |
| 1 | 1 | 1 | 7µs | 10µs | MARC::File::BEGIN@10 |
| 1 | 1 | 1 | 6µs | 8µs | MARC::File::BEGIN@11 |
| 1 | 1 | 1 | 6µs | 22µs | MARC::File::BEGIN@13 |
| 0 | 0 | 0 | 0s | 0s | MARC::File::_gripe |
| 0 | 0 | 0 | 0s | 0s | MARC::File::_unimplemented |
| 0 | 0 | 0 | 0s | 0s | MARC::File::_warn |
| 0 | 0 | 0 | 0s | 0s | MARC::File::close |
| 0 | 0 | 0 | 0s | 0s | MARC::File::decode |
| 0 | 0 | 0 | 0s | 0s | MARC::File::in |
| 0 | 0 | 0 | 0s | 0s | MARC::File::next |
| 0 | 0 | 0 | 0s | 0s | MARC::File::out |
| 0 | 0 | 0 | 0s | 0s | MARC::File::skip |
| 0 | 0 | 0 | 0s | 0s | MARC::File::warnings |
| 0 | 0 | 0 | 0s | 0s | MARC::File::write |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package MARC::File; | ||||
| 2 | |||||
| 3 | =head1 NAME | ||||
| 4 | |||||
| 5 | MARC::File - Base class for files of MARC records | ||||
| 6 | |||||
| 7 | =cut | ||||
| 8 | |||||
| 9 | 2 | 22µs | 2 | 33µs | # spent 22µs (11+11) within MARC::File::BEGIN@9 which was called:
# once (11µs+11µs) by MARC::File::USMARC::BEGIN@16 at line 9 # spent 22µs making 1 call to MARC::File::BEGIN@9
# spent 11µs making 1 call to strict::import |
| 10 | 2 | 18µs | 2 | 14µs | # spent 10µs (7+4) within MARC::File::BEGIN@10 which was called:
# once (7µs+4µs) by MARC::File::USMARC::BEGIN@16 at line 10 # spent 10µs making 1 call to MARC::File::BEGIN@10
# spent 4µs making 1 call to warnings::import |
| 11 | 2 | 26µs | 2 | 9µs | # spent 8µs (6+2) within MARC::File::BEGIN@11 which was called:
# once (6µs+2µs) by MARC::File::USMARC::BEGIN@16 at line 11 # spent 8µs making 1 call to MARC::File::BEGIN@11
# spent 2µs making 1 call to integer::import |
| 12 | |||||
| 13 | 2 | 62µs | 2 | 38µs | # spent 22µs (6+16) within MARC::File::BEGIN@13 which was called:
# once (6µs+16µs) by MARC::File::USMARC::BEGIN@16 at line 13 # spent 22µs making 1 call to MARC::File::BEGIN@13
# spent 16µs making 1 call to vars::import |
| 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 | sub in { | ||||
| 57 | my $class = shift; | ||||
| 58 | my $arg = shift; | ||||
| 59 | my ( $filename, $fh ); | ||||
| 60 | |||||
| 61 | ## if a valid filehandle was passed in | ||||
| 62 | 2 | 462µs | 2 | 28µs | # spent 17µs (7+11) within MARC::File::BEGIN@62 which was called:
# once (7µs+11µs) by MARC::File::USMARC::BEGIN@16 at line 62 # spent 17µs making 1 call to MARC::File::BEGIN@62
# spent 10µs making 1 call to strict::unimport |
| 63 | if ( $ishandle ) { | ||||
| 64 | $filename = scalar( $arg ); | ||||
| 65 | $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 | $filename = $arg; | ||||
| 72 | $fh = eval { local *FH; open( FH, '<', $arg ) or die; *FH{IO}; }; | ||||
| 73 | if ( $@ ) { | ||||
| 74 | $MARC::File::ERROR = "Couldn't open $filename: $@"; | ||||
| 75 | return; | ||||
| 76 | } | ||||
| 77 | } | ||||
| 78 | |||||
| 79 | my $self = { | ||||
| 80 | filename => $filename, | ||||
| 81 | fh => $fh, | ||||
| 82 | recnum => 0, | ||||
| 83 | warnings => [], | ||||
| 84 | }; | ||||
| 85 | |||||
| 86 | return( bless $self, $class ); | ||||
| 87 | |||||
| 88 | } # new() | ||||
| 89 | |||||
| 90 | sub out { | ||||
| 91 | 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 | sub next { | ||||
| 107 | my $self = shift; | ||||
| 108 | $self->{recnum}++; | ||||
| 109 | my $rec = $self->_next() or return; | ||||
| 110 | 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 | sub skip { | ||||
| 124 | my $self = shift; | ||||
| 125 | my $rec = $self->_next() or return; | ||||
| 126 | 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 | sub warnings { | ||||
| 138 | my $self = shift; | ||||
| 139 | my @warnings = @{ $self->{warnings} }; | ||||
| 140 | $self->{warnings} = []; | ||||
| 141 | 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 | sub close { | ||||
| 151 | my $self = shift; | ||||
| 152 | close( $self->{fh} ); | ||||
| 153 | delete $self->{fh}; | ||||
| 154 | delete $self->{filename}; | ||||
| 155 | return; | ||||
| 156 | } | ||||
| 157 | |||||
| 158 | sub _unimplemented { | ||||
| 159 | my $self = shift; | ||||
| 160 | my $method = shift; | ||||
| 161 | 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 | sub write { $_[0]->_unimplemented("write"); } | ||||
| 177 | sub decode { $_[0]->_unimplemented("decode"); } | ||||
| 178 | |||||
| 179 | # NOTE: _warn must be called as an object method | ||||
| 180 | |||||
| 181 | sub _warn { | ||||
| 182 | my ($self,$warning) = @_; | ||||
| 183 | push( @{ $self->{warnings} }, "$warning in record ".$self->{recnum} ); | ||||
| 184 | 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 | sub _gripe { | ||||
| 190 | my @parms = @_; | ||||
| 191 | if ( @parms ) { | ||||
| 192 | my $self = shift @parms; | ||||
| 193 | |||||
| 194 | if ( ref($self) =~ /^MARC::File/ ) { | ||||
| 195 | push( @parms, " at byte ", tell($self->{fh}) ) | ||||
| 196 | if $self->{fh}; | ||||
| 197 | push( @parms, " in file ", $self->{filename} ) if $self->{filename}; | ||||
| 198 | } else { | ||||
| 199 | unshift( @parms, $self ); | ||||
| 200 | } | ||||
| 201 | |||||
| 202 | $ERROR = join( "", @parms ); | ||||
| 203 | warn $ERROR; | ||||
| 204 | } | ||||
| 205 | |||||
| 206 | return; | ||||
| 207 | } | ||||
| 208 | |||||
| 209 | 1 | 2µs | 1; | ||
| 210 | |||||
| 211 | __END__ |