8 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
14 my $conf = {}; # configuration hashref
17 binmode(STDIN, ':utf8');
19 open RENUMBER, '>', $conf->{output};
20 binmode(RENUMBER, ':utf8');
22 foreach my $input ( @ARGV ) {
23 print STDERR "Processing $input, starting record id at ",
24 $conf->{'renumber-from'},"\n";
26 my $batch = MARC::Batch->new('XML', $input);
27 while ( my $record = $batch->next ) {
29 my @warnings = $batch->warnings;
30 print STDERR "WARNINGS: Record $count : ",
31 join(":",@warnings), " : continuing...\n"
34 while ($record->field($conf->{tag}))
35 { $record->delete_field( $record->field($conf->{tag}) ) }
36 my $new_id = $conf->{'renumber-from'} + $count - 1;
37 my $new_id_field = MARC::Field->new( $conf->{tag},
40 $conf->{subfield} => $new_id );
41 $record->append_fields($new_id_field);
42 print RENUMBER $record->as_xml;
43 print STDERR "\rLast record: $count";
45 print STDERR "\rProcessed $count records. Last record id at ",
46 ($conf->{'renumber-from'} + $count - 1), "\n";
52 Performs boring script initialization. Handles argument parsing,
61 # set mode on existing filehandles
62 binmode(STDIN, ':utf8');
64 my $rc = GetOptions( $c,
73 show_help() unless $rc;
74 show_help() if ($c->{help});
76 # set defaults if told to do so
78 $c->{tag} = 903 unless defined $c->{tag};
79 $c->{subfield} = 'a' unless defined $c->{subfield};
80 $c->{output} = 'incoming.renumbered.marc.xml'
81 unless defined $c->{output};
82 } elsif ($c->{incumbent}) {
83 $c->{tag} = 901 unless defined $c->{tag};
84 $c->{subfield} = 'c' unless defined $c->{subfield};
85 $c->{output} = 'incumbent.renumbered.marc.xml'
86 unless defined $c->{output};
89 my @keys = keys %{$c};
90 show_help() unless (@ARGV and @keys);
91 for my $key ('renumber-from', 'tag', 'subfield', 'output')
92 { push @missing, $key unless $c->{$key} }
94 print "Required option: ", join(', ', @missing), " missing!\n";
103 Display usage message when things go wrong
109 Usage is: $0 [REQUIRED ARGS] <filelist>
111 --renumber-from=N -rf First id# of new sequence
112 --tag=N -t Which tag to use
113 --subfield=X -s Which subfield to use
114 --output=<file> -o Output filename
116 --incoming Set -t, -s, -o to incoming defaults
117 --incumbent Set -t, -s, -o to incumbent defaults
119 Example: '$0 --incoming' is equivalent to
120 '$0 -t 903 -s a -o incoming.renumbered.marc.xml'
122 Any number of input files may be specified; one output file will result.