MARC::File - Base class for files of MARC records


MARC-Record documentation Contained in the MARC-Record distribution.

Index


Code Index:

NAME

Top

MARC::File - Base class for files of MARC records

SYNOPSIS

Top

    use MARC::File::USMARC;

    # If you have werid control fields...
    use MARC::Field;
    MARC::Field->allow_controlfield_tags('FMT', 'LDX');    

    my $file = MARC::File::USMARC->in( $filename );

    while ( my $marc = $file->next() ) {
        # Do something
    }
    $file->close();
    undef $file;

EXPORT

Top

None.

METHODS

Top

in()

Opens a file for import. Ordinarily you will use MARC::File::USMARC or MARC::File::MicroLIF to do this.

    my $file = MARC::File::USMARC->in( 'file.marc' );

Returns a MARC::File object, or undef on failure. If you encountered an error the error message will be stored in $MARC::File::ERROR.

Optionally you can also pass in a filehandle, and MARC::File. will "do the right thing".

    my $handle = IO::File->new( 'gunzip -c file.marc.gz |' );
    my $file = MARC::File::USMARC->in( $handle );

next( [\&filter_func] )

Reads the next record from the file handle passed in.

The $filter_func is a reference to a filtering function. Currently, only USMARC records support this. See MARC::File::USMARC's decode() function for details.

Returns a MARC::Record reference, or undef on error.

skip()

Skips over the next record in the file. Same as next(), without the overhead of parsing a record you're going to throw away anyway.

Returns 1 or undef.

warnings()

Simlilar to the methods in MARC::Record and MARC::Batch, warnings() will return any warnings that have accumulated while processing this file; and as a side-effect will clear the warnings buffer.

close()

Closes the file, both from the object's point of view, and the actual file.

write()

Writes a record to the output file. This method must be overridden in your subclass.

decode()

Decodes a record into a USMARC format. This method must be overridden in your subclass.

RELATED MODULES

Top

TODO

Top

* out() method

We only handle files for input right now.

LICENSE

Top

This code may be distributed under the same terms as Perl itself.

Please note that these modules are not products of or supported by the employers of the various contributors to the code.

AUTHOR

Top

Andy Lester, <andy@petdance.com>


MARC-Record documentation Contained in the MARC-Record distribution.
package MARC::File;

use strict;
use integer;

use vars qw( $ERROR );

sub in {
    my $class = shift;
    my $arg = shift;
    my ( $filename, $fh );

    ## if a valid filehandle was passed in
    my $ishandle = do { no strict; defined fileno($arg); };
    if ( $ishandle ) {
        $filename = scalar( $arg );
        $fh = $arg;
    }

    ## otherwise check if it's a filename, and
    ## return undef if we weren't able to open it
    else {
        $filename = $arg;
        $fh = eval { local *FH; open( FH, $arg ) or die; *FH{IO}; };
        if ( $@ ) {
            $MARC::File::ERROR = "Couldn't open $filename: $@";
            return;
        }
    }

    my $self = {
        filename    => $filename,
        fh          => $fh,
        recnum      => 0,
        warnings    => [],
    };

    return( bless $self, $class );

} # new()

sub out {
    die "Not yet written";
}

sub next {
    my $self = shift;
    $self->{recnum}++;
    my $rec = $self->_next() or return;
    return $self->decode($rec, @_);
}

sub skip {
    my $self = shift;
    my $rec = $self->_next() or return;
    return 1;
}

sub warnings {
    my $self = shift;
    my @warnings = @{ $self->{warnings} };
    $self->{warnings} = [];
    return(@warnings);
}

sub close {
    my $self = shift;
    close( $self->{fh} );
    delete $self->{fh};
    delete $self->{filename};
    return;
}

sub _unimplemented() {
    my $self = shift;
    my $method = shift;
    warn "Method $method must be overridden";
}

sub write   { $_[0]->_unimplemented("write"); }
sub decode  { $_[0]->_unimplemented("decode"); }

# NOTE: _warn must be called as an object method

sub _warn {
    my ($self,$warning) = @_;
    push( @{ $self->{warnings} }, "$warning in record ".$self->{recnum} );
    return( $self );
}

# NOTE: _gripe can be called as an object method, or not.  Your choice.
# NOTE: it's use is now depracated use _warn instead
sub _gripe(@) {
    my @parms = @_;
    if ( @parms ) {
        my $self = shift @parms;

        if ( ref($self) =~ /^MARC::File/ ) {
            push( @parms, " at byte ", tell($self->{fh}) )
                if $self->{fh};
            push( @parms, " in file ", $self->{filename} ) if $self->{filename};
        } else {
            unshift( @parms, $self );
        }

        $ERROR = join( "", @parms );
        warn $ERROR;
    }

    return;
}

1;

__END__