NAME

MARC::Record - Perl extension for handling MARC records

VERSION

Version 0.09

SYNOPSIS

  use MARC::Record;

  open( IN, "<", $filename ) or die "Couldn't open $filename: $!\n";
  binmode( IN ); # for the Windows folks
  while ( !eof(IN) ) {
  	my $marc = MARC::Record::next_from_file( *IN );
	die $MARC::Record::ERROR unless $marc;

	# Print the title tag
	print $marc->subfield(245,"a"), "\n";

	# Find any subject tags and print their _a subfields
	for my $subject ( $marc->field( "6XX" ) ) {
		print "\t", $subject->tag, ": ", $subject->subfield("a"), "\n";
	} # for subject
  } # while

  close IN or die "Error closing $filename: $!\n";

DESCRIPTION

Module for handling MARC records as objects, and reading them from USMARC files.

EXPORT

None.

ERROR HANDLING

Any errors generated are stored in $MARC::Record::ERROR. Warnings are kept with the record and accessible in the warnings() method.

METHODS

new()

Base constructor for the class.

new_from_usmarc()

Constructor for handling data from a USMARC file. This function takes care of all the directory parsing & mangling.

Any warnings or coercions can be checked in the warnings() function.

next_from_file(*FILEHANDLE)

Reads the next record from the file handle passed in.

  open( IN, "foo.marc" );
  while ( !eof(IN) ) {
	  my $marc = MARC::Record::next_from_file(*IN);
  } # while
  close IN;

leader([text])

Returns the leader for the record. Sets the leader if text is defined. No error checking is done on the validity of the leader.

update_leader()

If any changes get made to the MARC record, the first 5 bytes of the leader (the length) will be invalid. This function updates the leader with the correct length of the record as it would be if written out to a file.

_set_record_length($)

Internal function for updating the leader's length.

add_fields()

Adds MARC::Field objects to the end of the list. Returns the number of fields added, or undef if there was an error.

There are three ways of calling add_fields() to add data to the record.

1 Create a MARC::Field object and add it
  my $author = MARC::Field->new(
	        100, "1", " ", a => "Arnosky, Jim."
	        );
  $marc->add_fields( $author );
2 Add the data fields directly, and let add_fields() take care of the objectifying.
$marc->add_fields(
      245, "1", "0",
              a => "Raccoons and ripe corn /",
              c => "Jim Arnosky.",
      	);
3 Same as #2 above, but pass multiple fields of data in anonymous lists
  $marc->add_fields(
	[ 250, " ", " ", a => "1st ed." ],
	[ 650, "1", " ", a => "Raccoons." ],
	);

delete_field($field)

Deletes a field from the record.

The field must have been retrieved from the record using the field() method. For example, to delete a 526 tag if it exists:

    my $tag526 = $marc->field( "526" );
    if ( $tag526 ) {
	$marc->delete_field( $tag526 );
    }

delete_field() returns the number of fields that were deleted. This shouldn't be 0 unless you didn't get the tag properly.

fields()

Returns a list of all the fields in the record.

field(tagspec)

Returns a list of tags that match the field specifier, or in scalar context, just the first matching tag.

The field specifier can be a simple number (i.e. "245"), or use the "X" notation of wildcarding (i.e. subject tags are "6XX").

subfield(tag,subfield)

Shortcut method for getting just a subfield for a tag. These are equivalent:

my $title = $marc->field(245)->subfield("a");
my $title = $marc->subfield(245,"a");

If either the field or subfield can't be found, undef is returned.

as_formatted()

Returns a pretty string for printing in a MARC dump.

title()

Returns the title from the 245 tag

author()

Returns the author from the 100, 110 or 111 tag.

_build_tag_directory()

Function for internal use only: Builds the tag directory that gets put in front of the data in a MARC record.

Returns two array references, and a length: The tag directory, and the data fields themselves, and the length of all data, including the Leader that we expect will be added.

as_usmarc()

Returns a string of characters suitable for writing out to a USMARC file, including the leader, directory and all the fields.

warnings()

Returns the warnings that were created when the record was read. These are things like "Invalid indicators converted to blanks".

The warnings are items that you might be interested in, or might not. It depends on how stringently you're checking data. If you're doing some grunt data analysis, you probably don't care.

DESIGN NOTES

A brief discussion of why MARC::Record is done the way it is:

  • It's built for quick prototyping

    One of the areas Perl excels is in allowing the programmer to create easy solutions quickly. MARC::Record is designed along those same lines. You want a program to dump all the 6XX tags in a file? MARC::Record is your friend.

  • It's built for extensibility

    Currently, I'm using MARC::Record for analyzing bibliographic data, but who knows what might happen in the future? MARC::Record needs to be just as adept at authority data, too.

  • It's designed around accessor methods

    I use method calls everywhere, and I expect calling programs to do the same, rather than accessing internal data directly. If you access an object's hash fields on your own, future releases may break your code.

  • It's not built for speed

    One of the tradeoffs in using accessor methods is some overhead in the method calls. Is this slow? I don't know, I haven't measured. I would suggest that if you're a cycle junkie that you use Benchmark.pm to check to see where your bottlenecks are, and then decide if MARC::Record is for you.

SEE ALSO

TODO

  • Incorporate MARC.pm in the distribution.

    Combine MARC.pm and MARC::* into one distribution.

  • Podify MARC.pm

  • Allow regexes across the entire tag

    Imagine something like this:

    my @sears_headings = $marc->tag_grep( /Sears/ );

    (from Mike O'Regan)

  • Insert a field in an arbitrary place in the record

  • Allow deleting a field

      for my $field ( $record->field( "856" ) ) {
    	$record->delete_field( $field ) unless useful($field);
    	} # for

    (from Anne Highsmith hismith@tamu.edu)

  • Modifying an existing field

IDEAS

Ideas are things that have been considered, but nobody's actually asked for.

  • Read from MicroLIF

    Create a new_from_microlif() function that would handle the pretty MicroLIF format. Basically, a reverse of as_microlif().

  • Create multiple output formats.

    These could be ASCII, XML, or MarcMaker.

  • Create a clone of a record based on criteria

LICENSE

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

Andy Lester, <marc@petdance.com> or <alester@flr.follett.com>

1 POD Error

The following errors were encountered while parsing the POD:

Around line 224:

=pod directives shouldn't be over one line long! Ignoring all 3 lines of content