Parser.pm

filter: Statementstmt 100% 89 / 89 Branchbran 88.1% 37 / 42 Conditioncond 77.7% 14 / 18 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 92.6% 163 / 176 CC 31 SCAR 18.5
CC
31
Cov
94%
_getparser
11.0
parsefile
8.1
new
4.1
CRAP 6.4
« Field.pm ↑ Summary SRUSearchParser.pm »
1 package PICA::Parser;
2 {
3 13 $PICA::Parser::VERSION = '0.585';
4 }
5 #ABSTRACT: Parse PICA+ data
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 our @EXPORT_OK = qw(parsefile parsedata);
12 13 our @CARP_NOT = qw(PICA::PlainParser PICA::XMLParser);
13  
14 13 require PICA::PlainParser;
15 13 require PICA::XMLParser;
16  
17  
18 83 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
19 83 my $class = "PICA::Parser";
20 83 ▶ if (scalar(@_) % 2) { # odd
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2) 83 0 83
21 83 $class = shift;
22 83 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0830
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
23 }
24 83 my %params = @_;
25  
26 83 my $self = bless {
27 defaultparams => {},
28 xmlparser => undef,
29 plainparser => undef
30 }, $class;
31  
32 83 ▶ %{ $self->{defaultparams} } = %params if %params;
Branch: 1 branch
BranchTrueFalseTotal
if %params 65 18 83
33  
34 83 return $self;
35 }
36  
37  
38 86 ▶sub parsefile {
Subroutine parsefile: called CC=8 SCAR=20.9
Pod documented
39 86 my $self = shift;
40 86 my ($arg, $parser);
41  
42 86 ▶ if (ref($self) eq 'PICA::Parser') { # called as a method
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'PICA::Parser') { } 49 37 86
43 49 $arg = shift;
44 49 my %params = @_;
45 49 ▶ if (ref(\$arg) eq 'SCALAR' and ($arg =~ /.xml$/i or $arg =~ /.xml.gz$/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$arg eq "SCALAR" and $arg =~ /.xml$/i || $arg =~ /.xml.gz$/i) 15 34 49
Condition83%5/6
ref \$arg eq "SCALAR" and $arg =~ /.xml$/i || $arg =~ /.xml.gz$/i
!ll&&!rl&&r
151915
$arg =~ /.xml$/i or $arg =~ /.xml.gz$/i
l!l&&r!l&&!r
15019
MC/DC66%2/3
ref \$arg eq "SCALAR" and $arg =~ /.xml$/i || $arg =~ /.xml.gz$/i
ref \$arg eq "SCALAR" $arg =~ /.xml$/i $arg =~ /.xml.gz$/i
Truth table75%3/4
A and B or C
A= ref \$arg eq "SCALAR"B= $arg =~ /.xml$/iC= $arg =~ /.xml.gz$/i
ABCresult
0XX0
1000
11X1
1011
46 15 $params{Format} = "XML";
47 }
48 49 $parser = $self->_getparser( %params );
49 49 ▶ croak("Missing argument to parsefile") unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 49 49
50 49 $parser->parsefile( $arg );
51 49 $self;
52 } else { # called as a function
53 37 ▶ $arg = ($self eq 'PICA::Parser') ? shift : $self;
Branch: 1 branch
BranchTrueFalseTotal
$self eq 'PICA::Parser' ? : 33 4 37
54 37 ▶ croak("Missing argument to parsefile") unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 37 37
55 37 $parser = PICA::Parser->new( @_ );
56 37 $parser->parsefile( $arg );
57 37 $parser;
58 }
59 }
60  
61  
62 78 ▶sub parsedata {
Subroutine parsedata: called CC=3 SCAR=11.0
Pod documented
63 78 my $self = shift;
64 78 my ( $data, $parser );
65  
66 78 ▶ if (ref($self) eq 'PICA::Parser') { # called as a method
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'PICA::Parser') { } 46 32 78
67 46 $data = shift;
68 46 my %params = @_;
69 46 $parser = $self->_getparser( %params );
70 46 $parser->parsedata( $data );
71 46 $self;
72 } else { # called as a function
73 32 ▶ $data = ($self eq 'PICA::Parser') ? shift : $self;
Branch: 1 branch
BranchTrueFalseTotal
$self eq 'PICA::Parser' ? : 14 18 32
74 32 $parser = PICA::Parser->new( @_ );
75 32 $parser->parsedata( $data );
76 32 $parser;
77 }
78 }
79  
80  
81 26 ▶sub records {
Subroutine records: called CC=4 SCAR=13.9
Pod documented
82 26 my $self = shift;
83 26 ▶ return () unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 26 26
84  
85 26 ▶ return $self->{plainparser}->records() if $self->{plainparser};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"plainparser"} 17 9 26
86 9 ▶ return $self->{xmlparser}->records() if $self->{xmlparser};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"xmlparser"} 8 1 9
87  
88 1 return ();
89 }
90  
91  
92 22 ▶sub counter {
Subroutine counter: called CC=4 SCAR=13.9
Pod documented
93 22 my $self = shift;
94 22 ▶ return undef if !ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 22 22
95  
96 22 my $counter = 0;
97 22 ▶ $counter += $self->{plainparser}->counter() if $self->{plainparser};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"plainparser"} 20 2 22
98 22 ▶ $counter += $self->{xmlparser}->counter() if $self->{xmlparser};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"xmlparser"} 2 20 22
99 22 return $counter;
100 }
101  
102  
103 34 ▶sub enable_binmode_encoding {
Subroutine enable_binmode_encoding: called CC=3 SCAR=11.0
Pod documented
104 34 my $fh = shift;
105 34 foreach my $layer ( PerlIO::get_layers( $fh ) ) {
106 90 ▶ return if $layer =~ /^encoding|^utf8/;
Branch: 1 branch
BranchTrueFalseTotal
if $layer =~ /^encoding|^utf8/ 22 68 90
107 }
108 12 binmode ($fh, ':utf8');
109 }
110  
111  
112 95 ▶sub _getparser {
Subroutine _getparser: called CC=11 SCAR=24.0
113 95 my $self = shift;
114 95 my %params = @_;
115 95 ▶ delete $params{Proceed} if defined $params{Proceed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Proceed"} 4 91 95
116  
117 95 my $parser;
118  
119 # join parameters
120 95 my %unionparams = ();
121 95 my %defaultparams = %{ $self->{defaultparams} };
122 95 my $key;
123 95 foreach $key (keys %defaultparams) {
124 93 $unionparams{$key} = $defaultparams{$key}
125 }
126 95 foreach $key (keys %params) {
127 24 $unionparams{$key} = $params{$key}
128 }
129 # remove format parameter
130 95 ▶ delete $params{Format} if defined $params{Format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Format"} 18 77 95
131  
132 # XMLParser
133 95 ▶ if ( defined $unionparams{Format} and $unionparams{Format} =~ /^xml$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $unionparams{'Format'} and $unionparams{'Format'} =~ /^xml$/i) { } 24 71 95
Condition66%2/3
defined $unionparams{"Format"} and $unionparams{"Format"} =~ /^xml$/i
!ll&&!rl&&r
71024
MC/DC50%1/2
defined $unionparams{"Format"} and $unionparams{"Format"} =~ /^xml$/i
defined $unionparams{"Format"} $unionparams{"Format"} =~ /^xml$/i
Truth table66%2/3
A and B
A= defined $unionparams{"Format"}B= $unionparams{"Format"} =~ /^xml$/i
ABresult
0X0
100
111
134 24 ▶ if ( !$self->{xmlparser} or %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"xmlparser"} or %params) 22 2 24
Condition100%3/3
not $self->{"xmlparser"} or %params
l!l&&r!l&&!r
2022
MC/DC100%2/2
not $self->{"xmlparser"} or %params
not $self->{"xmlparser"} %params
Truth table100%3/3
not(A) or B
A= not $self->{"xmlparser"}B= %params
ABresult
1X1
011
000
135 #require PICA::XMLParser;
136 #if ($self->{xmlparser} &&
137 22 $self->{xmlparser} = PICA::XMLParser->new( %unionparams );
138 }
139 24 $parser = $self->{xmlparser};
140 } else { # PlainParser
141 71 ▶ if ( !$self->{plainparser} or %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"plainparser"} or %params) 65 6 71
Condition100%3/3
not $self->{"plainparser"} or %params
l!l&&r!l&&!r
6236
MC/DC100%2/2
not $self->{"plainparser"} or %params
not $self->{"plainparser"} %params
Truth table100%3/3
not(A) or B
A= not $self->{"plainparser"}B= %params
ABresult
1X1
011
000
142 #require PICA::PlainParser;
143 65 $self->{plainparser} = PICA::PlainParser->new( %unionparams );
144 }
145 71 $parser = $self->{plainparser};
146 }
147  
148 95 return $parser;
149 }
150  
151 13 1;
152  
153 __END__
« Field.pm ↑ Summary SRUSearchParser.pm »