File Coverage

script/marcvalidate
Criterion Covered Total %
statement 41 45 91.1
branch 9 16 56.2
condition 1 3 33.3
subroutine 7 8 87.5
pod n/a
total 58 72 80.5


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 3     3   15772 use strict;
  3         5  
  3         122  
4 3     3   14 use warnings;
  3         5  
  3         282  
5              
6 3         247411 our $VERSION = '0.10';
7              
8 3     3   2248 use Getopt::Long 'HelpMessage';
  3         46483  
  3         17  
9 3     3   1895 use MARC::Parser::RAW;
  3         208984  
  3         183  
10 3     3   5661 use MARC::Parser::XML;
  3         159142  
  3         168  
11 3     3   1882 use MARC::Schema;
  3         14  
  3         259459  
12              
13             GetOptions(
14             'file|f=s' => \my $file,
15             'type|t=s' => \( my $type = 'RAW' ),
16             'schema|s=s' => \my $schema_file,
17 0     0   0 'help|h' => sub { HelpMessage() },
18 3 50       40 ) or HelpMessage();
19              
20 3 100       3050 $file = shift unless defined($file);
21              
22 3 50 33     115 HelpMessage() unless defined $file and -e $file;
23              
24 3         73 my $schema = MARC::Schema->new({file => $schema_file});
25              
26 3         5 my $parser;
27 3 50       17 if ( $type eq 'RAW' ) {
    0          
28 3         51 $parser = MARC::Parser::RAW->new($file);
29             }
30             elsif ( $type eq 'XML' ) {
31 0         0 $parser = MARC::Parser::XML->new($file);
32             }
33             else {
34 0         0 print q{type '$type' not supported. Use 'RAW' or 'XML'};
35             }
36              
37 3         808 my $record_count = 0;
38 3         18 while ( my $record = $parser->next() ) {
39 30         16789 $record_count++;
40 30         110 my $id = _id($record);
41 30         125 my @error = $schema->check($record);
42 30 100       481 if (@error > 0) {
43 3         12 foreach my $error (@error) {
44 3 50       15 if (exists $error->{value}) {
45 3         110 print qq{$id\t$error->{tag}\t$error->{error}\t$error->{value}\n};
46             } else {
47 0         0 print qq{$id\t$error->{tag}\t$error->{error}\t\n};
48             }
49             }
50             }
51             }
52              
53             sub _id {
54 30     30   54 my ($record) = @_;
55 30         59 my ($id) = map { $_->[-1] } grep { $_->[0] eq '001' } @$record;
  30         69  
  549         846  
56 30 50       103 $id = defined $id ? $id : $record_count;
57 30         55 return $id;
58             }
59              
60             __END__