1MARC::Doc::Tutorial(3)User Contributed Perl DocumentationMARC::Doc::Tutorial(3)
2
3
4

NAME

6       MARC::Doc::Tutorial - A documentation-only module for new users of
7       MARC::Record
8

SYNOPSIS

10        perldoc MARC::Doc::Tutorial
11

INTRODUCTION

13   What is MARC?
14       The MAchine Readable Cataloging format was designed by the Library of
15       Congress in the late 1960s in order to allow libraries to convert their
16       card catalogs into a digital format. The advantages of having
17       computerized card catalogs were soon realized, and now MARC is being
18       used by all sorts of libraries around the world to provide computerized
19       access to their collections.  MARC data in transmission format is
20       optimized for processing by computers, so it's not very readable for
21       the normal human. For more about the MARC format, visit the Library of
22       Congress at http://www.loc.gov/marc/
23
24   What is this Tutorial?
25       The document you are reading is a beginners guide to using Perl to
26       processing MARC data, written in the 'cookbook' style. Inside, you will
27       find recipes on how to read, write, update and convert MARC data using
28       the MARC::Record CPAN package. As with any cookbook, you should feel
29       free to dip in at any section and use the recipe you find interesting.
30       If you are new to Perl, you may want to read from the beginning.
31
32       The document you are reading is distributed with the MARC::Record
33       package, however in case you are reading it somewhere else, you can
34       find the latest version at CPAN:
35       http://www.cpan.org/modules/by-module/MARC/. You'll notice that some
36       sections aren't filled in yet, which is a result of this document being
37       a work in progress. If you have ideas for new sections please make a
38       suggestion to perl4lib: https://perl4lib.perl.org/.
39
40   History of MARC on CPAN
41       In 1999, a group of developers began working on MARC.pm to provide a
42       Perl module for working with MARC data. MARC.pm was quite successful
43       since it grew to include many new options that were requested by the
44       Perl/library community.  However, in adding these features the module
45       swiftly outgrew its own clothes, and maintenance and addition of new
46       features became extremely difficult. In addition, as libraries began
47       using MARC.pm to process large MARC data files (>1000 records) they
48       noticed that memory consumption would skyrocket.  Memory consumption
49       became an issue for large batches of records because MARC.pm's object
50       model was based on the 'batch' rather than the record... so each record
51       in the file would often be read into memory. There were ways of getting
52       around this, but they were not obvious. Some effort was made to
53       reconcile the two approaches (batch and record), but with limited
54       success.
55
56       In mid 2001, Andy Lester released MARC::Record and MARC::Field which
57       provided a much simpler and maintainable package for processing MARC
58       data with Perl.  As its name suggests, MARC::Record treats an
59       individual MARC record as the primary Perl object, rather than having
60       the object represent a given set of records. Instead of forking the two
61       projects, the developers agreed to encourage use of the MARC::Record
62       framework, and to work on enhancing MARC::Record rather than extending
63       MARC.pm further. Soon afterwards, MARC::Batch was added, which allows
64       you to read in a large data file without having to worry about memory
65       consumption.
66
67       In Dec., 2004, the MARC::Lint module, an extension to check the
68       validity of MARC records, was removed from the MARC::Record
69       distribution, to become a separately distributed package. This tutorial
70       contains examples for using MARC::Lint.
71
72   Brief Overview of MARC Classes
73       The MARC::Record package is made up of several separate packages. This
74       can be somewhat confusing to people new to Perl, or Object Oriented
75       Programming. However this framework allows easy extension, and is built
76       to support new input/output formats as their need arises. For a good
77       introduction to using the object oriented features of Perl, see the
78       perlboot documentation that came with your version of Perl.
79
80       Here are the packages that get installed with MARC::Record:
81
82       MARC::Batch
83           A convenience class for accessing MARC data contained in an
84           external file.
85
86       MARC::Field
87           An object for representing the indicators and subfields of a single
88           MARC field.
89
90       MARC::Record
91           This primary class represents a MARC record, being a container for
92           multiple MARC::Field objects.
93
94       MARC::Doc::Tutorial
95           This document!
96
97       MARC::File
98           A superclass for representing files of MARC data.
99
100       MARC::File::MicroLIF
101           A subclass of MARC::File for working with data encoded in the
102           MicroLIF format.
103
104       MARC::File::USMARC
105           A subclass of MARC::File for working with data encoded in the
106           USMARC format.
107
108   Help Wanted!
109       It's already been mentioned but it's worth mentioning again:
110       MARC::Doc::Tutorial is a work in progress, and you are encouraged to
111       submit any suggestions for additional recipes via the perl4lib mailing
112       list at https://perl4lib.perl.org/. Also, patches and issue reports are
113       welcome at https://github.com/perl4lib/marc-perl.
114

READING

