ادارہ پاکستان براےء آزاد مصدر

Pakistan Open Source Organization


MARC / PERL: Koha Integrated Library System

MARC Tutorial

CREATING

The examples in the Section 1 covered how to read in existing USMARC data in a file. Section 2 will show you how to create a MARC record from scratch. The techniques in this section would allow you to write programs which create MARC records that could then be loaded into an online catalog, or sent to a third party.

Creating a record

To create a new MARC record, you'll need to first create a MARC::Record object, add a leader (though MARC::Record can create leaders automatically if you don't specifically define one), and then create and add MARC::Field objects to your MARC::Record object. For example:

1 ## Example C1
2
3 ## create a MARC::Record object.
4 use MARC::Record;
5 my $record = MARC::Record->new();
6
7 ## add the leader to the record. optional.
8 $record->leader('00903pam     2200265 a 4500');
9
10 ## create an author field.
11 my $author = MARC::Field->new(
12 '100',1,'',
13 a => 'Logan, Robert K.',
14 d => '1939-'
15 );
16 $record->append_fields($author);
17
18 ## create a title field.
19 my $title = MARC::Field->new(
20 '245','1','4',
21 a => 'The alphabet effect /',
22 c => 'Robert K. Logan.'
23 );
24 $record->append_fields($title);

The key to creating records from scratch is to use append_fields(), which adds a field to the end of the record. Since each field gets added at the end, it's up to you to order the fields the way you want. insert_fields_before() and insert_fields_after() are similar methods that allow you to define where the field gets added. These methods are covered in more detail below.

WRITING

Sections 1 and 2 showed how to read and create USMARC data. Once you know how to read and create, it becomes important to know how to write the USMARC data to disk in order to save your work. In these examples, we will create a new record and save it to a file called 'record.dat'.

Writing records to a file

1 ## Example W1
2
3 ## create a MARC::Record object.
4 use MARC::Record;
5 my $record = MARC::Record->new();
6
7 ## add the leader to the record. optional.
8 $record->leader('00903pam 2200265 a 4500');
9
10 ## create an author field.
11 my $author = MARC::Field->new(
12 '100',1,'',
13 a => 'Logan, Robert K.',
14 d => '1939-'
15 );
16
17 ## create a title field.
18 my $title = MARC::Field->new(
19 '245','1','4',
20 a => 'The alphabet effect /',
21 c => 'Robert K. Logan.'
22 );
23
24 $record->append_fields($author, $title);
25
26 ## open a filehandle to write to 'record.dat'.
27 open(OUTPUT, '> record.dat') or die $!;
28 print OUTPUT $record->as_usmarc();
29 close(OUTPUT);

The as_usmarc() method call at line 28 returns a scalar value which is the raw USMARC data for$record. The raw data is then promptly printed to the OUTPUT file handle. If you want to outputmultiple records to a file, simply repeat the process at line 28 for the additional records. Also of note isthe append_fieldsmethod: unlike recipe C1 which called the method once for each field added, thisrecipe demonstrates that append_fieldscan accept multiple arguments. Note to the curious: the as_usmarc() method is actually an alias to the MARC::File::USMARCencode() method. Having separate encode() methods is a design feature of the MARC classhierarchy, since it allows extensions to be built that translate MARC::Record objects into different dataformats.