1 | package MARC::File;
|
---|
2 |
|
---|
3 | =head1 NAME
|
---|
4 |
|
---|
5 | MARC::File - Base class for files of MARC records
|
---|
6 |
|
---|
7 | =cut
|
---|
8 |
|
---|
9 | use strict;
|
---|
10 | use integer;
|
---|
11 | eval 'use warnings' if $] >= 5.006;
|
---|
12 |
|
---|
13 | use vars qw( $ERROR );
|
---|
14 |
|
---|
15 | =head1 VERSION
|
---|
16 |
|
---|
17 | Version 1.10
|
---|
18 |
|
---|
19 | $Id: File.pm 3430 2002-09-24 05:17:39Z jrm21 $
|
---|
20 |
|
---|
21 | =cut
|
---|
22 |
|
---|
23 | use vars '$VERSION'; $VERSION = '1.10';
|
---|
24 |
|
---|
25 | =head1 SYNOPSIS
|
---|
26 |
|
---|
27 | use MARC::File::USMARC;
|
---|
28 |
|
---|
29 | my $file = MARC::File::USMARC->in( $filename );
|
---|
30 |
|
---|
31 | while ( my $marc = $file->next() ) {
|
---|
32 | # Do something
|
---|
33 | }
|
---|
34 | $file->close();
|
---|
35 | undef $file;
|
---|
36 |
|
---|
37 | =head1 EXPORT
|
---|
38 |
|
---|
39 | None.
|
---|
40 |
|
---|
41 | =head1 METHODS
|
---|
42 |
|
---|
43 | =head2 in()
|
---|
44 |
|
---|
45 | Opens a file for input.
|
---|
46 |
|
---|
47 | =cut
|
---|
48 |
|
---|
49 | sub in {
|
---|
50 | my $class = shift;
|
---|
51 | my $filename = shift;
|
---|
52 |
|
---|
53 | my $self = {
|
---|
54 | filename => $filename,
|
---|
55 | _warnings => [],
|
---|
56 | };
|
---|
57 |
|
---|
58 | bless $self, $class;
|
---|
59 |
|
---|
60 | my $fh = eval { local *FH; open( FH, $filename ) or die; *FH{IO}; };
|
---|
61 |
|
---|
62 | if ( $@ ) {
|
---|
63 | undef $self;
|
---|
64 | $MARC::File::ERROR = "Couldn't open $filename: $@";
|
---|
65 | } else {
|
---|
66 | $self->{fh} = $fh;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | return $self;
|
---|
71 | } # new()
|
---|
72 |
|
---|
73 | sub out {
|
---|
74 | die "Not yet written";
|
---|
75 | }
|
---|
76 |
|
---|
77 | =head2 next()
|
---|
78 |
|
---|
79 | Reads the next record from the file handle passed in.
|
---|
80 |
|
---|
81 | =cut
|
---|
82 |
|
---|
83 | sub next {
|
---|
84 | my $self = shift;
|
---|
85 | my $rec = $self->_next();
|
---|
86 | return $rec ? $self->decode($rec) : undef;
|
---|
87 | }
|
---|
88 |
|
---|
89 | =head2 skip()
|
---|
90 |
|
---|
91 | Skips over the next record in the file. Same as C<next()>,
|
---|
92 | without the overhead of parsing a record you're going to throw away
|
---|
93 | anyway.
|
---|
94 |
|
---|
95 | Returns 1 or undef.
|
---|
96 |
|
---|
97 | =cut
|
---|
98 |
|
---|
99 | sub skip {
|
---|
100 | my $self = shift;
|
---|
101 |
|
---|
102 | my $rec = $self->_next();
|
---|
103 |
|
---|
104 | return $rec ? 1 : undef;
|
---|
105 | }
|
---|
106 |
|
---|
107 | =head2 warnings()
|
---|
108 |
|
---|
109 | Simlilar to MARC::Record and MARC::Batch, warnings() will return any
|
---|
110 | warnings that have accumulated while processing this file; and as a
|
---|
111 | side-effect will clear the warnings buffer.
|
---|
112 |
|
---|
113 | =cut
|
---|
114 |
|
---|
115 | sub warnings {
|
---|
116 | my $self = shift;
|
---|
117 | my @warnings = @{ $self->{_warnings} };
|
---|
118 | $self->{_warnings} = [];
|
---|
119 | return(@warnings);
|
---|
120 | }
|
---|
121 |
|
---|
122 | sub close {
|
---|
123 | my $self = shift;
|
---|
124 |
|
---|
125 | close( $self->{fh} );
|
---|
126 | delete $self->{fh};
|
---|
127 | delete $self->{filename};
|
---|
128 |
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | sub _unimplemented() {
|
---|
133 | my $self = shift;
|
---|
134 | my $method = shift;
|
---|
135 |
|
---|
136 | warn "Method $method must be overridden";
|
---|
137 | }
|
---|
138 |
|
---|
139 | sub write { $_[0]->_unimplemented("write"); }
|
---|
140 | sub decode { $_[0]->_unimplemented("decode"); }
|
---|
141 |
|
---|
142 | # NOTE: _warn must be called as an object method
|
---|
143 |
|
---|
144 | sub _warn {
|
---|
145 | my ($self,$warning) = @_;
|
---|
146 | push( @{ $self->{_warnings} }, $warning );
|
---|
147 | return(undef);
|
---|
148 | }
|
---|
149 |
|
---|
150 | # NOTE: _gripe can be called as an object method, or not. Your choice.
|
---|
151 | sub _gripe(@) {
|
---|
152 | my @parms = @_;
|
---|
153 | if ( @parms ) {
|
---|
154 | my $self = shift @parms;
|
---|
155 |
|
---|
156 | if ( ref($self) =~ /^MARC::File/ ) {
|
---|
157 | push( @parms, " at byte ", tell($self->{fh}) ) if $self->{fh};
|
---|
158 | push( @parms, " in file ", $self->{filename} ) if $self->{filename};
|
---|
159 | } else {
|
---|
160 | unshift( @parms, $self );
|
---|
161 | }
|
---|
162 |
|
---|
163 | $ERROR = join( "", @parms );
|
---|
164 | warn $ERROR;
|
---|
165 | }
|
---|
166 |
|
---|
167 | return(undef);
|
---|
168 | }
|
---|
169 |
|
---|
170 | 1;
|
---|
171 |
|
---|
172 | __END__
|
---|
173 |
|
---|
174 | =head1 RELATED MODULES
|
---|
175 |
|
---|
176 | L<MARC::Record>
|
---|
177 |
|
---|
178 | =head1 TODO
|
---|
179 |
|
---|
180 | =over 4
|
---|
181 |
|
---|
182 | =item * C<out()> method
|
---|
183 |
|
---|
184 | We only handle files for input right now.
|
---|
185 |
|
---|
186 | =back
|
---|
187 |
|
---|
188 | =cut
|
---|
189 |
|
---|
190 | =head1 LICENSE
|
---|
191 |
|
---|
192 | This code may be distributed under the same terms as Perl itself.
|
---|
193 |
|
---|
194 | Please note that these modules are not products of or supported by the
|
---|
195 | employers of the various contributors to the code.
|
---|
196 |
|
---|
197 | =head1 AUTHOR
|
---|
198 |
|
---|
199 | Andy Lester, E<lt>[email protected]<gt> or E<lt>[email protected]<gt>
|
---|
200 |
|
---|
201 | =cut
|
---|
202 |
|
---|