116   Reading a record from a file
117       Let's say you have a USMARC record in 'file.dat' and you'd like to read
118       in the record and print out its title.
119
120          1   ## Example R1
121          2
122          3   ## create a MARC::Batch object.
123          4   use MARC::Batch;
124          5   my $batch = MARC::Batch->new('USMARC', 'file.dat');
125          6
126          7   ## get a MARC record from the MARC::Batch object.
127          8   ## the $record will be a MARC::Record object.
128          9   my $record = $batch->next();
129         10
130         11   ## print the title contained in the record.
131         12   print $record->title(),"\n";
132
133       Using the distribution's 't/camel.usmarc', your result should be:
134
135         ActivePerl with ASP and ADO / Tobias Martinsson.
136
137   Iterating through a batch file
138       Now imagine that 'file.dat' actually contains multiple records and we
139       want to print the title for each of them. Our program doesn't have to
140       change very much at all: we just need to add a loop around our call to
141       "next()".
142
143          1   ## Example R2
144          2
145          3   ## create a MARC::Batch object.
146          4   use MARC::Batch;
147          5   my $batch = MARC::Batch->new('USMARC','file.dat');
148          6
149          7   while (my $record = $batch->next()) {
150          8
151          9     ## print the title contained in the record.
152         10     print $record->title(),"\n";
153         11
154         12   }
155
156       The call to the "next()" method at line 7 returns the next record from
157       the file. "next()" returns "undef" when there are no more records left
158       in the file, which causes the "while" loop to end. This is a useful
159       idiom for reading in all the records in a file. Your results with
160       'camel.usmarc' should be:
161
162         ActivePerl with ASP and ADO / Tobias Martinsson.
163         Programming the Perl DBI / Alligator Descartes and Tim Bunce.
164         .
165         .
166         .
167         Cross-platform Perl / Eric F. Johnson.
168
169   Checking for errors
170       It is a good idea to get in the habit of checking for errors. MARC/Perl
171       has been designed to help you do this. Calls to "next()" when iterating
172       through a batch file will return "undef" when there are no more records
173       to return...  AND when an error was encountered (see the next recipe to
174       subvert this).  You probably want to make sure that you didn't abruptly
175       stop reading a batch file because of an error.
176
177          1   ## Example R3
178          2
179          3   ## create a MARC::Batch object.
180          4   use MARC::Batch;
181          5   my $batch = MARC::Batch->new('USMARC','file.dat');
182          6
183          7   ## get a marc record from the MARC::Batch object.
184          8   ## $record will be a MARC::Record object.
185          9   while ( my $record = $batch->next() ) {
186         10       print $record->title(),"\n";
187         11   }
188         12
189         13   ## make sure there weren't any problems.
190         14   if ( my @warnings = $batch->warnings() ) {
191         15       print "\nWarnings were detected!\n", @warnings;
192         16   }
193
194       The call to "warnings()" at line 14 will retrieve any warning messages
195       and store them in @warnings. This allows you to detect when "next()"
196       has aborted prematurely (before the end of the file has been reached).
197       When a warning is detected, an explanation is sent to "STDERR". By
198       introducing an error into 'camel.usmarc', we'll receive the following
199       output to "STDOUT":
200
201         Warnings were detected!
202         Invalid indicators "a0" forced to blanks in record 1 for tag 245
203
204   Recovering from errors
205       You may want to keep reading a batch file even after an error has been
206       encountered.  If so, you will want to turn strict mode off using the
207       "strict_off()" method. You can also prevent warnings from being printed
208       to "STDERR" using the "warnings_off()" method. By default, strict is on
209       as a safety precaution to prevent you from using corrupt MARC data.
210       Once off, you can turn both strict and warnings back on again with the
211       "strict_on()" and "warnings_on()" methods.
212
213          1   ## Example R4
214          2
215          3   use MARC::Batch;
216          4   my $batch = MARC::Batch->new('USMARC', 'file.dat');
217          5   $batch->strict_off();
218          6
219          7   while ( my $record = $batch->next() ) {
220          8      print $record->title(),"\n";
221          9   }
222         10
223         11   ## make sure there weren't any problems.
224         12   if ( my @warnings = $batch->warnings() ) {
225         13       print "\nWarnings were detected!\n", @warnings;
226         14   }
227
228       Introducing a second error to the 'camel.usmarc' file gives the
229       following:
230
231          ActivePerl with ASP and ADO / Tobias Martinsson.
232          Programming the Perl DBI / Alligator Descartes and Tim Bunce.
233          .
234          .
235          .
236          Cross-platform Perl / Eric F. Johnson.
237
238          Warnings were detected!
239          Invalid indicators "a0" forced to blanks in record 1 for tag 245
240          Invalid indicators "a0" forced to blanks in record 5 for tag 245
241
242   Looking at a field
243       Our previous examples use MARC::Record's "title()" method to easily
244       access the 245 field, but you will probably want programs that access
245       lots of other MARC fields. MARC::Record's "field()" method gives you
246       complete access to the data found in any MARC field. The "field()"
247       method returns a MARC::Field object which can be used to access the
248       data, indicators, and even the individual subfields. Our next example
249       shows how this is done.
250
251          1   ## Example R5
252          2
253          3   ## open a file.
254          4   use MARC::Batch;
255          5   my $batch = MARC::Batch->new('USMARC','file.dat');
256          6
257          7   ## read a record.
258          8   my $record = $batch->next();
259          9
260         10   ## get the 100 field as a MARC::Field object.
261         11   my $field = $record->field('100');
262         12   print "The 100 field contains: ",$field->as_string(),"\n";
263         13   print "The 1st indicator is ",$field->indicator(1),"\n";
264         14   print "The 2nd indicator is ",$field->indicator(2),"\n";
265         15   print "Subfield d contains: ",$field->subfield('d'),"\n";
266
267       Which results in something like:
268
269         The 100 field contains: Martinsson, Tobias, 1976-
270         The 1st indicator is 1
271         The 2nd indicator is
272         Subfield d contains: 1976-
273
274       As before, use a "while" loop to iterate through all the records in a
275       batch.
276
277   Looking at repeatable fields
278       So how do you retrieve data from repeatable fields? The "field()"
279       method can help you with this as well.  In our previous example's line
280       11, the "field()" method was used in a scalar context, since the result
281       was being assigned to the variable $field. However in a list context,
282       "field()" will return all the fields in the record of that particular
283       type. For example:
284
285          1   ## Example R6
286          2
287          3   use MARC::Batch;
288          4   my $batch = MARC::Batch->new('USMARC','file.dat');
289          5   my $record = $batch->next();
290          6
291          7   ## get all the 650 fields (list context).
292          8   my @fields = $record->field('650');
293          9
294         10   ## examine each 650 field and print it out.
295         11   foreach my $field (@fields) {
296         12     print $field->as_string(),"\n";
297         13   }
298
299       Which prints out the following for the first record of
300       't/camel.usmarc':
301
302         Active server pages.
303         ActiveX.
304
305   Looking at a set of related fields
306       "field()" also allows you to retrieve similar fields using '.' as a
307       wildcard.
308
309          1   ## Example R7
310          2
311          3   use MARC::Batch;
312          4   my $batch = MARC::Batch->new('USMARC','file.dat');
313          5   my $record = $batch->next();
314          6
315          7   # retrieve all title fields in one shot.
316          8   foreach my $field ($record->field('2..')) {
317          9     print $field->tag(),' contains ',$field->as_string(),"\n";
318         10   }
319
320       Notice the shorthand in line 8 which compacts lines 7-13 of our
321       previous example.  Instead of storing the fields in an array, the
322       "field()" still returns a list in the "for" loop. Line 9 uses the
323       "tag()" method which returns the tag number for a particular MARC
324       field, which is useful when you aren't certain what tag you are
325       currently dealing with. Sample output from this recipe:
326
327          245 contains ActivePerl with ASP and ADO / Tobias Martinsson.
328          260 contains New York : John Wiley & Sons, 2000.
329
330       You  can also return all tags for a specific record by using '...'  in
331       "field" (though, see the next recipe).
332
333   Looking at all the fields in a record
334       The last example in this section illustrates how to retrieve all the
335       fields in a record using the "fields()" method. This method is similar
336       to passing '...' as a wildcard (see our previous recipe for alternative
337       access).
338
339          1   ## Example R8
340          2
341          3   use MARC::Batch;
342          4   my $file = MARC::Batch->new('USMARC','file.dat');
343          5   my $record = $batch->next();
344          6
345          7   ## get all of the fields using the fields() method.
346          8   my @fields = $record->fields();
347          9
348         10   ## print out the tag, the indicators and the field contents.
349         11   foreach my $field (@fields) {
350         12     print
351         13       $field->tag(), " ",
352         14       defined $field->indicator(1) ? $field->indicator(1) : "",
353         15       defined $field->indicator(2) ? $field->indicator(2) : "",
354         16       " ", $field->as_string, " \n";
355         17   }
356
357       The above code would print the following for the first record of
358       't/camel.usmarc':
359
360         001  fol05731351
361         003  IMchF
362         .
363         .
364         .
365         300    xxi, 289 p. : ill. ; 23 cm. + 1 computer  laser disc (4 3/4 in.)
366         500    "Wiley Computer Publishing."
367         650  0 Perl (Computer program language)
368         630 00 Active server pages.
369         630 00 ActiveX.
370

