File Coverage

blib/lib/App/MARC/Record/Stats.pm
Criterion Covered Total %
statement 46 74 62.1
branch 1 14 7.1
condition 3 6 50.0
subroutine 13 14 92.8
pod 2 2 100.0
total 65 110 59.0


line stmt bran cond sub pod time code
1             package App::MARC::Record::Stats;
2              
3 4     4   133390 use strict;
  4         7  
  4         154  
4 4     4   19 use warnings;
  4         6  
  4         220  
5              
6 4     4   2429 use Class::Utils qw(set_params);
  4         57894  
  4         85  
7 4     4   299 use English;
  4         8  
  4         22  
8 4     4   1666 use Error::Pure qw(err);
  4         7  
  4         174  
9 4     4   3930 use Getopt::Std;
  4         8013  
  4         271  
10 4     4   22 use List::Util 1.33 qw(none);
  4         68  
  4         226  
11 4     4   2362 use MARC::File::XML (BinaryEncoding => 'utf8', RecordFormat => 'MARC21');
  4         711798  
  4         34  
12 4     4   2555 use MARC::Record::Stats;
  4         21420  
  4         172  
13 4     4   31 use Readonly;
  4         8  
  4         288  
14 4     4   1834 use Unicode::UTF8 qw(encode_utf8);
  4         2906  
  4         2864  
15              
16             Readonly::Array our @INPUT_FORMATS => qw(xml);
17              
18             our $VERSION = 0.02;
19              
20             # Constructor.
21             sub new {
22 5     5 1 765464 my ($class, @params) = @_;
23              
24             # Create object.
25 5         17 my $self = bless {}, $class;
26              
27             # Process parameters.
28 5         29 set_params($self, @params);
29              
30             # Object.
31 4         41 return $self;
32             }
33              
34             # Run.
35             sub run {
36 3     3 1 5 my $self = shift;
37              
38             # Process arguments.
39 3         22 $self->{'_opts'} = {
40             'h' => 0,
41             'i' => 'xml',
42             };
43 3 50 66     15 if (! getopts('hi:', $self->{'_opts'}) || @ARGV < 1
      33        
44             || $self->{'_opts'}->{'h'}) {
45              
46 3         444 print STDERR "Usage: $0 [-h] [-i format] [--version] marc_file\n";
47 3         95 print STDERR "\t-h\t\tPrint help.\n";
48 3         88 print STDERR "\t-i format\tInput MARC format. Possible formats is xml (default values is xml).\n";
49 3         46 print STDERR "\t--version\tPrint version.\n";
50 3         36 print STDERR "\tmarc_file\tMARC file.\n";
51 3         17 return 1;
52             }
53 0           $self->{'_marc_file'} = shift @ARGV;
54              
55             # Check output format.
56 0 0   0     if (none { $self->{'_opts'}->{'i'} eq $_ } @INPUT_FORMATS) {
  0            
57 0           err "Input format '$self->{'_opts'}->{'i'}' doesn't supported.";
58             }
59              
60 0           my $stats = MARC::Record::Stats->new;
61              
62 0           my $marc_file;
63 0 0         if ($self->{'_opts'}->{'i'} eq 'xml') {
64 0           $marc_file = MARC::File::XML->in($self->{'_marc_file'});
65 0 0         if (! defined $marc_file) {
66 0           err 'Cannot create object for MARC file.';
67             }
68             } else {
69 0           err 'Bad input MARC file.';
70             }
71 0           my $num = 1;
72 0           my $previous_record;
73 0           while (1) {
74 0           my $record = eval {
75 0           $marc_file->next;
76             };
77 0 0         if ($EVAL_ERROR) {
78 0 0         print STDERR "Cannot process '$num' record. ".
79             (
80             defined $previous_record
81             ? "Previous record is ".encode_utf8($previous_record->title)."\n"
82             : ''
83             );
84 0           print STDERR "Error: $EVAL_ERROR\n";
85 0           next;
86             }
87 0 0         if (! defined $record) {
88 0           last;
89             }
90              
91 0           $stats->add_record_to_stats($record);
92              
93 0           $previous_record = $record;
94              
95 0           $num++;
96             }
97              
98 0           $marc_file->close;
99              
100             # Print out.
101 0           $stats->report(*STDOUT);
102            
103 0           return 0;
104             }
105              
106             1;
107              
108              
109             __END__