Skip to content

Instantly share code, notes, and snippets.

@ubermichael
Created October 25, 2016 06:46
Show Gist options
  • Save ubermichael/b0a6f0da4b5271f2ddd91a5b570adc22 to your computer and use it in GitHub Desktop.
Save ubermichael/b0a6f0da4b5271f2ddd91a5b570adc22 to your computer and use it in GitHub Desktop.
Based heavily on https://gist.github.com/gmcharlt/f7e3bcb3680a5bf748aacd7e21c13175
```perl
#!/usr/bin/env perl
use 5.014;
use utf8;
use strict;
use warnings;
use version; our $VERSION = qv(0.0_1);
use Data::Dumper; $Data::Dumper::Indent = 1;
use MARC::Charset qw/ marc8_to_utf8 /;
use MARC::Batch;
binmode(STDIN, ':encoding(utf-8)');
binmode(STDOUT, ':encoding(utf-8)');
$|++;
# -------------------------------------
my $dots = 50;
my $line = 70 * $dots;
sub convert {
my $marc = shift;
my $ldr = $marc->leader();
if(substr($ldr, 9, 1) eq 'a') {
return;
} else {
substr($ldr, 9, 1) = 'a';
$marc->leader($ldr);
}
foreach my $field ($marc->fields()) {
next if($field->is_control_field());
my @converted = ();
foreach my $sf ($field->subfields()) {
$sf->[1] = marc8_to_utf8($sf->[1]);
push @converted, @$sf;
}
$field->replace_with(MARC::Field->new(
$field->tag(),
$field->indicator(1),
$field->indicator(2),
@converted
));
}
}
sub main {
my $inPath = shift;
my $outPath = shift;
open(my $in, '<:raw', $inPath) || die "cannot open $inPath: $!\n";
binmode($in);
open(my $out, '>:utf8', $outPath) || die "cannot open $outPath: $!\n";
my $batch = MARC::Batch->new('USMARC', $in);
my $i = 0;
MARC::Charset->ignore_errors(1);
while(my $marc = $batch->next()) {
convert($marc);
print $out $marc->as_usmarc();
$i++;
if($i % $dots == 0) {
print '.';
};
if($i % $line == 0) {
printf("%7d\n", $i);
}
}
printf("%7d\n", $i);
}
# -------------------------------------
&main(@ARGV);
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment