blib/lib/App/MARC/Count.pm

filter: Statementstmt 87.6% 71 / 81 Branchbran 68.7% 11 / 16 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 87.5% 105 / 120 CC 11 SCAR 17.0
CC
11
Cov
85%
run
10.6
_open_marc_input
2.2
BEGIN
1.0
CRAP 5.5
↑ Summary
1 package App::MARC::Count;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use IO::Uncompress::AnyUncompress qw($AnyUncompressError);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use MARC::Batch;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MARC::File::XML (BinaryEncoding => 'utf8', RecordFormat => 'MARC21');
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Unicode::UTF8 qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 our $VERSION = 0.05;
15  
16 # Constructor.
17 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 8 my ($class, @params) = @_;
19  
20 # Create object.
21 8 my $self = bless {}, $class;
22  
23 # Process parameters.
24 8 set_params($self, @params);
25  
26 # Object.
27 7 return $self;
28 }
29  
30 # Run.
31 6 ▶sub run {
Subroutine run: called CC=10 SCAR=23.7
Pod documented
32 6 my $self = shift;
33  
34 # Process arguments.
35 6 $self->{'_opts'} = {
36 'h' => 0,
37 };
38 6 ▶ if (! getopts('h', $self->{'_opts'})
Branch: 1 branch
BranchTrueFalseTotal
if (not &getopts("h", $self->{"_opts"}) or $self->{"_opts"}{"h"} or @ARGV < 1) 3 3 6
Condition100%6/6
not &getopts("h", $self->{"_opts"}) or $self->{"_opts"}{"h"} or @ARGV < 1
l!l&&r!l&&!r
213
not &getopts("h", $self->{"_opts"}) or $self->{"_opts"}{"h"}
l!l&&r!l&&!r
114
MC/DC100%3/3
not &getopts("h", $self->{"_opts"}) or $self->{"_opts"}{"h"} or @ARGV < 1
not &getopts("h", $self->{"_opts"}) $self->{"_opts"}{"h"} @ARGV < 1
Truth table100%4/4
not(A) or B or C
A= not &getopts("h", $self->{"_opts"})B= $self->{"_opts"}{"h"}C= @ARGV < 1
ABCresult
1XX1
01X1
0011
0000
39 || $self->{'_opts'}->{'h'}
40 || @ARGV < 1) {
41  
42 3 print STDERR "Usage: $0 [-h] [--version] marc_xml_file\n";
43 3 print STDERR "\t-h\t\tPrint help.\n";
44 3 print STDERR "\t--version\tPrint version.\n";
45 3 print STDERR "\tmarc_xml_file\tMARC XML file.\n";
46 3 return 1;
47 }
48 3 $self->{'_marc_xml_file'} = shift @ARGV;
49  
50 3 my ($fh, $errno);
51 3 ▶ if ($self->_open_marc_input($self->{'_marc_xml_file'}, \$fh, \$errno)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_open_marc_input($self->{"_marc_xml_file"}, \$fh, \$errno)) 0 3 3
52 0 print STDERR "Cannot open file '$self->{'_marc_xml_file'}'.";
53 0 ▶ if (defined $errno) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $errno) 0 0 0
54 0 print STDERR "\tErrno: $errno\n";
55 }
56 0 return 1;
57 }
58 3 my $stream = 'XML';
59 3 my $marc_batch = eval {
60 3 MARC::Batch->new('XML', $fh);
61 };
62 3 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::MARC::Count::EVAL_ERROR) 0 3 3
63 0 print STDERR "Cannot open MARC $stream stream.\n";
64 0 print STDERR "\tError: $EVAL_ERROR\n";
65 0 return 1;
66 }
67 3 my $ret_hr = {};
68 3 my $num = 0;
69 3 my $previous_record;
70 3 while (1) {
71 8 $num++;
72 8 my $record = eval {
73 8 $marc_batch->next;
74 };
75 8 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::MARC::Count::EVAL_ERROR) 2 6 8
76 2 ▶ print STDERR "Cannot process '$num' record. ".
Branch: 1 branch
BranchTrueFalseTotal
defined $previous_record ? : 1 1 2
77 (
78 defined $previous_record
79 ? "Previous record is ".encode_utf8($previous_record->title)."\n"
80 : ''
81 );
82 2 print STDERR "Error: $EVAL_ERROR\n";
83 2 next;
84 }
85 6 ▶ if (! defined $record) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $record) 3 3 6
86 3 $num--;
87 3 last;
88 }
89 3 $previous_record = $record;
90 }
91  
92 # Print out.
93 3 print $num."\n";
94
95 3 return 0;
96 }
97  
98 3 ▶sub _open_marc_input {
Subroutine _open_marc_input: called CC=2 SCAR=7.8
99 3 my ($self, $path, $fh_sr, $errno_sr) = @_;
100  
101 # Compression autodetection.
102 3 ${$fh_sr} = IO::Uncompress::AnyUncompress->new($path);
103 3 ▶ if (defined ${$fh_sr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${$fh_sr;}) 3 0 3
104 3 return 0;
105 }
106 0 ${$errno_sr} = $AnyUncompressError;
107  
108 0 return 1;
109 }
110  
111 4 1;
112  
113 __END__
↑ Summary