source: gs2-extensions/parallel-building/trunk/src/perllib/cpan/MARC/File.pm@ 24626

Last change on this file since 24626 was 24626, checked in by jmt12, 13 years ago

An (almost) complete copy of the perllib directory from a (circa SEP2011) head checkout from Greenstone 2 trunk - in order to try and make merging in this extension a little easier later on (as there have been some major changes to buildcol.pl commited in the main trunk but not in the x64 branch)

File size: 3.2 KB
Line 
1package MARC::File;
2
3=head1 NAME
4
5MARC::File - Base class for files of MARC records
6
7=cut
8
9use strict;
10use integer;
11eval 'use warnings' if $] >= 5.006;
12
13use vars qw( $ERROR );
14
15=head1 VERSION
16
17Version 1.10
18
19 $Id: File.pm 3430 2002-09-24 05:17:39Z jrm21 $
20
21=cut
22
23use 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
39None.
40
41=head1 METHODS
42
43=head2 in()
44
45Opens a file for input.
46
47=cut
48
49sub 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
73sub out {
74 die "Not yet written";
75}
76
77=head2 next()
78
79Reads the next record from the file handle passed in.
80
81=cut
82
83sub next {
84 my $self = shift;
85 my $rec = $self->_next();
86 return $rec ? $self->decode($rec) : undef;
87}
88
89=head2 skip()
90
91Skips over the next record in the file. Same as C<next()>,
92without the overhead of parsing a record you're going to throw away
93anyway.
94
95Returns 1 or undef.
96
97=cut
98
99sub skip {
100 my $self = shift;
101
102 my $rec = $self->_next();
103
104 return $rec ? 1 : undef;
105}
106
107=head2 warnings()
108
109Simlilar to MARC::Record and MARC::Batch, warnings() will return any
110warnings that have accumulated while processing this file; and as a
111side-effect will clear the warnings buffer.
112
113=cut
114
115sub warnings {
116 my $self = shift;
117 my @warnings = @{ $self->{_warnings} };
118 $self->{_warnings} = [];
119 return(@warnings);
120}
121
122sub close {
123 my $self = shift;
124
125 close( $self->{fh} );
126 delete $self->{fh};
127 delete $self->{filename};
128
129 return;
130}
131
132sub _unimplemented() {
133 my $self = shift;
134 my $method = shift;
135
136 warn "Method $method must be overridden";
137}
138
139sub write { $_[0]->_unimplemented("write"); }
140sub decode { $_[0]->_unimplemented("decode"); }
141
142# NOTE: _warn must be called as an object method
143
144sub _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.
151sub _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
1701;
171
172__END__
173
174=head1 RELATED MODULES
175
176L<MARC::Record>
177
178=head1 TODO
179
180=over 4
181
182=item * C<out()> method
183
184We only handle files for input right now.
185
186=back
187
188=cut
189
190=head1 LICENSE
191
192This code may be distributed under the same terms as Perl itself.
193
194Please note that these modules are not products of or supported by the
195employers of the various contributors to the code.
196
197=head1 AUTHOR
198
199Andy Lester, E<lt>[email protected]<gt> or E<lt>[email protected]<gt>
200
201=cut
202
Note: See TracBrowser for help on using the repository browser.