File Coverage

blib/lib/App/MARC/Count.pm
Criterion Covered Total %
statement 52 52 100.0
branch 8 8 100.0
condition 6 6 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 77 77 100.0


line stmt bran cond sub pod time code
1             package App::MARC::Count;
2              
3 4     4   169378 use strict;
  4         8  
  4         190  
4 4     4   68 use warnings;
  4         8  
  4         262  
5              
6 4     4   2481 use Class::Utils qw(set_params);
  4         76519  
  4         111  
7 4     4   519 use English;
  4         15  
  4         23  
8 4     4   5784 use Getopt::Std;
  4         13127  
  4         346  
9 4     4   2567 use MARC::File::XML (BinaryEncoding => 'utf8', RecordFormat => 'MARC21');
  4         650450  
  4         36  
10 4     4   2109 use Unicode::UTF8 qw(encode_utf8);
  4         3271  
  4         2255  
11              
12             our $VERSION = 0.04;
13              
14             # Constructor.
15             sub new {
16 8     8 1 783507 my ($class, @params) = @_;
17              
18             # Create object.
19 8         42 my $self = bless {}, $class;
20              
21             # Process parameters.
22 8         61 set_params($self, @params);
23              
24             # Object.
25 7         85 return $self;
26             }
27              
28             # Run.
29             sub run {
30 6     6 1 18 my $self = shift;
31              
32             # Process arguments.
33 6         42 $self->{'_opts'} = {
34             'h' => 0,
35             };
36 6 100 100     47 if (! getopts('h', $self->{'_opts'})
      100        
37             || $self->{'_opts'}->{'h'}
38             || @ARGV < 1) {
39              
40 3         501 print STDERR "Usage: $0 [-h] [--version] marc_xml_file\n";
41 3         97 print STDERR "\t-h\t\tPrint help.\n";
42 3         42 print STDERR "\t--version\tPrint version.\n";
43 3         32 print STDERR "\tmarc_xml_file\tMARC XML file.\n";
44 3         18 return 1;
45             }
46 3         137 $self->{'_marc_xml_file'} = shift @ARGV;
47              
48 3         49 my $marc_file = MARC::File::XML->in($self->{'_marc_xml_file'});
49 3         405 my $ret_hr = {};
50 3         11 my $num = 0;
51 3         7 my $previous_record;
52 3         8 while (1) {
53 8         28 $num++;
54 8         19 my $record = eval {
55 8         61 $marc_file->next;
56             };
57 8 100       22127 if ($EVAL_ERROR) {
58 2 100       128 print STDERR "Cannot process '$num' record. ".
59             (
60             defined $previous_record
61             ? "Previous record is ".encode_utf8($previous_record->title)."\n"
62             : ''
63             );
64 2         457 print STDERR "Error: $EVAL_ERROR\n";
65 2         9 next;
66             }
67 6 100       22 if (! defined $record) {
68 3         10 $num--;
69 3         7 last;
70             }
71 3         102 $previous_record = $record;
72             }
73              
74             # Print out.
75 3         152 print $num."\n";
76            
77 3         172 return 0;
78             }
79              
80             1;
81              
82              
83             __END__