Template that goes with opac-userupdate.pl
[koha.git] / marc / File.pm
1 package MARC::File;
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 =head1 NAME
22
23 MARC::File - Base class for files of MARC records
24
25 =cut
26
27 use 5.6.0;
28 use strict;
29 use integer;
30 use vars qw( $VERSION $ERROR );
31
32 =head1 VERSION
33
34 Version 0.93
35
36     $Id$
37
38 =cut
39
40 our $VERSION = '0.93';
41
42 =head1 SYNOPSIS
43
44     use MARC::File::USMARC;
45
46     my $file = MARC::File::USMARC->in( $filename );
47     
48     while ( my $marc = $file->next() ) {
49         # Do something
50     }
51     $file->close();
52     undef $file;
53
54 =head1 EXPORT
55
56 None.  
57
58 =head1 METHODS
59
60 =head2 in()
61
62 Opens a file for input.
63
64 =cut
65
66 sub in {
67     my $class = shift;
68     my $filename = shift;
69
70     my $self = {
71         filename => $filename,
72     };
73
74     bless $self, $class;
75
76     if ( !open( $self->{fh}, "<", $filename ) ) {
77         undef $self;
78         $MARC::File::ERROR = "Couldn't open $filename: $!";
79     }
80
81     return $self;
82 } # new()
83
84 sub indata {
85     my $class = shift;
86     my $data = shift;
87
88     my $self = {
89         fh => '',
90         data => $data,
91         pointer => 0,
92     };
93
94     bless $self, $class;
95
96 #    if ( !open( $self->{fh}, "<", $filename ) ) {
97 #       undef $self;
98 #       $MARC::File::ERROR = "Couldn't open $filename: $!";
99 #    }
100
101     return $self;
102 } # new()
103
104 sub out {
105     die "Not yet written";
106 }
107
108 =head2 next()
109
110 Reads the next record from the file handle passed in.
111
112 =cut
113
114 sub next {
115     my $self = shift;
116
117     my $rec = $self->_next();
118
119     return $rec ? $self->decode($rec) : undef;
120 }
121
122 =head2 skip
123
124 Skips over the next record in the file.  Same as C<next()>,
125 without the overhead of parsing a record you're going to throw away
126 anyway.
127
128 Returns 1 or undef.
129
130 =cut
131
132 sub skip {
133     my $self = shift;
134
135     my $rec = $self->_next();
136
137     return $rec ? 1 : undef;
138 }
139
140 sub close {
141     my $self = shift;
142
143     close( $self->{fh} );
144     delete $self->{fh};
145
146     return;
147 }
148
149 sub _unimplemented() {
150     my $self = shift;
151     my $method = shift;
152
153     warn "Method $method must be overridden";
154 }
155
156 sub write   { $_[0]->_unimplemented("write"); }
157 sub decode  { $_[0]->_unimplemented("decode"); }
158
159 # NOTE: _gripe can be called as an object method, or not.  Your choice.
160 sub _gripe(@) {
161     if ( @_ ) {
162         shift if ref($_[0]) =~ /^MARC::File/;   # Skip first parm if it's a $self
163         $ERROR = join( "", @_ );
164     }
165
166     return undef;
167 }
168
169 1;
170
171 __END__
172
173 =head1 RELATED MODULES
174
175 L<MARC::Record>
176
177 =head1 TODO
178
179 =over 4
180
181 =item * C<out()> method
182
183 We only handle files for input right now.
184
185 =back
186
187 =cut
188
189 =head1 LICENSE
190
191 This code may be distributed under the same terms as Perl itself. 
192
193 Please note that these modules are not products of or supported by the
194 employers of the various contributors to the code.
195
196 =head1 AUTHOR
197
198 Andy Lester, E<lt>marc@petdance.comE<gt> or E<lt>alester@flr.follett.comE<gt>
199
200 =cut
201