blib/lib/WWW/LEO.pm

filter: Statementstmt 55.0% 55 / 100 Branchbran 11.5% 3 / 26 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 68.4% 13 / 19 Podpod 100% 12 / 12 totaltotal 50.3% 84 / 167 CC 17 SCAR 40.1
CC
17
Cov
45%
_parse_response
156.0
new
4.7
query
3.1
CRAP 54.9
↑ Summary
1 package WWW::LEO;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 require Exporter;
12  
13 1 our @ISA = qw(Exporter);
14 1 our %EXPORT_TAGS = ();
15 1 our @EXPORT_OK = ();
16 1 our @EXPORT = qw();
17 1 our $VERSION = '0.01';
18  
19 sub new {
20  
21 1 ▶ my ($proto, $args) = @_;
Subroutine new: called CC=4 SCAR=15.5
Pod documented
22 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
23 1 my $self = {};
24 1 bless ($self, $class);
25 1 $self->reset;
26  
27 # set user agent string to something reasonable
28 1 $self->{ua} = LWP::UserAgent->new(
29 agent => "Mozilla/5.0 (compatible; WWW::LEO/$VERSION)"
30 );
31  
32 # XXX[incorporate more args]
33 # process args supplied to constructor
34 1 ▶ if (defined $args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) 0 1 1
35 0 ▶ $self->agent->agent($args->{agent}) if exists $args->{agent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"agent"} 0 0 0
36 }
37  
38 1 $self->{req} = HTTP::Request->new('GET');
39  
40 1 return $self;
41  
42 }
43  
44 sub agent {
45  
46 1 ▶ return $_[0]->{ua};
Subroutine agent: called CC=1 SCAR=0.0
Pod documented
47  
48 }
49  
50 sub request {
51  
52 3 ▶ return $_[0]->{req};
Subroutine request: called CC=1 SCAR=0.0
Pod documented
53  
54 }
55  
56 sub response {
57  
58 0 ▶ return $_[0]->{res};
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
59  
60 }
61  
62 sub num_results {
63  
64 5 ▶ return $_[0]->{num_results};
Subroutine num_results: called CC=1 SCAR=0.0
Pod documented
65  
66 }
67  
68 sub maxlen_en {
69  
70 0 ▶ return $_[0]->{maxlen_en};
Subroutine maxlen_en: not called CC=1 SCAR=6.9
Pod documented
71  
72 }
73  
74 sub maxlen_de {
75  
76 0 ▶ return $_[0]->{maxlen_de};
Subroutine maxlen_de: not called CC=1 SCAR=6.9
Pod documented
77  
78 }
79  
80 sub en {
81  
82 0 ▶ return $_[0]->{en};
Subroutine en: not called CC=1 SCAR=6.9
Pod documented
83  
84 }
85  
86 sub de {
87  
88 0 ▶ return $_[0]->{de};
Subroutine de: not called CC=1 SCAR=6.9
Pod documented
89  
90 }
91  
92 sub en_de {
93  
94 1 ▶ return $_[0]->{en_de};
Subroutine en_de: called CC=1 SCAR=0.0
Pod documented
95  
96 }
97  
98 sub query {
99  
100 3 ▶ my ($self, $query) = @_;
Subroutine query: called CC=3 SCAR=11.3
Pod documented
101  
102 3 ▶ return $self->{query} unless defined $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 0 3 3
103  
104 3 $self->reset;
105 3 $self->{query} = $query;
106  
107 3 $query = uri_escape($query);
108 3 $self->{req}->uri("http://dict.leo.org/?search=$query&relink=off");
109 3 $self->{res} = $self->{ua}->request($self->request);
110  
111 3 ▶ if ($self->{res}->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"res"}->is_success) 0 3 3
112  
113 0 $self->_parse_response;
114  
115 }
116  
117 3 return $self->{num_results};
118  
119 }
120  
121 sub reset {
122  
123 5 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
124 5 $self->{query} = undef; # query string
125 5 $self->{num_results} = undef; # number of results
126 5 $self->{maxlen_en} = undef; # length of longest string in English results
127 5 $self->{maxlen_de} = undef; # length of longest string in German results
128 5 $self->{en} = []; # English results (array)
129 5 $self->{de} = []; # German results (array)
130 5 $self->{en_de} = []; # English and German result pairs (array of arrays)
131 # $self->{en_de}[$i][0]: English result
132 # $self->{en_de}[$i][1]: German result
133  
134 5 return $self;
135  
136 }
137  
138  
139 sub _parse_response {
140  
141 0 ▶ my $self = shift;
Subroutine _parse_response: not called CC=12 SCAR=50.5
142  
143 0 require HTML::TokeParser;
144 0 ▶ $self->{tokep} = HTML::TokeParser->new($self->{res}->content_ref) or croak "HTML::TokeParser->new(): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"tokep"} = "HTML::TokeParser"->new($self->{"res"}->content_ref) or croak("HTML::TokeParser->new(): $!\n") 0 0 0
145  
146 TOKEN:
147 0 ▶ while (my $token = $self->{tokep}->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->{"tokep"}->get_token) 0 0 0
148  
149 0 ▶ if ($token->[0] eq 'T') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->[0] eq "T") 0 0 0
150  
151 0 my $text = $token->[1];
152  
153 0 for ($text) {
154  
155 0 ▶ /no\s+results/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/no\s+results/ and do { $self->{"num_results"} = 0; return } 0 0 0
156 0 $self->{num_results} = 0;
157 0 return;
158 };
159  
160 0 ▶ /search\s+results/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/search\s+results/ and do { ($self->{"num_results"}) = $token->[1] =~ /(\d+)/; last TOKEN } 0 0 0
161 0 ($self->{num_results}) = $token->[1] =~ /(\d+)/;
162 0 last TOKEN;
163 }
164  
165 }
166  
167 }
168  
169 }
170  
171 0 my ($maxlen_en, $maxlen_de) = (0, 0);
172  
173 # extract translations
174 0 while (1) {
175  
176 0 $self->{tokep}->get_tag('/td');
177  
178 # get English text
179 0 my $en = $self->{tokep}->get_trimmed_text('/td');
180 # fix English text
181 0 $en =~ s/^\xa0\x20//;
182 0 $en =~ s/\x20\xa0$//;
183 0 ▶ $maxlen_en = length $en if length $en > $maxlen_en;
Branch: 1 branch
BranchTrueFalseTotal
if length $en > $maxlen_en 0 0 0
184 0 push @{$self->{en}}, $en;
185  
186 0 $self->{tokep}->get_tag('/td');
187  
188 # get German text
189 0 my $de = $self->{tokep}->get_trimmed_text('/tr');
190 # fix German text
191 0 $de =~ s/^\xa0\x20//;
192 0 $de =~ s/\x20\xa0$//;
193 0 ▶ $maxlen_de = length $de if length $de > $maxlen_de;
Branch: 1 branch
BranchTrueFalseTotal
if length $de > $maxlen_de 0 0 0
194 0 push @{$self->{de}}, $de;
195  
196 0 push @{$self->{en_de}}, [$en, $de];
197  
198 # skip </tr>
199 0 $self->{tokep}->get_token;
200  
201 # are we at end of table yet?
202 0 my $next = $self->{tokep}->get_token;
203 0 ▶ $self->{tokep}->unget_token($next) unless $next->[0] eq 'T';
Branch: 1 branch
BranchTrueFalseTotal
unless $next->[0] eq "T" 0 0 0
204 0 $next = $self->{tokep}->get_token;
205 0 ▶ ($next->[0] eq 'E' and $next->[1] eq 'table') ? last : $self->{tokep}->unget_token($next);
Branch: 1 branch
BranchTrueFalseTotal
$next->[0] eq 'E' && $next->[1] eq 'table' ? : 0 0 0
Condition0%0/3
$next->[0] eq "E" and $next->[1] eq "table"
!ll&&!rl&&r
000
MC/DC0%0/2
$next->[0] eq "E" and $next->[1] eq "table"
$next->[0] eq "E" $next->[1] eq "table"
Truth table0%0/3
A and B
A= $next->[0] eq "E"B= $next->[1] eq "table"
ABresult
0X0
100
111
206  
207 }
208  
209 0 @$self{qw/maxlen_en maxlen_de/} = ($maxlen_en, $maxlen_de);
210  
211 0 return $self->{num_results};
212  
213 }
214  
215 1 1;
216 __END__
↑ Summary