1st scripts for MARC-DB.
[koha.git] / marc / File.pm
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 5.6.0;
10 use strict;
11 use integer;
12 use vars qw( $VERSION $ERROR );
13
14 =head1 VERSION
15
16 Version 0.93
17
18     $Id$
19
20 =cut
21
22 our $VERSION = '0.93';
23
24 =head1 SYNOPSIS
25
26     use MARC::File::USMARC;
27
28     my $file = MARC::File::USMARC->in( $filename );
29     
30     while ( my $marc = $file->next() ) {
31         # Do something
32     }
33     $file->close();
34     undef $file;
35
36 =head1 EXPORT
37
38 None.  
39
40 =head1 METHODS
41
42 =head2 in()
43
44 Opens a file for input.
45
46 =cut
47
48 sub in {
49     my $class = shift;
50     my $filename = shift;
51
52     my $self = {
53         filename => $filename,
54     };
55
56     bless $self, $class;
57
58     if ( !open( $self->{fh}, "<", $filename ) ) {
59         undef $self;
60         $MARC::File::ERROR = "Couldn't open $filename: $!";
61     }
62
63     return $self;
64 } # new()
65
66 sub indata {
67     my $class = shift;
68     my $data = shift;
69
70     my $self = {
71         fh => '',
72         data => $data,
73         pointer => 0,
74     };
75
76     bless $self, $class;
77
78 #    if ( !open( $self->{fh}, "<", $filename ) ) {
79 #       undef $self;
80 #       $MARC::File::ERROR = "Couldn't open $filename: $!";
81 #    }
82
83     return $self;
84 } # new()
85
86 sub out {
87     die "Not yet written";
88 }
89
90 =head2 next()
91
92 Reads the next record from the file handle passed in.
93
94 =cut
95
96 sub next {
97     my $self = shift;
98
99     my $rec = $self->_next();
100
101     return $rec ? $self->decode($rec) : undef;
102 }
103
104 =head2 skip
105
106 Skips over the next record in the file.  Same as C<next()>,
107 without the overhead of parsing a record you're going to throw away
108 anyway.
109
110 Returns 1 or undef.
111
112 =cut
113
114 sub skip {
115     my $self = shift;
116
117     my $rec = $self->_next();
118
119     return $rec ? 1 : undef;
120 }
121
122 sub close {
123     my $self = shift;
124
125     close( $self->{fh} );
126     delete $self->{fh};
127
128     return;
129 }
130
131 sub _unimplemented() {
132     my $self = shift;
133     my $method = shift;
134
135     warn "Method $method must be overridden";
136 }
137
138 sub write   { $_[0]->_unimplemented("write"); }
139 sub decode  { $_[0]->_unimplemented("decode"); }
140
141 # NOTE: _gripe can be called as an object method, or not.  Your choice.
142 sub _gripe(@) {
143     if ( @_ ) {
144         shift if ref($_[0]) =~ /^MARC::File/;   # Skip first parm if it's a $self
145         $ERROR = join( "", @_ );
146     }
147
148     return undef;
149 }
150
151 1;
152
153 __END__
154
155 =head1 RELATED MODULES
156
157 L<MARC::Record>
158
159 =head1 TODO
160
161 =over 4
162
163 =item * C<out()> method
164
165 We only handle files for input right now.
166
167 =back
168
169 =cut
170
171 =head1 LICENSE
172
173 This code may be distributed under the same terms as Perl itself. 
174
175 Please note that these modules are not products of or supported by the
176 employers of the various contributors to the code.
177
178 =head1 AUTHOR
179
180 Andy Lester, E<lt>marc@petdance.comE<gt> or E<lt>alester@flr.follett.comE<gt>
181
182 =cut
183