8 #use MARC::File::XML ( BinaryEncoding => 'utf-8' );
12 my $conf = {}; # configuration hashref
15 binmode(STDIN, ':utf8');
17 open RENUMBER, '>', $conf->{output};
18 binmode(RENUMBER, ':utf8');
20 foreach my $input ( @ARGV ) {
21 print STDERR "Processing $input, starting record id at ",
22 $conf->{'renumber-from'},"\n";
24 my $batch = MARC::Batch->new('XML', $input);
25 while ( my $record = $batch->next ) {
27 my @warnings = $batch->warnings;
28 print STDERR "WARNINGS: Record $count : ",
29 join(":",@warnings), " : continuing...\n"
32 while ($record->field($conf->{tag}))
33 { $record->delete_field( $record->field($conf->{tag}) ) }
34 my $new_id = $conf->{'renumber-from'} + $count - 1;
35 my $new_id_field = MARC::Field->new( $conf->{tag},
38 $conf->{subfield} => $new_id );
39 $record->append_fields($new_id_field);
40 print RENUMBER $record->as_xml;
42 print STDERR "Processed $count records. Last record id at ",
43 ($conf->{'renumber-from'} + $count - 1), "\n";
49 Performs boring script initialization. Handles argument parsing,
58 # set mode on existing filehandles
59 binmode(STDIN, ':utf8');
61 my $rc = GetOptions( $c,
70 show_help() unless $rc;
71 show_help() if ($c->{help});
73 # set defaults if told to do so
75 $c->{tag} = 903 unless defined $c->{tag};
76 $c->{subfield} = 'a' unless defined $c->{subfield};
77 $c->{output} = 'incoming.renumbered.marc.xml'
78 unless defined $c->{output};
79 } elsif ($c->{incumbent}) {
80 $c->{tag} = 901 unless defined $c->{tag};
81 $c->{subfield} = 'c' unless defined $c->{subfield};
82 $c->{output} = 'incumbent.renumbered.marc.xml'
83 unless defined $c->{output};
86 my @keys = keys %{$c};
87 show_help() unless (@ARGV and @keys);
88 for my $key ('renumber-from', 'tag', 'subfield', 'output')
89 { push @missing, $key unless $c->{$key} }
91 print "Required option: ", join(', ', @missing), " missing!\n";
100 Display usage message when things go wrong
106 Usage is: $0 [REQUIRED ARGS] <filelist>
108 --renumber-from=N -rf First id# of new sequence
109 --tag=N -t Which tag to use
110 --subfield=X -s Which subfield to use
111 --output=<file> -o Output filename
113 --incoming Set -t, -s, -o to incoming defaults
114 --incumbent Set -t, -s, -o to incumbent defaults
116 Example: '$0 --incoming' is equivalent to
117 '$0 -t 903 -s a -o incoming.renumbered.marc.xml'
119 Any number of input files may be specified; one output file will result.