CREATING

372       The examples in the Section 1 covered how to read in existing USMARC
373       data in a file. Section 2 will show you how to create a MARC record
374       from scratch.  The techniques in this section would allow you to write
375       programs which create MARC records that could then be loaded into an
376       online catalog, or sent to a third party.
377
378   Creating a record
379       To create a new MARC record, you'll need to first create a MARC::Record
380       object, add a leader (though MARC::Record can create leaders
381       automatically if you don't specifically define one), and then create
382       and add MARC::Field objects to your MARC::Record object. For example:
383
384          1   ## Example C1
385          2
386          3   ## create a MARC::Record object.
387          4   use MARC::Record;
388          5   my $record = MARC::Record->new();
389          6
390          7   ## add the leader to the record. optional.
391          8   $record->leader('00903pam  2200265 a 4500');
392          9
393         10   ## create an author field.
394         11   my $author = MARC::Field->new(
395         12     '100',1,'',
396         13       a => 'Logan, Robert K.',
397         14       d => '1939-'
398         15     );
399         16   $record->append_fields($author);
400         17
401         18   ## create a title field.
402         19   my $title = MARC::Field->new(
403         20     '245','1','4',
404         21       a => 'The alphabet effect /',
405         22       c => 'Robert K. Logan.'
406         23     );
407         24   $record->append_fields($title);
408
409       The key to creating records from scratch is to use "append_fields()",
410       which adds a field to the end of the record. Since each field gets
411       added at the end, it's up to you to order the fields the way you want.
412       "insert_fields_before()" and "insert_fields_after()" are similar
413       methods that allow you to define where the field gets added. These
414       methods are covered in more detail below.
415

WRITING

417       Sections 1 and 2 showed how to read and create USMARC data. Once you
418       know how to read and create, it becomes important to know how to write
419       the USMARC data to disk in order to save your work. In these examples,
420       we will create a new record and save it to a file called 'record.dat'.
421
422   Writing records to a file
423          1   ## Example W1
424          2
425          3   ## create a MARC::Record object.
426          4   use MARC::Record;
427          5   my $record = MARC::Record->new();
428          6
429          7   ## add the leader to the record. optional.
430          8   $record->leader('00903pam  2200265 a 4500');
431          9
432         10   ## create an author field.
433         11   my $author = MARC::Field->new(
434         12     '100',1,'',
435         13       a => 'Logan, Robert K.',
436         14       d => '1939-'
437         15     );
438         16
439         17   ## create a title field.
440         18   my $title = MARC::Field->new(
441         19     '245','1','4',
442         20       a => 'The alphabet effect /',
443         21       c => 'Robert K. Logan.'
444         22     );
445         23
446         24   $record->append_fields($author, $title);
447         25
448         26   ## open a filehandle to write to 'record.dat'.
449         27   open(OUTPUT, '> record.dat') or die $!;
450         28   print OUTPUT $record->as_usmarc();
451         29   close(OUTPUT);
452
453       The "as_usmarc()" method call at line 28 returns a scalar value which
454       is the raw USMARC data for $record. The raw data is then promptly
455       printed to the "OUTPUT" file handle. If you want to output multiple
456       records to a file, simply repeat the process at line 28 for the
457       additional records. Also of note is the "append_fields" method: unlike
458       recipe C1 which called the method once for each field added, this
459       recipe demonstrates that "append_fields" can accept multiple arguments.
460
461       Note to the curious: the "as_usmarc()" method is actually an alias to
462       the MARC::File::USMARC "encode()" method. Having separate "encode()"
463       methods is a design feature of the MARC class hierarchy, since it
464       allows extensions to be built that translate MARC::Record objects into
465       different data formats.
466
467   Debugging with "as_formatted()"
468       Since raw USMARC data isn't very easy for humans to read, it is often
469       useful to be able to see the contents of your MARC::Record object
470       represented in a 'pretty' way for debugging purposes. If you have a
471       MARC::Record object you'd like to pretty-print, use the
472       "as_formatted()" method.
473
474          1   ## Example W2
475          2
476          3   ## create a MARC::Record object.
477          4   use MARC::Record;
478          5   my $record = MARC::Record->new();
479          6
480          7   $record->leader('00903pam  2200265 a 4500');
481          8
482          9   $record->append_fields(
483         10    MARC::Field->new('100','1','', a=>'Logan, Robert K.', d=>'1939-'),
484         11    MARC::Field->new('245','1','4', a=>'The alphabet effect /', c=>'Robert K. Logan.')
485         12   );
486         13
487         14   ## pretty print the record.
488         15   print $record->as_formatted(), "\n";
489
490       This code will pretty print the contents of the newly created record:
491
492         LDR 00903pam  2200265 a 4500
493         100 1  _aLogan, Robert K.
494                _d1939-
495         245 14 _aThe alphabet effect /
496                _cRobert K. Logan.
497
498       Notice on lines 9-12 how you can add a list of new fields by creating
499       MARC::Field objects within a call to "append_fields()". This is yet
500       another shorthand method to those shown in recipes C1 and W1. For more
501       pretty-printing capabilities, try "marcdump()" in our next recipe.
502
503   Debugging with marcdump()
504       If you have written USMARC data to a file (as in recipe W2) and you
505       would like to verify that the data is stored correctly you can use the
506       "marcdump" command line utility that was installed with the
507       MARC::Record package:
508
509        % marcdump record.dat
510        record.dat
511        LDR 00122pam  2200049 a 4500
512        100 1  _aLogan, Robert K.
513               _d1939-
514        245 14 _aThe alphabet effect /
515               _cRobert K. Logan.
516
517         Recs  Errs Filename
518        ----- ----- --------
519            1     0 record.dat
520
521       As you can see, this command results in the record being pretty printed
522       to your screen ("STDOUT") similarly to the "as_formatted" method from
523       recipe W2. It is useful for verifying your USMARC data after it has
524       been stored on disk. More details about debugging are found later in
525       VALIDATING.
526

UPDATING

528       Now that you know how to read, write and create MARC data, you have the
529       tools you need to update or edit exiting MARC data. Updating MARC data
530       is a common task for library catalogers. Sometimes there are huge
531       amounts of records that need to be touched up... and while the touch
532       ups are very detail oriented, they are also highly repetitive. Luckily,
533       computers are tireless, and not very prone to error (assuming the
534       programmer isn't).
535
536       When libraries receive large batches of MARC records for electronic
537       text collections such as NetLibrary, Making of America, or microfiche
538       sets like Early American Imprints, the records are often loaded into an
539       online system and then the system is used to update the records.
540       Unfortunately, not all these systems are created equal, and catalogers
541       have to spend a great deal of time touching up each individual record.
542       An alternative would be to process the records prior to import and
543       then, once in the system, the records would not need editing. This
544       scenario would save a great deal of time for the cataloger who would be
545       liberated to spend their time doing original cataloging... which
546       computers are notably bad at!
547
548   Adding a field
549       Imagine a batch of records in 'file.dat' that you'd like to add local
550       notes (590) to, then saving your changes:
551
552          1   ## Example U1
553          2
554          3   ## create our MARC::Batch object.
555          4   use MARC::Batch;
556          5   my $batch = MARC::Batch->new('USMARC','file.dat');
557          6
558          7   ## open a file handle to write to.
559          8   open(OUT,'>new.dat') or die $!;
560          9
561         10   ## read each record, modify, then print.
562         11   while ( my $record = $batch->next() ) {
563         12
564         13       ## add a 590 field.
565         14       $record->append_fields(
566         15          MARC::Field->new('590','','',a=>'Access provided by Enron.')
567         16       );
568         17
569         18       print OUT $record->as_usmarc();
570         19
571         20   }
572         21
573         22   close(OUT);
574
575   Preserving field order
576       As its name suggests, "append_fields()" will add the 590 field in
577       recipe U1 to the end of the record. If you want to preserve a
578       particular order, you can use the "insert_fields_before()" and
579       "insert_fields_after()" methods. In order to use these, you need to
580       locate the field you want to insert before or after.  Here is an
581       example ("insert_fields_after()" works similarly):
582
583          1   ## Example U2
584          2
585          3   use MARC::Batch;
586          4   my $batch = MARC::Batch->new('USMARC','file.dat');
587          5   open(OUT,'>new.dat') or die $!;
588          6
589          7   ## read in each record.
590          8   while ( my $record = $batch->next() ) {
591          9
592         10       ## find the tag after 590.
593         11       my $before;
594         12       foreach ($record->fields()) {
595         13           $before = $_;
596         14           last if $_->tag() > 590;
597         15       }
598         16
599         17       ## create the 590 field.
600         18       my $new = MARC::Field->new('590','','',a=>'Access provided by Enron.');
601         19
602         20       ## insert our 590 field after the $before.
603         21       $record->insert_fields_before($before,$new);
604         22
605         23       ## and print out the new record.
606         24       print OUT $record->as_usmarc();
607         25
608         26   }
609
610   Deleting a field
611       You can also delete fields that you don't want. But you will probably
612       want to check that the field contains what you expect before deleting
613       it. Let's say Enron has gone out of business and the 590 field needs to
614       be deleted:
615
616          1   ## Example U3
617          2
618          3   use MARC::Batch;
619          4   my $batch = MARC::Batch->new('USMARC','new.dat');
620          5   open(OUT,'>newer.dat') or die $1;
621          6
622          7   while ( my $record = $batch->next() ) {
623          8
624          9     ## get the 590 record.
625         10     my $field = $record->field('590');
626         11
627         12     ## if there is a 590 AND it has the word "Enron"...
628         13     if ($field and $field->as_string() =~ /Enron/i) {
629         14
630         15       ## delete it!
631         16       $record->delete_field($field);
632         17
633         18     }
634         19
635         20     ## output possibly modified record.
636         21     print OUT $record->as_usmarc();
637         22
638         23   }
639
640       The 590 field is retrieved on line 10, but notice how we check that we
641       actually received a valid $field, and that it then contains the word
642       'Enron' before we delete it. You need to pass "delete_field()" a
643       MARC::Field object that can be retrieved with the "field()" method.
644
645   Changing existing fields
646       Perhaps rather than adding or deleting a field, you need to modify an
647       existing field. This is achieved in several steps: first, read in the
648       MARC record you want to update, and then the field you're interested
649       in.  From there, call the field's "update" or "replace_with" methods to
650       modify its contents, and then resave the record. Below is an example of
651       updating existing 590 field's containing the word 'Enron' to indicate
652       that access is now provided through Arthur Andersen:
653
654          1   ## Example U4
655          2
656          3   use MARC::Batch;
657          4   my $batch = MARC::Batch->new('USMARC','new.dat');
658          5   open(OUT,'>newer.dat') or die $1;
659          6
660          7   while ( my $record = $batch->next() ) {
661          8
662          9     ## look for a 590 containing "Enron"...
663         10     my $field = $record->field('590');
664         11     if ($field and $field->as_string =~ /Enron/i) {
665         12
666         13       ## create a new 590 field.
667         14       my $new_field = MARC::Field->new(
668         15         '590','','', a => 'Access provided by Arthur Andersen.' );
669         16
670         17       ## replace existing with our new one.
671         18       $field->replace_with($new_field);
672         19
673         20     }
674         21
675         22     ## output possibly modified record.
676         23     print OUT $record->as_usmarc();
677         24
678         25   }
679
680       In this example, we used MARC::Field's method "replace_with()" to
681       replace an existing field in the record with a new field that we
682       created. To use "replace_with()", you need to retrieve the field you
683       want to replace from a MARC::Record object (line 10), create a new
684       field to replace the existing one with (lines 13-15), and then call the
685       existing field's "replace_with()" method passing the new field as an
686       argument (lines 18). You must pass "replace_with()" a valid MARC::Field
687       object.
688
689   Updating subfields and indicators
690       If you'd rather not replace an existing field with a new one, you can
691       also edit the contents of the field itself using the "update()" method.
692       Let's say you've got a batch of records and want to make sure that the
693       2nd indicator for the 245 field is properly set for titles that begin
694       with 'The' (where the indicator should be '4').
695
696          1   ## Example U5
697          2
698          3   use MARC::Batch;
699          4   my $batch = MARC::Batch->new('USMARC','file.dat');
700          5   open(OUT,'>new.dat') or die $!;
701          6
702          7   while (my $record = $batch->next()) {
703          8
704          9     ## retrieve the 245 record.
705         10     my $field_245 = $record->field('245');
706         11
707         12     ## if we got 245 and it starts with 'The'...
708         13     if ($field_245 and $field_245->as_string() =~ /^The /) {
709         14
710         15       ## if the 2nd indicator isn't 4, update
711         16       if ($field_245->indicator(2) != 4) {
712         17         $field_245->update( ind2 => 4 );
713         18       }
714         19
715         20     }
716         21
717         22     print OUT $record->as_usmarc();
718         23
719         24   }
720
721       In a similar fashion, you can update individual or multiple subfields:
722
723         $field_245->update( a => 'History of the World :', b => 'part 1' );
724
725       But beware, you can only update the first occurrence of a subfield
726       using "update()". If you need to do more finer grained updates, you are
727       advised to build a new field and replace the existing field with
728       "replace_with()".
729
730   Changing a record's leader
731       The above procedure works for fields, but editing the leader requires
732       that you use the "leader()" method. When called with no arguments,
733       "leader()" will return the current leader, and when you pass a scalar
734       value as an argument, the leader will be set to this value. This
735       example shows how you might want to update position 6 of a records
736       leader to reflect a computer file.
737
738          1   ## Example U6
739          2
740          3   use MARC::Batch;
741          4   my $batch = MARC::Batch->new('USMARC','file.dat');
742          5   open(OUT,'>new.dat') or die $!;
743          6   my $record = $batch->next();
744          7
745          8   ## get the current leader.
746          9   my $leader = $record->leader();
747         10
748         11   ## replace position 6 with 'm'
749         12   substr($leader,6,1) = 'm';
750         13
751         14   ## update the leader
752         15   $record->leader($leader);
753         16
754         17   ## save the record to a file
755         18   print OUT $record->as_usmarc();
756
757   Modifying fields without indicators
758       MARC::Record and MARC::Field are smart and know that you don't have
759       field indicators with tags less than 010. Here's an example of
760       updating/adding an 005 field to indicate a new transaction time. For a
761       little pizzazz, we use Perl's "localtime()" to generate the data we
762       need for this field.
763
764          1   ## Example U7
765          2
766          3   use MARC::Batch;
767          4   my $batch = MARC::Batch->new('USMARC','file.dat');
768          5   open(OUT,'>new.dat') or die $!;
769          6
770          7   while (my $record = $batch->next() ) {
771          8
772          9     ## see if there is a 005 field.
773         10     my $field_005 = $record->field('005');
774         11
775         12     ## delete it if we find one.
776         13     $record->delete_field($field_005) if $field_005;
777         14
778         15     ## figure out the contents of our new 005 field.
779         16     my ($sec,$min,$hour,$mday,$mon,$year) = localtime();
780         17     $year += 1900; $mon += 1; # catering to offsets.
781         18     my $datetime = sprintf("%4d%02d%02d%02d%02d%02d.0",
782         19                             $year,$mon,$mday,$hour,$min,$sec);
783         20
784         21     ## create a new 005 field using our new datetime.
785         22     $record->append_fields( MARC::Field->new('005',$datetime) );
786         23
787         24     ## save record to a file.
788         25     print OUT $record->as_usmarc();
789         26
790         27   }
791
792   Reordering subfields
793       You may find yourself in the situation where you would like to
794       programmatically reorder, and possibly modify, subfields in a
795       particular field. For example, imagine that you have a batch of records
796       that have 856 fields which contain subfields z, u, and possibly 3... in
797       any order!  Now imagine that you'd like to standardize the subfield z,
798       and reorder them so that subfield 3 precedes subfield z, which precedes
799       subfield u. This is tricky but can be done in the following manner:
800       read in a record, extract the existing 856 field, build a new 856 field
801       based on the existing one, replace the existing field with your newly
802       created version.
803
804          1   ## Example U8
805          2
806          3   use MARC::Batch;
807          4   my $batch = MARC::Batch->new('USMARC','856.dat');
808          5   open(OUT,'>856_new.dat') or die $!;
809          6
810          7   while (my $record = $batch->next()) {
811          8
812          9     my $existing = $record->field('856');
813         10
814         11     ## make sure 856 exists.
815         12     if ($existing) {
816         13
817         14       ## our ordered subfields.
818         15       my @subfields = ();
819         16
820         17       ## if we have a subfield 3, add it.
821         18       if (defined($existing->subfield('3'))) {
822         19         push(@subfields,'3',$existing->subfield('3'));
823         20       }
824         21
825         22       ## now add subfields z and u.
826         23       push(@subfields,'z','Access restricted',
827         24         'u',$existing->subfield('u'));
828         25
829         26       ## create a new 856.
830         27       my $new = MARC::Field->new(
831         28          856', $existing->indicator(1),
832         29          $existing->indicator(2), @subfields
833         30       );
834         31
835         32       ## replace the existing subfield.
836         33       $existing->replace_with($new);
837         34
838         35     }
839         36
840         37     ## write out the record
841         38     print OUT $record->as_usmarc();
842         39
843         40   }
844
845   Updating subject subfield x to subfield v
846       As a somewhat more complicated example, you may find yourself wanting
847       to update the last subfield x in a 650 field to be a subfield v
848       instead. With the  MARC::Field "subfields()" and "replace_with()"
849       methods along with some  fancy footwork this can be done relatively
850       easily.
851
852          1  ## Example U9
853          2
854          3  use MARC::Batch;
855          4
856          5  my $file = shift;
857          6
858          7  my $batch = MARC::Batch->new('USMARC', $file);
859          8  while ( my $record = $batch->next() ) {
860          9
861         10    # go through all 6XX fields in the record.
862         11    foreach my $subject ( $record->field( '6..' ) ) {
863         12
864         13      # extract subfields as an array of array refs.
865         14      my @subfields = $subject->subfields();
866         15
867         16      # setup an array to store our new field.
868         17      my @newSubfields = ();
869         18
870         19      # a flag to indicate that we found an subfield x.
871         20      my $foundX = 0;
872         21
873         22      # use pop() to read the subfields backwards.
874         23      while ( my $subfield = pop( @subfields ) ) {
875         24
876         25        # for convenience, pull out the subfield
877         26        # code and data from  the array ref.
878         27        my ($code,$data) = @$subfield;
879         28
880         29        # if the subfield code is 'x' and
881         30        # we haven't already found one...
882         31        if ( $code eq 'x' and ! $foundX ) {
883         32
884         33          # change to a v.
885         34          $code = 'v';
886         35
887         36          # set flag so we know not to
888         37          # translate any more subfield x.
889         38          $foundX = 1;
890         39
891         40        }
892         41
893         42        # add our (potentially changed) subfield
894         43        # data to our new subfield data array.
895         44        unshift( @newSubfields, $code, $data );
896         45
897         46      }
898         47
899         48      # if we did find a subfield x, then create a new field using our
900         49      # new subfield data, and replace the old one with the new one.
901         50      if ( $foundX ) {
902         51        my $newSubject = MARC::Field->new(
903         52          $subject->tag(),
904         53          $subject->indicator(1),
905         54          $subject->indicator(2),
906         55          @newSubfields
907         56        );
908         57        $subject->replace_with( $newSubject );
909         58      }
910         59
911         60    }
912         61
913         62    # output the potentially changed record as MARC.
914         63    print $record->as_usmarc();
915         64
916         65  }
917

VALIDATING

919       MARC::Lint, available on CPAN and in cvs on SourceForge, has some extra
920       goodies to allow you to validate records. MARC::Lint provides an
921       extensive battery of tests, and it also provides a framework for adding
922       more.
923
924   Using MARC::Lint
925       Here is an example of using MARC::Lint to generate a list of errors
926       present in a batch of records in a file named 'file.dat':
927
928          1   ## Example V1
929          2
930          3   use MARC::Batch;
931          4   use MARC::Lint;
932          5
933          6   my $batch = MARC::Batch->new('USMARC','file.dat');
934          7   my $linter = MARC::Lint->new();
935          8   my $counter = 0;
936          9
937         10   while (my $record = $batch->next() ) {
938         11
939         12     $counter++;
940         13
941         14     ## feed the record to our linter object.
942         15     $linter->check_record($record);
943         16
944         17     ## get the warnings...
945         18     my @warnings = $linter->warnings();
946         19
947         20     ## output any warnings.
948         21     if (@warnings) {
949         22
950         23       print "RECORD $counter\n";
951         24       print join("\n",@warnings),"\n";
952         25
953         26     }
954         27
955         28   }
956
957       MARC::Lint is quite thorough, and will check the following when
958       validating: presence of a 245 field, repeatability of fields and
959       subfields, valid use of subfield within particular fields, presence of
960       indicators and their values. All checks are based on MARC21
961       bibliographic format.
962
963   Customizing MARC::Lint
964       MARC::Lint makes no claim to check everything that might be wrong with
965       a MARC record. In practice, individual libraries may have their own
966       idea about what is valid or invalid. For example, a library may mandate
967       that all MARC records with an 856 field should have a subfield z that
968       reads "Connect to this resource".
969
970       MARC::Lint does provide a framework for adding rules. It can be done
971       using the object oriented programming technique of inheritance. In
972       short, you can create your own subclass of MARC::Lint, and then use it
973       to validate your records. Here's an example:
974
975          1   ## Example V2
976          2
977          3   ## first, create our own subclass of MARC::Lint.
978          4   ## should be saved in a file called MyLint.pm.
979          5
980          6   package MyLint;
981          7   use base qw(MARC::Lint);
982          8
983          9   ## add a method to check that the 856
984         10   ## fields contain a correct subfield z.
985         11   sub check_856 {
986         12
987         13     ## your method is passed the MARC::Lint
988         14     ## and MARC::Field objects for the record.
989         15     my ($self,$field) = @_;
990         16
991         17     if ($field->subfield('z') ne 'Connect to this resource') {
992         18
993         19       ## add a warning to our lint object.
994         20       $self->warn("856 subfield z must read 'Connect to this resource'.");
995         21
996         22     }
997         23
998         24   }
999
1000       Then create a separate program that uses your subclass to validate your
1001       MARC records. You'll need to make sure your program is able to find
1002       your module (in this case, MyLint.pm)... this can be achieved by
1003       putting both MyLint.pm and the following program in the same directory:
1004
1005          1   ## Example V3
1006          2
1007          3   use MARC::Batch;
1008          4   use MyLint;
1009          5
1010          6   my $linter = MyLint->new();
1011          7   my $batch = MARC::Batch->new('USMARC','file.marc');
1012          8   my $counter = 0;
1013          9
1014         10   while (my $record = $batch->next()) {
1015         11
1016         12     $counter++;
1017         13
1018         14     ## check the record
1019         15     $linter->check_record($record);
1020         16
1021         17     ## get the warnings, and print them out
1022         18     my @warnings = $linter->warnings();
1023         19     if (@warnings) {
1024         20       print "RECORD $counter\n";
1025         21       print join("\n",@warnings),"\n";
1026         22     }
1027         23
1028         24   }
1029
1030       Notice how the call to "check_record()"  at line 15 automatically calls
1031       the "check_record" in MARC::Lint. The property of inheritance is what
1032       makes this happen. $linter is an instance of the MyLint class, and
1033       MyLint inherits from the MARC::Lint class, which allows $linter to
1034       inherit all the functionality of a normal MARC::Lint object plus the
1035       new functionality found in the "check_856" method.
1036
1037       Notice also that we don't have to call "check_856()" directly. The call
1038       to "check_record()" automatically looks for any "check_XXX" methods
1039       that it can call to verify the record. Pretty neat stuff. If you've
1040       added validation checks that you think could be of use to the general
1041       public, please share them on the perl4lib mailing list, or become a
1042       developer and add them to the source!
1043

SWOLLEN APPENDICES

1045       Brian Eno fans might catch this reference to his autobiography which
1046       was comprised of a years worth of diary entries plus extra topics at
1047       the end, and was entitled "A Year With Swollen Appendices". The
1048       following section is a grab bag group of appendices. Many of them are
1049       not filled in yet; this is because they are just ideas... so perhaps
1050       the appendices aren't that swollen yet.  Feel free to suggest new ones,
1051       or to fill these in.
1052
1053   Comparing Collections
1054   Authority Records
1055   URLs
1056   ISBN/ISSNs
1057   Call numbers
1058   Subject headings
1059       Suppose you have a batch of MARC records and you want to extract all
1060       the subject headings, generating a report of how many times each
1061       subject heading appeared in the batch:
1062
1063          1   use MARC::File::USMARC;
1064          2   use constant MAX => 20;
1065          3
1066          4   my %counts;
1067          5
1068          6   my $filename = shift or die "Must specify filename\n";
1069          7   my $file = MARC::File::USMARC->in( $filename );
1070          8
1071          9   while ( my $marc = $file->next() ) {
1072         10       for my $field ( $marc->field("6..") ) {
1073         11           my $heading = $field->subfield('a');
1074         12
1075         13           # trailing whitespace / punctuation.
1076         14           $heading =~ s/[.,]?\s*$//;
1077         15
1078         16           # Now count it.
1079         17           ++$counts{$heading};
1080         18       }
1081         19   }
1082         20   $file->close();
1083         21
1084         22   # Sort the list of headings based on the count of each.
1085         23   my @headings = reverse sort { $counts{$a} <=> $counts{$b} } keys %counts;
1086         24
1087         25   # Take the top N hits...
1088         26   @headings = @headings[0..MAX-1];
1089         27
1090         28   # And print out the results.
1091         29   for my $heading ( @headings ) {
1092         30       printf( "%5d %s\n", $counts{$heading}, $heading );
1093         31   }
1094
1095       Which will generate results like this:
1096
1097         600 United States
1098         140 World War, 1939-1945
1099          78 Great Britain
1100          63 Afro-Americans
1101          61 Indians of North America
1102          58 American poetry
1103          55 France
1104          53 West (U.S.)
1105          53 Science fiction
1106          53 American literature
1107          50 Shakespeare, William
1108          48 Soviet Union
1109          46 Mystery and detective stories
1110          45 Presidents
1111          43 China
1112          40 Frontier and pioneer life
1113          38 English poetry
1114          37 Authors, American
1115          37 English language
1116          35 Japan
1117
1118   HTML
1119   XML
1120   MARCMaker
1121       MARC::File::MARCMaker, available on CPAN and in cvs on SourceForge, is
1122       a subclass of MARC::File for working with MARC 21 data encoded in the
1123       format used by the Library of Congress MARCMaker and MARCBreaker
1124       programs (<http://www.loc.gov/marc/makrbrkr.html>) and MarcEdit ().
1125
1126       An example of a brief record in this format:
1127
1128        =LDR  00314nam  22001215a 4500
1129        =001  ctr00000123\
1130        =003  XX-XxUND
1131        =005  20000613133448.0
1132        =008  051029s2005\\\\xxua\\\\\\\\\\001\0\eng\\
1133        =040  \\$aXX-XxUND$cXX-XxUND
1134        =245  00$aSample of MARCMaker record.
1135        =260  \\$a[United States] :$b[S.n.],$c2005.
1136        =300  \\$a1 p. ;$c28 cm.
1137
1138       The following example converts an ISO2709 format record into MARCMaker
1139       format.
1140
1141          1    ## Example Maker1
1142          2
1143          3    use MARC::Batch;
1144          4    use MARC::File::MARCMaker;
1145          5
1146          6    #mrc indicates ISO2709 format
1147          7    my $mrc_in = 'in.mrc';
1148          8    #mrk indicates MARCMaker format
1149          9    my $mrk_out = 'out.mrk';
1150          10
1151          11   #initialize $batch_mrc as new MARC::Batch object
1152          12   my $batch_mrc = MARC::Batch->new('USMARC', $mrc_in);
1153          13
1154          14   #open mrk (MARCMaker) format output file
1155          15   open (OUTMRK, ">$mrk_out") || die "Cannot open $mrk_out, $!";
1156          16
1157          17   my $rec_count = 0;
1158          18   while (my $record = $batch_mrc->next()) {
1159          19      $rec_count++;
1160          20
1161          21      print OUTMRK MARC::File::MARCMaker->encode($record);
1162          22
1163          23   } # while
1164          24
1165          25   print "$rec_count records processed\n";
1166
1167       The following example shows conversion from MARCMaker format to ISO2709
1168       format.
1169
1170          1    ## Example Maker2
1171          2
1172          3    use MARC::Batch;
1173          4    use MARC::File::MARCMaker;
1174          5
1175          6    #mrk indicates MARCMaker format
1176          7    my $mrk_in = 'in.mrk';
1177          8    #mrc indicates ISO2709 format
1178          9    my $mrc_out = 'out.mrc';
1179          10
1180          11   #initialize $batch_mrk as new MARC::Batch object
1181          12   my $batch_mrk = MARC::Batch->new( 'MARCMaker', $mrk_in);
1182          13
1183          14   #open mrc (ISO2709) format output file
1184          15   open (OUTMRC, ">$mrc_out") || die "Cannot open $mrc_out, $!";
1185          16
1186          17   my $rec_count = 0;
1187          18   while (my $record = $batch_mrk->next()) {
1188          19      $rec_count++;
1189          20
1190          21      print OUTMRC $record->as_usmarc();
1191          22
1192          23   } # while
1193          24
1194          25   print "$rec_count records processed\n";
1195
1196   Excel
1197   Z39.50
1198       Chris Biemesderfer was kind enough to contribute a short example of how
1199       to use MARC::Record in tandem with Net::Z3950.  Net::Z3950 is a CPAN
1200       module which provides an easy to use interface to the Z39.50 protocol
1201       so that you can write programs that retrieve records from bibliographic
1202       database around the world.
1203
1204       Chris' program is a command line utility which you run like so:
1205
1206         ./zm.pl 0596000278
1207
1208       where 0596000278 is an ISBN (for the 3rd edition of the Camel
1209       incidentally).  The program will query the Library of Congress Z39.50
1210       server for the ISBN, and dump out the retrieved MARC record on the
1211       screen. The program is designed to lookup multiple ISBNs if you
1212       separate them with a space.  This is just an example showing what is
1213       possible.
1214
1215          1   #!/usr/bin/perl -w
1216          2
1217          3   # GET-MARC-ISBN -- Get MARC records by ISBN from a Z39.50 server
1218          4
1219          5   use strict;
1220          6   use Carp;
1221          7   use Net::Z3950;
1222          8   use MARC::Record;
1223          9
1224         10   exit if ($#ARGV < 0);
1225         11
1226         12   # We handle multiple ISBNs in the same query by assembling a
1227         13   # (potentially very large) search string with Prefix Query Notation
1228         14   # that ORs the ISBN-bearing attributes.
1229         15   #
1230         16   # For purposes of automation, we want to request batches of many MARC
1231         17   # records.  I am not a Z39.50 weenie, though, and I don't know
1232         18   # offhand if there is a limit on how big a PQN query can be...
1233         19
1234         20   my $zq = "\@attr 1=7 ". pop();
1235         21   while (@ARGV) { $zq = '@or @attr 1=7 '. pop() ." $zq" }
1236         22
1237         23   ## HERE IS THE CODE FOR Z3950 REC RETRIEVAL
1238         24   # Set up connection management structures, connect
1239         25   # to the server, and submit the Z39.50 query.
1240         26
1241         27   my $mgr = Net::Z3950::Manager->new( databaseName => 'voyager' );
1242         28   $mgr->option( elementSetName => "f" );
1243         29   $mgr->option( preferredRecordSyntax => Net::Z3950::RecordSyntax::USMARC );
1244         30
1245         31   my $conn = $mgr->connect('z3950.loc.gov', '7090');
1246         32   croak "Unable to connect to server" if !defined($conn);
1247         33
1248         34   my $rs = $conn->search($zq);
1249         35
1250         36   my $numrec = $rs->size();
1251         37   print STDERR "$numrec record(s) found\n";
1252         38
1253         39   for (my $ii = 1; $ii <= $numrec; $ii++) {
1254         40
1255         41       # Extract MARC records from Z3950
1256         42       # result set, and load MARC::Record.
1257         43       my $zrec = $rs->record($ii);
1258         44       my $mrec = MARC::Record->new_from_usmarc($zrec->rawdata());
1259         45       print $mrec->as_formatted, "\n\n";
1260         46
1261         47   }
1262
1263   Databases
1264       Here's a script that will do a Z39.50 query (using Chris Biemesderfer's
1265       zm.pl as a model), get a MARC record back, and store it as a binary
1266       blob in a MySQL table of this structure:
1267
1268        +---------------+---------------+------+-----+---------+----------------+
1269        | Field         | Type          | Null | Key | Default | Extra          |
1270        +---------------+---------------+------+-----+---------+----------------+
1271        | TitleID       | int(7)        |      | PRI | NULL    | auto_increment |
1272        | RecLastMod    | timestamp(14) | YES  |     | NULL    |                |
1273        | ISSN          | text          | YES  |     | NULL    |                |
1274        | RawMARCRecord | blob          | YES  |     | NULL    |                |
1275        +---------------+---------------+------+-----+---------+----------------+
1276
1277          1 #!/usr/bin/perl -w
1278          2
1279          3 # Script that reads in a file of ISSNs, queries a Z39.50 server,
1280          4 # and stores resulting records in a database. Limitations: Only
1281          5 # stores 1 records per ISSN.
1282          6 # Last updated 2004-09-08 Mark Jordan, mjordan@sfu.ca
1283          7
1284          8 use strict;
1285          9 use Carp;
1286         10 use Net::Z3950;
1287         11 use MARC::Record;
1288         12 use DBI;
1289         13
1290         14 # DB connection settings
1291         15 my $host = "somehost";
1292         16 my $user = "someuser";
1293         17 my $password = "somepass";
1294         18 my $database = "somedb";
1295         19
1296         20 # Input file (one ISSS/line)
1297         21 my $InputFile = $ARGV[0];
1298         22
1299         23 # Prepare list of ISSNs to search
1300         24 my @ISSNs;
1301         25 open (INPUT, "< $InputFile") or die "Can't find input file\n";
1302         26 while (<INPUT>) { chomp $_; push (@ISSNs, $_); }
1303         27 close INPUT;
1304         28
1305         29
1306         30 # Set up connection management structures, connect to the server,
1307         31 # and submit the Z39.50 query.
1308         32 my $mgr = Net::Z3950::Manager->new( databaseName => 'voyager' );
1309         33 $mgr->option( elementSetName => "f" );
1310         34 $mgr->option( preferredRecordSyntax => Net::Z3950::RecordSyntax::USMARC );
1311         35 my $conn = $mgr->connect('z3950.loc.gov', '7090');
1312         36 croak "Unable to connect to server" if !defined($conn);
1313         37
1314         38
1315         39 my $handle = DBI->connect("DBI:mysql:$database:$host","$user","$password")
1316         40         or die $DBI::errstr;
1317         41
1318         42 foreach my $ISSN (@ISSNs) {
1319         43         my $zq = "\@attr 1=8 ". $ISSN;
1320         44         my $rs = $conn->search($zq);
1321         45         my $numrec = $rs->size();
1322         46         if ($numrec == 0) {
1323         47             print "Record for ISSN $ISSN not found, moving to next ISSN...\n";
1324         48             next;
1325         49         } else {
1326         50            # Extract MARC record from the result set, and invoke MARC::Record
1327         51            my $zrec = $rs->record(1);
1328         52            my $mrec = MARC::Record->new_from_usmarc($zrec->rawdata());
1329         53            my $rawdata = $zrec->rawdata();
1330         54            $rawdata = $handle->quote ($rawdata);
1331         55            # Add to db
1332         56            my $SQL = "insert into Titles values (NULL,NULL,'$ISSN',$rawdata)";
1333         57            my $cursor = $handle->prepare($SQL);
1334         58            $cursor->execute;
1335         59            print "Record for ISSN $ISSN added to database...\n";
1336         60            $cursor->finish;
1337         61         }
1338         62 }
1339         63 $handle->disconnect;
1340         64
1341         65 __END__
1342
1343       If you want to pull records out of the same database and do something
1344       with them, here's a template script:
1345
1346          1 #!/usr/bin/perl -w
1347          2
1348          3 # Script that gets MARC records (in blobs) from a database.
1349          4 # Last updated 2004-09-08 Mark Jordan, mjordan@sfu.ca
1350          5
1351          6 use strict;
1352          7 use MARC::Record;
1353          8 use DBI;
1354          9
1355         10 # DB connection settings
1356         11 my $mysql_host = "somehost";
1357         12 my $mysql_user = "someuser";
1358         13 my $mysql_password = "somepass*";
1359         14 my $mysql_database = "somedb";
1360         15
1361         16
1362         17 my $handle = DBI->connect("DBI:mysql:$mysql_database:$mysql_host",
1363         18         "$mysql_user","$mysql_password") or die $DBI::errstr;
1364         19
1365         20 my $SQL = "select * from Titles";
1366         21 my $cursor = $handle->prepare($SQL);
1367         22 $cursor->execute;
1368         23
1369         24 while (my @Records = $cursor->fetchrow_array) {
1370         25         my $RawMARC = $Records[3];
1371         26         my $mrec = MARC::Record->new_from_usmarc($RawMARC);
1372         27         # Print out the title
1373         28         print $mrec->title , "\n";
1374         29 }
1375         30
1376         31 $cursor->finish;
1377         32 $handle->disconnect;
1378         33
1379         34 __END__
1380
1381   Procite/Endnote

CONTRIBUTORS

1383       Many thanks to all the contributors who have made this document
1384       possible.
1385
1386       ·   Bryan Baldus <eijabb@cpan.org>
1387
1388       ·   Chris Biemesderfer <chris@seagoat.com>
1389
1390       ·   Morbus Iff <morbus@disobey.com>
1391
1392       ·   Mark Jordan <mjordan@sfu.ca>
1393
1394       ·   Andy Lester <andy@petdance.com>
1395
1396       ·   Christopher Morgan <morgan@acm.org>
1397
1398       ·   Shashi Pinheiro <SPinheiro@utsa.edu>
1399
1400       ·   Jackie Shieh <jshieh@umich.edu>
1401
1402       ·   Ed Summers <ehs@pobox.com>
1403
1404
1405
1406perl v5.28.0                      2017-05-24            MARC::Doc::Tutorial(3)
Impressum