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 in
158       the file, which causes the "while" loop to end. This is a useful idiom
159       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() has
196       aborted prematurely (before the end of the file has been reached). When
197       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() method
247       returns a MARC::Field object which can be used to access the data,
248       indicators, and even the individual subfields. Our next example shows
249       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() method
279       can help you with this as well.  In our previous example's line 11, the
280       field() method was used in a scalar context, since the result was being
281       assigned to the variable $field. However in a list context, field()
282       will return all the fields in the record of that particular type. For
283       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 tag()
323       method which returns the tag number for a particular MARC field, which
324       is useful when you aren't certain what tag you are currently dealing
325       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 to
336       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 methods
413       that allow you to define where the field gets added. These methods are
414       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 is
454       the raw USMARC data for $record. The raw data is then promptly printed
455       to the "OUTPUT" file handle. If you want to output multiple records to
456       a file, simply repeat the process at line 28 for the additional
457       records. Also of note is the "append_fields" method: unlike recipe C1
458       which called the method once for each field added, this recipe
459       demonstrates that "append_fields" can accept multiple arguments.
460
461       Note to the curious: the as_usmarc() method is actually an alias to the
462       MARC::File::USMARC encode() method. Having separate encode() methods is
463       a design feature of the MARC class hierarchy, since it allows
464       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 as_formatted()
472       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 recipe
577       U1 to the end of the record. If you want to preserve a particular
578       order, you can use the insert_fields_before() and insert_fields_after()
579       methods. In order to use these, you need to locate the field you want
580       to insert before or after.  Here is an example (insert_fields_after()
581       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 replace
681       an existing field in the record with a new field that we created. To
682       use replace_with(), you need to retrieve the field you want to replace
683       from a MARC::Record object (line 10), create a new field to replace the
684       existing one with (lines 13-15), and then call the existing field's
685       replace_with() method passing the new field as an argument (lines 18).
686       You must pass replace_with() a valid MARC::Field object.
687
688   Updating subfields and indicators
689       If you'd rather not replace an existing field with a new one, you can
690       also edit the contents of the field itself using the update() method.
691       Let's say you've got a batch of records and want to make sure that the
692       2nd indicator for the 245 field is properly set for titles that begin
693       with 'The' (where the indicator should be '4').
694
695          1   ## Example U5
696          2
697          3   use MARC::Batch;
698          4   my $batch = MARC::Batch->new('USMARC','file.dat');
699          5   open(OUT,'>new.dat') or die $!;
700          6
701          7   while (my $record = $batch->next()) {
702          8
703          9     ## retrieve the 245 record.
704         10     my $field_245 = $record->field('245');
705         11
706         12     ## if we got 245 and it starts with 'The'...
707         13     if ($field_245 and $field_245->as_string() =~ /^The /) {
708         14
709         15       ## if the 2nd indicator isn't 4, update
710         16       if ($field_245->indicator(2) != 4) {
711         17         $field_245->update( ind2 => 4 );
712         18       }
713         19
714         20     }
715         21
716         22     print OUT $record->as_usmarc();
717         23
718         24   }
719
720       In a similar fashion, you can update individual or multiple subfields:
721
722         $field_245->update( a => 'History of the World :', b => 'part 1' );
723
724       But beware, you can only update the first occurrence of a subfield
725       using update(). If you need to do more finer grained updates, you are
726       advised to build a new field and replace the existing field with
727       replace_with().
728
729   Changing a record's leader
730       The above procedure works for fields, but editing the leader requires
731       that you use the leader() method. When called with no arguments,
732       leader() will return the current leader, and when you pass a scalar
733       value as an argument, the leader will be set to this value. This
734       example shows how you might want to update position 6 of a records
735       leader to reflect a computer file.
736
737          1   ## Example U6
738          2
739          3   use MARC::Batch;
740          4   my $batch = MARC::Batch->new('USMARC','file.dat');
741          5   open(OUT,'>new.dat') or die $!;
742          6   my $record = $batch->next();
743          7
744          8   ## get the current leader.
745          9   my $leader = $record->leader();
746         10
747         11   ## replace position 6 with 'm'
748         12   substr($leader,6,1) = 'm';
749         13
750         14   ## update the leader
751         15   $record->leader($leader);
752         16
753         17   ## save the record to a file
754         18   print OUT $record->as_usmarc();
755
756   Modifying fields without indicators
757       MARC::Record and MARC::Field are smart and know that you don't have
758       field indicators with tags less than 010. Here's an example of
759       updating/adding an 005 field to indicate a new transaction time. For a
760       little pizzazz, we use Perl's localtime() to generate the data we need
761       for this field.
762
763          1   ## Example U7
764          2
765          3   use MARC::Batch;
766          4   my $batch = MARC::Batch->new('USMARC','file.dat');
767          5   open(OUT,'>new.dat') or die $!;
768          6
769          7   while (my $record = $batch->next() ) {
770          8
771          9     ## see if there is a 005 field.
772         10     my $field_005 = $record->field('005');
773         11
774         12     ## delete it if we find one.
775         13     $record->delete_field($field_005) if $field_005;
776         14
777         15     ## figure out the contents of our new 005 field.
778         16     my ($sec,$min,$hour,$mday,$mon,$year) = localtime();
779         17     $year += 1900; $mon += 1; # catering to offsets.
780         18     my $datetime = sprintf("%4d%02d%02d%02d%02d%02d.0",
781         19                             $year,$mon,$mday,$hour,$min,$sec);
782         20
783         21     ## create a new 005 field using our new datetime.
784         22     $record->append_fields( MARC::Field->new('005',$datetime) );
785         23
786         24     ## save record to a file.
787         25     print OUT $record->as_usmarc();
788         26
789         27   }
790
791   Reordering subfields
792       You may find yourself in the situation where you would like to
793       programmatically reorder, and possibly modify, subfields in a
794       particular field. For example, imagine that you have a batch of records
795       that have 856 fields which contain subfields z, u, and possibly 3... in
796       any order!  Now imagine that you'd like to standardize the subfield z,
797       and reorder them so that subfield 3 precedes subfield z, which precedes
798       subfield u. This is tricky but can be done in the following manner:
799       read in a record, extract the existing 856 field, build a new 856 field
800       based on the existing one, replace the existing field with your newly
801       created version.
802
803          1   ## Example U8
804          2
805          3   use MARC::Batch;
806          4   my $batch = MARC::Batch->new('USMARC','856.dat');
807          5   open(OUT,'>856_new.dat') or die $!;
808          6
809          7   while (my $record = $batch->next()) {
810          8
811          9     my $existing = $record->field('856');
812         10
813         11     ## make sure 856 exists.
814         12     if ($existing) {
815         13
816         14       ## our ordered subfields.
817         15       my @subfields = ();
818         16
819         17       ## if we have a subfield 3, add it.
820         18       if (defined($existing->subfield('3'))) {
821         19         push(@subfields,'3',$existing->subfield('3'));
822         20       }
823         21
824         22       ## now add subfields z and u.
825         23       push(@subfields,'z','Access restricted',
826         24         'u',$existing->subfield('u'));
827         25
828         26       ## create a new 856.
829         27       my $new = MARC::Field->new(
830         28          856', $existing->indicator(1),
831         29          $existing->indicator(2), @subfields
832         30       );
833         31
834         32       ## replace the existing subfield.
835         33       $existing->replace_with($new);
836         34
837         35     }
838         36
839         37     ## write out the record
840         38     print OUT $record->as_usmarc();
841         39
842         40   }
843
844   Updating subject subfield x to subfield v
845       As a somewhat more complicated example, you may find yourself wanting
846       to update the last subfield x in a 650 field to be a subfield v
847       instead. With the  MARC::Field subfields() and replace_with() methods
848       along with some  fancy footwork this can be done relatively easily.
849
850          1  ## Example U9
851          2
852          3  use MARC::Batch;
853          4
854          5  my $file = shift;
855          6
856          7  my $batch = MARC::Batch->new('USMARC', $file);
857          8  while ( my $record = $batch->next() ) {
858          9
859         10    # go through all 6XX fields in the record.
860         11    foreach my $subject ( $record->field( '6..' ) ) {
861         12
862         13      # extract subfields as an array of array refs.
863         14      my @subfields = $subject->subfields();
864         15
865         16      # setup an array to store our new field.
866         17      my @newSubfields = ();
867         18
868         19      # a flag to indicate that we found an subfield x.
869         20      my $foundX = 0;
870         21
871         22      # use pop() to read the subfields backwards.
872         23      while ( my $subfield = pop( @subfields ) ) {
873         24
874         25        # for convenience, pull out the subfield
875         26        # code and data from  the array ref.
876         27        my ($code,$data) = @$subfield;
877         28
878         29        # if the subfield code is 'x' and
879         30        # we haven't already found one...
880         31        if ( $code eq 'x' and ! $foundX ) {
881         32
882         33          # change to a v.
883         34          $code = 'v';
884         35
885         36          # set flag so we know not to
886         37          # translate any more subfield x.
887         38          $foundX = 1;
888         39
889         40        }
890         41
891         42        # add our (potentially changed) subfield
892         43        # data to our new subfield data array.
893         44        unshift( @newSubfields, $code, $data );
894         45
895         46      }
896         47
897         48      # if we did find a subfield x, then create a new field using our
898         49      # new subfield data, and replace the old one with the new one.
899         50      if ( $foundX ) {
900         51        my $newSubject = MARC::Field->new(
901         52          $subject->tag(),
902         53          $subject->indicator(1),
903         54          $subject->indicator(2),
904         55          @newSubfields
905         56        );
906         57        $subject->replace_with( $newSubject );
907         58      }
908         59
909         60    }
910         61
911         62    # output the potentially changed record as MARC.
912         63    print $record->as_usmarc();
913         64
914         65  }
915

VALIDATING

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

SWOLLEN APPENDICES

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

CONTRIBUTORS

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