Handle errors in counter.
authorRobin H. Johnson <rjohnson@sitka.bclibraries.ca>
Mon, 31 Oct 2011 07:48:04 +0000 (00:48 -0700)
committerRobin H. Johnson <rjohnson@sitka.bclibraries.ca>
Mon, 31 Oct 2011 07:48:04 +0000 (00:48 -0700)
marc_export_custom/print901c.pl

index c45b8ad..70e3fd2 100755 (executable)
@@ -2,12 +2,33 @@
 use MARC::Batch;
 use strict;
 use warnings;
 use MARC::Batch;
 use strict;
 use warnings;
-
+use Encode qw(:fallback_all);
 
 my $batch = MARC::Batch->new( 'USMARC', @ARGV );
 
 my $batch = MARC::Batch->new( 'USMARC', @ARGV );
+my $oldencoding = $PerlIO::encoding::fallback;
 $batch->strict_off();
 $batch->warnings_off();
 
 $batch->strict_off();
 $batch->warnings_off();
 
-while ( my $marc = $batch->next ) {
-    print $marc->subfield(901,"c"), "\n";
-}
+my ($count_raw, $count_attempted, $count_901c) = (0, 0, 0);
+eval {
+       while ( my $marc = $batch->next ) {
+           $PerlIO::encoding::fallback = Encode::WARN_ON_ERR | Encode::PERLQQ;
+           $count_attempted++;
+           eval {
+               my $field_901c = $marc->subfield(901,"c");
+               if(defined $field_901c) {
+                       chomp $field_901c;
+                       $count_901c++ if ($field_901c =~ /^.+$/);
+                       printf "%s\n",$field_901c;
+               }
+               $count_raw++;
+           };
+           warn "Inner: $@" if $@;
+       }
+};
+warn "Outer: $@" if $@;
+
+$PerlIO::encoding::fallback = $oldencoding;
+printf STDERR "Count (attempted): %d\n",$count_attempted;
+printf STDERR "Count (raw): %d\n",$count_raw;
+printf STDERR "Count (901c): %d\n",$count_901c;