sort leader offsets to support marc_clone
[webpac2] / lib / WebPAC / Output / MARC.pm
index d1e817b..89e38fe 100644 (file)
@@ -8,7 +8,6 @@ use base qw/WebPAC::Common/;
 use MARC::Record;
 use MARC::Lint;
 use Data::Dump qw/dump/;
-use Encode qw/from_to decode/;
 
 =head1 NAME
 
@@ -34,7 +33,6 @@ L<WebPAC::Normalize>.
 
   my $marc = new WebPAC::Output::MARC(
        path => '/path/to/output.marc',
-       native_encoding => 'iso-8859-2',
        marc_encoding => 'utf-8',
        lint => 1,
        dump => 0,
@@ -54,19 +52,18 @@ sub new {
                        $log->warn("Can't create MARC::Lint object, linting is disabled");
        }
 
+       $self->{marc_encoding} ||= 'utf-8';
+
        if (my $path = $self->{path}) {
                open($self->{fh}, '>', $path) ||
                        $log->logdie("can't open MARC output $path: $!");
                binmode($self->{fh}, ':utf8');
 
-               $log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', "\n");
+               $log->info("Creating MARC export file $path", $self->{lint} ? ' (with lint)' : '', " encoding ", $self->{marc_encoding}, "\n");
        } else {
                $log->logconfess("new called without path");
        }
 
-       $self->{native_encoding} ||= 'iso-8859-2';
-       $self->{marc_encoding} ||= 'utf-8';
-
        $self ? return $self : return undef;
 }
 
@@ -109,7 +106,6 @@ sub add {
        foreach my $j ( 0 .. $#$fields ) {
                foreach my $i ( 0 .. ( ( $#{$fields->[$j]} - 3 ) / 2 ) ) {
                        my $f = $fields->[$j]->[ ($i * 2) + 4 ];
-                       $f = decode( $self->{native_encoding}, $f );
                        $fields->[$j]->[ ($i * 2) + 4 ] = $f;
                }
        }
@@ -126,7 +122,7 @@ sub add {
 
                my $leader = $marc->leader;
 
-               foreach my $o ( keys %$new_l ) {
+               foreach my $o ( sort { $a <=> $b } keys %$new_l ) {
                        my $insert = $new_l->{$o};
                        $leader = substr($leader, 0, $o) .
                                $insert . substr($leader, $o+length($insert));