Client.pm

filter: Statementstmt 62.3% 296 / 475 Branchbran 35.3% 94 / 266 Conditioncond 42.8% 27 / 63 MC/DCmcdc 12.8% 5 / 39 Subroutinesub 69.0% 49 / 71 Podpod 100% 15 / 15 totaltotal 52.3% 486 / 929 CC 162 SCAR 43.2
CC
162
Cov
52%
__ANON__
420.0
query
285.8
url_query
110.0
CRAP 75.3
↑ Summary Client/WSQueryInterfaceService.pm »
1 =head1 NAME
2  
3 Astro::SIMBAD::Client - Fetch astronomical data from SIMBAD 4.
4  
5 =head1 SYNOPSIS
6  
7 use Astro::SIMBAD::Client;
8 my $simbad = Astro::SIMBAD::Client->new ();
9 print $simbad->query (id => 'Arcturus');
10  
11 =head1 NOTICE
12  
13 As of release 0.027_01 the SOAP interface is deprecated. The University
14 of Strasbourg has announced at
15 L<https://cds.u-strasbg.fr/resources.gml?id=soap> that this
16 interface will not be maintained after April 1 2014, and that
17 B<this interface will be stopped on December 31 2018>.
18  
19 Because the SOAP interface is still sort of functional (except for
20 VO-format queries) as of June 4 2014, I have revised the transition plan
21 announced with the release of 0.027_01 on October 28 2014.
22  
23 What I have done as of version 0.031_01 is to add attribute
24 C<emulate_soap_queries>. This was false by default. If this attribute is
25 true, the C<query()> method and friends, instead of issuing a SOAP
26 request to the SIMBAD server, will instead construct an equivalent
27 script query, and issue that. The deprecation warning will not be issued
28 if C<emulate_soap_queries> is true, since the SOAP interface is not
29 being used.
30  
31 As of March 22 2021, SOAP queries started returning 404. Because of
32 this, I have made the default of C<emulate_soap_queries> true. Well,
33 actually I have made it the Boolean inverse of environment variable
34 L<ASTRO_SIMBAD_CLIENT_USE_SOAP|/ASTRO_SIMBAD_CLIENT_USE_SOAP>. This is
35 mostly for my benefit, so I can see if SOAP has come back.
36  
37 If SOAP still has not come back after six months, SOAP queries will
38 become fatal, as will setting C<emulate_soap_queries> to a false value.
39  
40 Eventually the SOAP code will be removed. In the meantime all tests are
41 skipped unless C<ASTRO_SIMBAD_CLIENT_USE_SOAP> is true, and are marked
42 TODO. Support of SOAP by this module will be on a best-effort basis;
43 that is, if I can make it work without a huge amount of work I will --
44 otherwise SOAP will become unsupported.
45  
46 =head1 DESCRIPTION
47  
48 This package implements several query interfaces to version 4 of the
49 SIMBAD on-line astronomical database, as documented at
50 L<http://simbad.u-strasbg.fr/simbad4.htx>. B<This package will not work
51 with SIMBAD version 3.> Its primary purpose is to obtain SIMBAD data,
52 though some rudimentary parsing functionality also exists.
53  
54 There are three ways to access these data.
55  
56 - URL queries are essentially page scrapers, but their use is
57 documented, and output is available as HTML, text, or VOTable. URL
58 queries are implemented by the url_query() method.
59  
60 - Scripts may be submitted using the script() or script_file() methods.
61 The former takes as its argument the text of the script, the latter
62 takes a file name.
63  
64 - Queries may be made using the web services (SOAP) interface. The
65 query() method implements this, and queryObjectByBib,
66 queryObjectByCoord, and queryObjectById have been provided as
67 convenience methods. As of version 0.027_01, SOAP queries are
68 deprecated. See the L<NOTICE|/NOTICE> section above for the deprecation
69 schedule.
70  
71 Astro::SIMBAD::Client is object-oriented, with the object supplying not
72 only the URL scheme and SIMBAD server name, but the default format and
73 output type for URL and web service queries.
74  
75 A simple command line client application is also provided, as are
76 various examples in the F<eg> directory.
77  
78 =head2 Methods
79  
80 The following methods should be considered public:
81  
82 =over 4
83  
84 =cut
85  
86 package Astro::SIMBAD::Client;
87  
88 # We require Perl 5.008 because of MailTools, used by SOAP::Lite.
89 # Otherwise it would be 5.006 because of 'our'.
90  
91 5 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 5 ▶use LWP::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 5 ▶use HTTP::Request::Common qw{POST};
Subroutine BEGIN: called CC=1 SCAR=0.0
100 5 ▶use Scalar::Util 1.01 qw{looks_like_number};
Subroutine BEGIN: called CC=1 SCAR=0.0
101 5 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
102 # use XML::DoubleEncodedEntities;
103 # use Astro::SIMBAD::Client::WSQueryInterfaceService;
104  
105 5 ▶use constant HAVE_DOUBLE_ENCODED => do {
Subroutine BEGIN: called CC=1 SCAR=0.0
106 5 local $@ = undef;
107 5 eval { ## no critic (RequireCheckingReturnValueOfEval)
108 5 require XML::DoubleEncodedEntities;
109 0 1;
110 };
111 5 };
112  
113 5 ▶use constant ARRAY_REF => ref [];
Subroutine BEGIN: called CC=1 SCAR=0.0
114 5 ▶use constant CODE_REF => ref sub {};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
115 5 ▶use constant HASH_REF => ref {};
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 5 my $have_time_hires;
118 5 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=13.0
119 5 $have_time_hires = eval {
120 5 require Time::HiRes;
121 5 Time::HiRes->import (qw{time sleep});
122 5 1;
123 };
124  
125 *_escape_uri = URI::Escape->can( 'uri_escape_utf8' )
126 || URI::Escape->can( 'uri_escape' )
127 0 ▶ || sub { return $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=2.2
Condition33%2/6
'URI::Escape'->can('uri_escape_utf8') || 'URI::Escape'->can('uri_escape') || sub { return $_[0]; }
l!l&&r!l&&!r
500
"URI::Escape"->can("uri_escape_utf8") or "URI::Escape"->can("uri_escape")
l!l&&r!l&&!r
500
MC/DC0%0/3
'URI::Escape'->can('uri_escape_utf8') || 'URI::Escape'->can('uri_escape') || sub { return $_[0]; }
"URI::Escape"->can("uri_escape_utf8") "URI::Escape"->can("uri_escape") sub { return $_[0]; }
Truth table25%1/4
A or B || C
A= "URI::Escape"->can("uri_escape_utf8")B= "URI::Escape"->can("uri_escape")C= sub { return $_[0]; }
ABCresult
1XX1
01X1
0011
0000
128 }
129  
130 5 our $VERSION = '0.050';
131  
132 5 our @CARP_NOT = qw{Astro::SIMBAD::Client::WSQueryInterfaceService};
133  
134 # TODO replace this with s///r if we ever get to the point where we
135 # require Perl 5.13.2 or greater.
136 10 ▶sub _strip_returns {
Subroutine _strip_returns: called CC=1 SCAR=0.0
137 10 my ( $data ) = @_;
138 10 $data =~ s/ \n //smxg;
139 10 return $data;
140 }
141  
142 5 ▶use constant FORMAT_TXT_SIMPLE_BASIC => _strip_returns( <<'EOD' );
Subroutine BEGIN: called CC=1 SCAR=0.0
143 ---\n
144 name: %IDLIST(NAME|1)\n
145 type: %OTYPE\n
146 long: %OTYPELIST\n
147 ra: %COO(d;A)\n
148 dec: %COO(d;D)\n
149 plx: %PLX(V)\n
150 pmra: %PM(A)\n
151 pmdec: %PM(D)\n
152 radial: %RV(V)\n
153 redshift: %RV(Z)\n
154 spec: %SP(S)\n
155 bmag: %FLUXLIST(B)[%flux(F)]\n
156 vmag: %FLUXLIST(V)[%flux(F)]\n
157 ident: %IDLIST[%*,]
158 EOD
159  
160 5 ▶use constant FORMAT_TXT_YAML_BASIC => _strip_returns( <<'EOD' );
Subroutine BEGIN: called CC=1 SCAR=0.0
161 ---\n
162 name: '%IDLIST(NAME|1)'\n
163 type: '%OTYPE'\n
164 long: '%OTYPELIST'\n
165 ra: %COO(d;A)\n
166 dec: %COO(d;D)\n
167 plx: %PLX(V)\n
168 pm:\n
169 - %PM(A)\n
170 - %PM(D)\n
171 radial: %RV(V)\n
172 redshift: %RV(Z)\n
173 spec: %SP(S)\n
174 bmag: %FLUXLIST(B)[%flux(F)]\n
175 vmag: %FLUXLIST(V)[%flux(F)]\n
176 ident:\n%IDLIST[ - '%*'\n]
177 EOD
178  
179 # Documentation errors/omissions:
180 # %PLX:
181 # P = something. Yields '2' for Arcturus
182 # %SP: is really %sptype
183 # B = bibcode? Yields '~' for Arcturus
184 # N = don't know -- yields 'S' for Arcturus
185 # Q = quality? Yields 'C' for Arcturus
186 # S = spectral type
187  
188 5 ▶use constant FORMAT_VO_BASIC => join ',', qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
189 id(NAME|1) otype ra(d) dec(d) plx_value pmra pmdec rv_value z_value
190 5 sp_type flux(B) flux(V)};
191 # Note that idlist was documented at one point as being the
192 # VOTable equivalent of %IDLIST. But it is no longer documented,
193 # and never returned anything but '<TD>?IDLIST</TD>'.
194  
195 my %static = (
196 autoload => 1,
197 debug => 0,
198 delay => 3,
199 emulate_soap_queries => ! $ENV{ASTRO_SIMBAD_CLIENT_USE_SOAP},
200 format => {
201 txt => FORMAT_TXT_YAML_BASIC,
202 vo => FORMAT_VO_BASIC,
203 script => '',
204 },
205 parser => {
206 txt => '',
207 vo => '',
208 script => '',
209 },
210 post => 1,
211 # lc(...) per https://tools.ietf.org/html/rfc3986#section-3.1
212 scheme => lc( $ENV{ASTRO_SIMBAD_CLIENT_SCHEME} || 'https' ),
213 ## server => 'simbad.u-strasbg.fr',
214 5 ▶ server => $ENV{ASTRO_SIMBAD_CLIENT_SERVER} || 'simbad.u-strasbg.fr',
Condition50%2/4
$ENV{'ASTRO_SIMBAD_CLIENT_SCHEME'} || 'https'
l!l
05
$ENV{'ASTRO_SIMBAD_CLIENT_SERVER'} || 'simbad.u-strasbg.fr'
l!l
05
MC/DC0%0/1
$ENV{'ASTRO_SIMBAD_CLIENT_SCHEME'} || 'https'
$ENV{'ASTRO_SIMBAD_CLIENT_SCHEME'}
MC/DC0%0/1
$ENV{'ASTRO_SIMBAD_CLIENT_SERVER'} || 'simbad.u-strasbg.fr'
$ENV{'ASTRO_SIMBAD_CLIENT_SERVER'}
Truth table50%1/2
A
A= $ENV{'ASTRO_SIMBAD_CLIENT_SCHEME'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'ASTRO_SIMBAD_CLIENT_SERVER'}
Aresult
11
00
215 timeout => LWP::UserAgent->new()->timeout(),
216 type => 'txt',
217 url_args => {},
218 verbatim => 0,
219 );
220  
221 5 ▶if ( my $msg = _is_scheme_valid(
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = _is_scheme_valid($static{"scheme"}, "Unsupported ASTRO_SIMBAD_CLIENT_SCHEME '%s'; falling back to 'http'")) 0 5 5
222 $static{scheme},
223 q<Unsupported ASTRO_SIMBAD_CLIENT_SCHEME '%s'; falling back to 'http'>,
224 ) ) {
225 0 carp $msg;
226 0 $static{scheme} = 'http';
227 }
228  
229 =item $simbad = Astro::SIMBAD::Client->new ();
230  
231 This method instantiates an Astro::SIMBAD::Client object. Any arguments will be
232 passed to the set() method once the object is instantiated.
233  
234 =cut
235  
236 # The set() method does the unpacking. CAVEAT: do _NOT_ modify the
237 # contents of @_, as this will be seen by the caller. Modifying @_
238 # itself is fine.
239 8 ▶sub new { ## no critic (RequireArgUnpacking)
Subroutine new: called CC=2 SCAR=7.0
Pod documented
240 8 my $class = shift;
241 8 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 8 8
242 8 my $self = bless {}, $class;
243 8 $self->set (%static, @_);
244 8 return $self;
245 }
246  
247 =item $string = $simbad->agent ();
248  
249 This method retrieves the user agent string used to identify this
250 package in queries to SIMBAD. This string will be the default string for
251 LWP::UserAgent, with this package name and version number appended in
252 parentheses. This method is exposed for the curious.
253  
254 =cut
255  
256 {
257 5 my $agent_string;
258 9 ▶ sub agent {
Subroutine agent: called CC=2 SCAR=7.2
Pod documented
259 9 ▶ return ($agent_string ||= join (' ', LWP::UserAgent->_agent,
Condition66%2/3
$agent_string ||= join(" ", "LWP::UserAgent"->_agent, 'Astro::SIMBAD::Client/' . $Astro::SIMBAD::Client::VERSION)
l!l&&r!l&&!r
630
MC/DC0%0/2
$agent_string ||= join(" ", "LWP::UserAgent"->_agent, 'Astro::SIMBAD::Client/' . $Astro::SIMBAD::Client::VERSION)
$agent_string join(" ", "LWP::UserAgent"->_agent, 'Astro::SIMBAD::Client/' . $Astro::SIMBAD::Client::VERSION)
Truth table66%2/3
A ||= B
A= $agent_stringB= join(" ", "LWP::UserAgent"->_agent, 'Astro::SIMBAD::Client/' . $Astro::SIMBAD::Client::VERSION)
ABresult
1X1
011
000
260 __PACKAGE__ . '/' . $VERSION));
261 }
262 }
263  
264 =item @attribs = $simbad->attributes ();
265  
266 This method retrieves the names of all public attributes, in
267 alphabetical order. It can be called as a static method, or
268 even as a subroutine.
269  
270 =cut
271  
272 5 ▶sub attributes {
Subroutine attributes: not called CC=2 SCAR=13.0
Pod documented
273 0 ▶ return wantarray ? sort keys %static : [sort keys %static]
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
274 }
275  
276 =item $value = $simbad->get ($attrib);
277  
278 This method retrieves the current value of the named
279 L<attribute|/Attributes>. It can be called as a static method to
280 retrieve the default value.
281  
282 =cut
283  
284 84 ▶sub get {
Subroutine get: called CC=4 SCAR=14.8
Pod documented
285 84 my $self = shift;
286 0 croak "Error - First argument must be an @{[__PACKAGE__]} object"
287 84 ▶ unless eval {$self->isa(__PACKAGE__)};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa(__PACKAGE__) } } 0 84 84
288 84 ▶ $self = \%static unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 84 84
289 84 my $name = shift;
290 croak "Error - Attribute '$name' is unknown"
291 84 ▶ unless exists $static{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $static{$name} 0 84 84
292 84 return $self->{$name};
293 }
294  
295 =item $result = Parse_TXT_Simple ($data);
296  
297 This subroutine (B<not> method) parses the given text data under the
298 assumption that it was generated using FORMAT_TXT_SIMPLE_BASIC or
299 something similar. The data is expected to be formatted as follows:
300  
301 A line consisting of exactly '---' separates objects.
302  
303 Data appear on lines that look like
304  
305 name: data
306  
307 and are parsed into a hash keyed by the given name. If the line ends
308 with a comma, it is assumed to contain multiple items, and the data
309 portion of the line is split on the commas; the resultant hash value
310 is a list reference.
311  
312 The user would normally not call this directly, but specify it as the
313 parser for 'txt'-type queries:
314  
315 $simbad->set (parser => {txt => 'Parse_TXT_Simple'});
316  
317 =cut
318  
319 3 ▶sub Parse_TXT_Simple {
Subroutine Parse_TXT_Simple: called CC=5 SCAR=16.1
Pod documented
320 3 my $text = shift;
321 3 my $obj = {};
322 3 my @data;
323 3 foreach (split '\s*\n', $text) {
324 45 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 45 45
325 45 ▶ if (m/^-+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-+$/) { } 3 42 45
326 3 $obj = {};
327 3 push @data, $obj;
328 } else {
329 42 my ($name, $val) = split ':\s*', $_;
330 42 ▶ $val =~ s/,$// and $val = [split ',', $val];
Branch: 1 branch
BranchTrueFalseTotal
$val =~ s/,$// and $val = [split(/,/, $val, 0)] 3 39 42
331 42 $obj->{$name} = $val;
332 }
333 }
334 3 return @data;
335 }
336  
337 =item $result = Parse_VO_Table ($data);
338  
339 This subroutine (B<not> method) parses the given VOTable data,
340 returning a list of anonymous hashes describing the data. The $data
341 value is split on '<?xml' before parsing, so that you get multiple
342 VOTables back (rather than a parse error) if that is what the input
343 contains.
344  
345 This is B<not> a full-grown VOTable parser capable of handling
346 the full spec (see L<https://www.ivoa.net/documents/VOTable/>).
347 It is oriented toward returning E<lt>TABLEDATAE<gt> contents, and the
348 metadata that can reasonably be associated with those contents.
349  
350 B<NOTE> that as of version 0.026_01, the requisite modules
351 to support VO format are B<not> required. If you need VO format you will
352 need to install L<XML::Parser|XML::Parser> or L<XML::Parser::Lite>
353  
354 The return is a list of anonymous hashes, one per E<lt>TABLEE<gt>. Each
355 hash contains two keys:
356  
357 {data} is the data contained in the table, and
358 {meta} is the metadata for the table.
359  
360 The {meta} element for the table is a reference to a list of data
361 gathered from the E<lt>TABLEE<gt> tag. Element zero is the tag name
362 ('TABLE'), and element 1 is a reference to a hash containing the
363 attributes of the E<lt>TABLEE<gt> tag. Subsequent elements if any
364 represent metadata tags in the order encountered in the parse.
365  
366 The {data} contains an anonymous list, each element of which is a row of
367 data from the E<lt>TABLEDATAE<gt> element of the E<lt>TABLEE<gt>, in the
368 order encountered by the parse. Each row is a reference to a list of
369 anonymous hashes, which represent the individual data of the row, in the
370 order encountered by the parse. The data hashes contain two keys:
371  
372 {value} is the value of the datum with undef for '~', and
373 {meta} is a reference to the metadata for the datum.
374  
375 The {meta} element for a datum is a reference to the metadata tag that
376 describes that datum. This will be an anonymous list, of which element 0
377 is the tag ('FIELD'), element 1 is a reference to a hash containing that
378 tag's attributes, and subsequent elements will be the contents of the
379 tag (typically including a reference to the list representing the
380 E<lt>DESCRIPTIONE<gt> tag for that FIELD).
381  
382 All values are returned as provided by the XML parser; no further
383 decoding is done. Specifically, the datatype and arraysize attributes
384 are ignored.
385  
386 This parser is based on XML::Parser.
387  
388 The user would normally not call this directly, but specify it as the
389 parser for 'vo'-type queries:
390  
391 $simbad->set (parser => {vo => 'Parse_VO_Table'});
392  
393 =cut
394  
395 { # Begin local symbol block.
396  
397 5 my $xml_parser;
398  
399 # TODO get rid of XML::Parser::Lite when you get rid of SOAP
400 5 foreach (qw{XML::Parser XML::Parser::Lite}) {
401 0 ▶ eval { _load_module( $_ ); 1 } or next;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _load_module($_); 1 } } or next 0 10 10
402 0 $xml_parser = $_;
403 0 last;
404 }
405  
406 0 ▶ sub Parse_VO_Table {
Subroutine Parse_VO_Table: not called CC=3 SCAR=24.8
Pod documented
407 0 my $data = shift;
408  
409 0 ▶ defined $xml_parser
Branch: 1 branch
BranchTrueFalseTotal
defined $xml_parser or croak("Error - No XML parser available. Need XML::Parser or XML::Parser::Lite") 0 0 0
410 or croak 'Error - No XML parser available. Need XML::Parser or XML::Parser::Lite';
411  
412 0 my $root;
413 my @tree;
414 0 my @table;
415 0 my @to_strip;
416  
417 # Arguments:
418 # Init ($class)
419 # Start ($class, $tag, $attr => $value ...)
420 # Char ($class, $text)
421 # End ($class, $tag)
422 # Final ($class)
423  
424 my $psr = $xml_parser->new (
425 Handlers => {
426 0 ▶ Init => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
427 0 $root = [];
428 0 @tree = ($root);
429 0 @table = ();
430 },
431 0 ▶ Start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
432 0 shift;
433 0 my $tag = shift;
434 0 my $item = [$tag, {@_}];
435 0 push @{$tree[-1]}, $item;
436 0 push @tree, $item;
437 },
438 0 ▶ Char => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
439 0 push @{$tree[-1]}, $_[1];
440 },
441 0 ▶ End => sub {
Subroutine __ANON__: not called CC=20 SCAR=60.4
442 0 my $tag = $_[1];
443 0 ▶ die <<eod unless @tree > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @tree > 1 0 0 0
444 Error - Unmatched end tag </$tag>
445 eod
446 0 ▶ die <<eod unless $tag eq $tree[-1][0];
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq $tree[-1][0] 0 0 0
447 Error - End tag </$tag> does not match start tag <$tree[-1][0]>
448 eod
449  
450 # From here to the end of the subroutine is devoted to detecting
451 # the </TABLE> tag and extracting the data of the table into what
452 # is hopefully a more usable format. Any relationship of tables
453 # to resources is lost.
454  
455 0 my $element = pop @tree;
456 0 ▶ if ($element->[0] eq 'TABLE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->[0] eq "TABLE") 0 0 0
457 0 my (@meta, @data, @descr);
458 0 foreach (@$element) {
459 0 ▶ next unless ARRAY_REF eq ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $_ 0 0 0
460 0 ▶ if ($_->[0] eq 'FIELD') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->[0] eq 'FIELD') { } 0 0 0
elsif ($_->[0] eq 'DATA') { } 0 0 0
461 0 push @meta, $_;
462 0 push @descr, $_;
463 } elsif ($_->[0] eq 'DATA') {
464 0 foreach (@$_) {
465 0 ▶ next unless ARRAY_REF eq ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $_ 0 0 0
466 0 ▶ next unless $_->[0] eq 'TABLEDATA';
Branch: 1 branch
BranchTrueFalseTotal
unless $_->[0] eq "TABLEDATA" 0 0 0
467 0 foreach (@$_) {
468 0 ▶ next unless ARRAY_REF eq ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $_ 0 0 0
469 0 ▶ next unless $_->[0] eq 'TR';
Branch: 1 branch
BranchTrueFalseTotal
unless $_->[0] eq "TR" 0 0 0
470 0 my @row;
471 0 foreach (@$_) {
472 0 ▶ next unless ARRAY_REF eq ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $_ 0 0 0
473 0 ▶ next unless $_->[0] eq 'TD';
Branch: 1 branch
BranchTrueFalseTotal
unless $_->[0] eq "TD" 0 0 0
474 0 my @inf = grep {!ref $_} @$_;
475 0 shift @inf;
476 0 push @row, join ' ', @inf;
477 }
478 0 push @data, \@row;
479 }
480 }
481 } else {
482 0 push @descr, $_;
483 }
484 }
485 0 foreach (@data) {
486 0 my $inx = 0;
487 0 @$_ = map { {
488 0 ▶ value => (defined $_ && $_ eq '~')
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && $_ eq '~' ? : 0 0 0
Condition0%0/3
defined $_ and $_ eq "~"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and $_ eq "~"
defined $_ $_ eq "~"
Truth table0%0/3
A and B
A= defined $_B= $_ eq "~"
ABresult
0X0
100
111
489 ? undef : $_,
490 meta => $meta[$inx++],
491 } } @$_;
492 }
493 0 push @to_strip, @descr;
494 0 push @table, {
495 data => \@data,
496 meta => [$element->[0],
497 $element->[1], @descr],
498 };
499 }
500 },
501 0 ▶ Final => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
502 0 ▶ die <<eod if @tree > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @tree > 1 0 0 0
503 Error - Missing end tags.
504 eod
505  
506 ## _strip_empty ($root);
507 ## @$root;
508 # If the previous two lines were uncommented and the following two
509 # commented, the parser would return the parse tree for the
510 # VOTable.
511 0 _strip_empty (\@to_strip);
512 0 @table;
513 },
514 0 });
515 0 ▶ return map {$_ ? $psr->parse ($_) : ()} split '(?=<\?xml)', $data
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
516 }
517  
518 } # End of local symbol block.
519  
520 # _strip_empty (\@tree)
521 #
522 # splices out anything in the tree that is not a reference and
523 # does not match m/\S/.
524  
525 5 ▶sub _strip_empty {
Subroutine _strip_empty: not called CC=5 SCAR=32.6
526 0 my $ref = shift;
527 0 my $inx = @$ref;
528 0 ▶ while (--$inx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$inx >= 0) 0 0 0
529 0 my $val = $ref->[$inx];
530 0 my $typ = ref $val;
531 0 ▶ if ( ARRAY_REF eq $typ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq $typ) { } 0 0 0
elsif (not $typ) { } 0 0 0
532 0 _strip_empty ($val);
533 } elsif (!$typ) {
534 0 ▶ splice @$ref, $inx, 1 unless $val =~ m/\S/ms;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /\S/ms 0 0 0
535 }
536 }
537 0 return;
538 }
539  
540 =item $result = $simbad->query ($query => @args);
541  
542 This method is B<deprecated>, and will cease to work in April 2014.
543 Please choose a method that does not use SOAP. See the L<NOTICE|/NOTICE>
544 above for details.
545  
546 This method issues a web services (SOAP) query to the SIMBAD database.
547 The $query specifies a SIMBAD query method, and the @args are the
548 arguments for that method. Valid $query values and the corresponding
549 SIMBAD methods and arguments are:
550  
551 bib => queryObjectByBib ($bibcode, $format, $type)
552 coo => queryObjectByCoord ($coord, $radius, $format, $type)
553 id => queryObjectById ($id, $format, $type)
554  
555 where:
556  
557 $bibcode is a SIMBAD bibliographic code
558 $coord is a set of coordinates
559 $radius is an angular radius around the coordinates
560 $type is the type of data to be returned
561 $format is a format appropriate to the data type.
562  
563 The $type defaults to the value of the L<type|/type> attribute, and
564 the $format defaults to the value of the L<format|/format> attribute
565 for the given $type.
566  
567 The return value depends on a number of factors:
568  
569 If the query found nothing, you get undef in scalar context, and an
570 empty list in list context.
571  
572 If a L<parser|/parser> is defined for the given type, the returned
573 data will be fed to the parser, and the output of the parser will be
574 returned. This is assumed to be a list, so a reference to the list
575 will be used in scalar context. Parser exceptions are not trapped,
576 so the caller will need to be prepared to deal with malformed data.
577  
578 Otherwise, the result of the query is returned as-is.
579  
580 B<NOTE> that this functionality makes use of the
581 L<SOAP::Lite|SOAP::Lite> module. As of version 0.026_01 of
582 C<Astro::SIMBAD::Client>, L<SOAP::Lite|SOAP::Lite> is not a prerequisite
583 of this module. If you wish to use the C<query()> method, you will have
584 to install L<SOAP::Lite|SOAP::Lite> separately. This can be done after
585 C<Astro::SIMBAD::Client> is installed.
586  
587 =cut
588  
589 { # Begin local symbol block
590  
591 5 my %query_args = (
592 id => {
593 type => 2,
594 format => 1,
595 method => 'queryObjectById',
596 },
597 bib => {
598 type => 2,
599 format => 1,
600 method => 'queryObjectByBib',
601 },
602 coo => {
603 type => 3,
604 format => 2,
605 method => 'queryObjectByCoord',
606 },
607 );
608  
609 my %transform = (
610 2 ▶ txt => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
611 2 local $_ = $_[0];
612 2 s/\n//gm;
613 2 return $_
614 },
615 0 ▶ vo => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
616 0 ▶ local $_ = ref $_[0] ? join (',', @{$_[0]}) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
617 0 ▶ if ( defined $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
618 0 s/\s+/,/gms;
619 0 s/^,+//;
620 0 s/,+$//;
621 0 s/,+/,/g;
622 }
623 0 return $_
624 },
625 5 );
626  
627 my %make_script = (
628 2 ▶ txt => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
629 2 my ( $self, $query, @args ) = @_;
630 2 return <<"EOD";
631 2 format object "@{[ $transform{txt}->( $self->get( 'format' )->{txt} ) ]}"
632 query $query @args
633 EOD
634 },
635 0 ▶ vo => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
636 0 my ( $self, $query, @args ) = @_;
637 0 return <<"EOD";
638 votable myvo {
639 0 @{[ $transform{vo}->( $self->get( 'format' )->{vo} ) ]}
640 }
641 votable open myvo
642 query $query @args
643 votable close myvo
644 EOD
645 },
646 5 );
647  
648 2 ▶ sub query {
Subroutine query: called CC=19 SCAR=56.6
Pod documented
649 2 my ( $self, $query, @args ) = @_;
650 2 ▶ if ( $self->get( 'emulate_soap_queries' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get("emulate_soap_queries")) 2 0 2
651 2 my $type = $self->get( 'type' );
652 0 ▶ my $code = $make_script{$type} || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
653 0 my ( undef, $query, @args ) = @_; # Invocant unused
654 0 return "query $query @args\n";
655 2 ▶ };
Condition33%1/3
$make_script{$type} || sub { my(undef, $query, @args) = @_; return "query $query @args\n"; }
l!l&&r!l&&!r
200
MC/DC0%0/2
$make_script{$type} || sub { my(undef, $query, @args) = @_; return "query $query @args\n"; }
$make_script{$type} sub { my(undef, $query, @args) = @_; return "query $query @args\n"; }
Truth table33%1/3
A || B
A= $make_script{$type}B= sub { my(undef, $query, @args) = @_; return "query $query @args\n"; }
ABresult
1X1
011
000
656 2 return $self->_script(
657 parser => $type,
658 script => $code->( $self, $query, @args ),
659 verbatim => 0,
660 );
661 }
662 0 $self->_deprecation_notice( method => 'query', 'a non-SOAP method' );
663 0 ▶ eval { _load_module( 'SOAP::Lite' ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _load_module("SOAP::Lite"); 1 } } or croak("Error - query() requires SOAP::Lite") 0 0 0
664 or croak 'Error - query() requires SOAP::Lite';
665 0 ▶ eval { _load_module(
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _load_module("Astro::SIMBAD::Client::WSQueryInterfaceService"); 1 } } or croak("Programming Error - Can not load Astro::SIMBAD::Client::WSQueryInterfaceService: $@") 0 0 0
666 0 'Astro::SIMBAD::Client::WSQueryInterfaceService' ); 1 }
667 or croak "Programming Error - Can not load Astro::SIMBAD::Client::WSQueryInterfaceService: $@";
668 croak "Error - Illegal query type '$query'"
669 0 ▶ unless $query_args{$query};
Branch: 1 branch
BranchTrueFalseTotal
unless $query_args{$query} 0 0 0
670 0 my $method = $query_args{$query}{method};
671 0 ▶ croak "Programming error - Illegal query $query method $method"
Branch: 1 branch
BranchTrueFalseTotal
unless "Astro::SIMBAD::Client::WSQueryInterfaceService"->can($method) 0 0 0
672 unless Astro::SIMBAD::Client::WSQueryInterfaceService->can ($method);
673 0 my $debug = $self->get ('debug');
674 0 my $parser;
675 0 ▶ if (defined (my $type = $query_args{$query}{type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $type = $query_args{$query}{"type"})) 0 0 0
676 0 ▶ $args[$type] ||= $self->get ('type');
Condition0%0/3
$args[$type] ||= $self->get("type")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args[$type] ||= $self->get("type")
$args[$type] $self->get("type")
Truth table0%0/3
A ||= B
A= $args[$type]B= $self->get("type")
ABresult
1X1
011
000
677 0 ▶ if (defined (my $format = $query_args{$query}{format})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $format = $query_args{$query}{"format"})) 0 0 0
678 0 ▶ $args[$format] ||= $self->get ('format')->{$args[$type]};
Condition0%0/3
$args[$format] ||= $self->get("format")->{$args[$type]}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args[$format] ||= $self->get("format")->{$args[$type]}
$args[$format] $self->get("format")->{$args[$type]}
Truth table0%0/3
A ||= B
A= $args[$format]B= $self->get("format")->{$args[$type]}
ABresult
1X1
011
000
679 $args[$format] = $transform{$args[$type]}->($args[$format])
680 0 ▶ if $transform{$args[$type]};
Branch: 1 branch
BranchTrueFalseTotal
if $transform{$args[$type]} 0 0 0
681 0 ▶ warn "$args[$type] format: $args[$format]\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
682 0 ▶ $args[$format] = undef unless $args[$format];
Branch: 1 branch
BranchTrueFalseTotal
unless $args[$format] 0 0 0
683 }
684 0 $parser = $self->_get_parser ($args[$type]);
685 }
686 0 ▶ SOAP::Lite->import (+trace => $debug ? 'all' : '-all');
Branch: 1 branch
BranchTrueFalseTotal
$debug ? : 0 0 0
687 0 $self->_delay ();
688 ## $debug and SOAP::Trace->import ('all');
689 0 my $rslt = Astro::SIMBAD::Client::WSQueryInterfaceService->$method(
690 $self, @args);
691 0 ▶ return unless defined $rslt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rslt 0 0 0
692 0 ▶ HAVE_DOUBLE_ENCODED
Branch: 1 branch
BranchTrueFalseTotal
&HAVE_DOUBLE_ENCODED() and $rslt = XML::DoubleEncodedEntities::decode($rslt) 0 0 0
693 and $rslt = XML::DoubleEncodedEntities::decode ($rslt);
694 0 ▶ return wantarray ? ($parser->($rslt)) : [$parser->($rslt)]
Branch: 2 branches
BranchTrueFalseTotal
if $parser 0 0 0
wantarray ? : 0 0 0
695 if $parser;
696 0 return $rslt;
697 }
698  
699 } # End local symbol block.
700  
701 =item $value = $simbad->queryObjectByBib ($bibcode, $format, $type);
702  
703 This method is B<deprecated>, and will cease to work on December 31
704 2018. Please choose a method that does not use SOAP. See the
705 L<NOTICE|/NOTICE> above for details.
706  
707 This method is just a convenience wrapper for
708  
709 $value = $simbad->query (bib => $bibcode, $format, $type);
710  
711 See the query() documentation for more information.
712  
713 =cut
714  
715 5 ▶sub queryObjectByBib {
Subroutine queryObjectByBib: not called CC=1 SCAR=2.6
Pod documented
716 0 my $self = shift;
717 0 return $self->query (bib => @_);
718 }
719  
720 =item $value = $simbad->queryObjectByCoord ($coord, $radius, $format, $type);
721  
722 This method is B<deprecated>, and will cease to work on December 31
723 2018. Please choose a method that does not use SOAP. See the
724 L<NOTICE|/NOTICE> above for details.
725  
726 This method is just a convenience wrapper for
727  
728 $value = $simbad->query (coo => $coord, $radius, $format, $type);
729  
730 See the query() documentation for more information.
731  
732 =cut
733  
734 0 ▶sub queryObjectByCoord {
Subroutine queryObjectByCoord: not called CC=1 SCAR=6.9
Pod documented
735 0 my $self = shift;
736 0 return $self->query (coo => @_);
737 }
738  
739 =item $value = $simbad->queryObjectById ($id, $format, $type);
740  
741 This method is B<deprecated>, and will cease to work on December 31
742 2018. Please choose a method that does not use SOAP. See the
743 L<NOTICE|/NOTICE> above for details.
744  
745 This method is just a convenience wrapper for
746  
747 $value = $simbad->query (id => $id, $format, $type);
748  
749 See the query() documentation for more information.
750  
751 =cut
752  
753 0 ▶sub queryObjectById {
Subroutine queryObjectById: not called CC=1 SCAR=6.9
Pod documented
754 0 my $self = shift;
755 0 return $self->query (id => @_);
756 }
757  
758 =item $release = $simbad->release ();
759  
760 This method returns the current SIMBAD4 release, as scraped from the
761 top-level web page. This will look something like 'SIMBAD4 1.045 -
762 27-Jul-2007'
763  
764 If called in list context, it returns ($major, $minor, $point, $patch,
765 $date). The returned information corresponding to the scalar example
766 above is:
767  
768 $major => 4
769 $minor => 1
770 $point => 45
771 $patch => ''
772 $date => '27-Jul-2007'
773  
774 The $patch will usually be empty, but occasionally you get something
775 like release '1.019a', in which case $patch would be 'a'.
776  
777 Please note that this method is B<not> based on a published interface,
778 but is simply a web page scraper, and subject to all the problems such
779 software is heir to. What the algorithm attempts to do is to find (and
780 parse, if called in list context) the contents of the next E<lt>tdE<gt>
781 after 'Release:' (case-insensitive).
782  
783 =cut
784  
785 3 ▶sub release {
Subroutine release: called CC=6 SCAR=18.1
Pod documented
786 3 my $self = shift;
787 3 my $rslt = $self->_retrieve( 'simbad/' );
788 3 ▶ my ($rls) = $rslt->content =~
Branch: 1 branch
BranchTrueFalseTotal
my($rls) = $rslt->content =~ m[Release:.*?</td>.*?<td.*?>(.*?)</td>]six or croak("Error - Release information not found") 3 0 3
789 m{Release:.*?</td>.*?<td.*?>(.*?)</td>}sxi
790 or croak "Error - Release information not found";
791 3 $rls =~ s{<.*?>}{}g;
792 3 $rls =~ s/^\s+//;
793 3 $rls =~ s/\s+$//;
794 3 ▶ wantarray or return $rls;
Branch: 1 branch
BranchTrueFalseTotal
wantarray or return $rls 1 2 3
795 1 $rls =~ s/\s+-\s+/ /;
796 1 ▶ my ($major, $minor, $date) = split '\s+', $rls
Branch: 1 branch
BranchTrueFalseTotal
() = split(/\s+/, $rls, 4) or croak("Error - Release '${rls}' is ill-formed") 1 0 1
797 or croak "Error - Release '$rls' is ill-formed";
798 1 $major =~s/^\D+//;
799 1 $major += 0;
800 1 ($minor, my $point) = split '\.', $minor, 2;
801 1 $minor += 0;
802 1 ▶ ($point, my $patch) = $point =~ m/^(\d+)(.*)/
Branch: 1 branch
BranchTrueFalseTotal
($point, my $patch) = $point =~ /^(\d+)(.*)/ or croak("Error - Release '${rls}' is ill-formed: bad point") 1 0 1
803 or croak "Error - Release '$rls' is ill-formed: bad point";
804 1 ▶ defined $patch or $patch = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $patch or $patch = "" 1 0 1
805 1 $point += 0;
806 1 return ($major, $minor, $point, $patch, $date);
807 }
808  
809 =item $value = $simbad->script ($script);
810  
811 This method submits the given script to SIMBAD4. The $script variable
812 contains the text if the script; if you want to submit a script file
813 by name, use the script_file() method.
814  
815 If the L<verbatim|/verbatim> attribute is false, the front matter of the
816 result (up to and including the '::data:::::' line) is stripped. If
817 there is no '::data:::::' line, the entire script output is raised as an
818 exception.
819  
820 If a 'script' L<parser|/parser> was specified, the output of the script
821 (after stripping front matter if that was specified) is passed to it.
822 The parser is presumed to return a list, so if script() was called in
823 scalar context you get a reference to that list back.
824  
825 If no 'script' L<parser|/parser> is specified, the output of the script
826 (after stripping front matter if that was specified) is simply returned
827 to the caller.
828  
829 =cut
830  
831 2 ▶sub script {
Subroutine script: called CC=1 SCAR=0.0
Pod documented
832 2 my ( $self, $script ) = @_;
833 2 return $self->_script(
834 parser => 'script',
835 script => $script,
836 verbatim => $self->get( 'verbatim' ),
837 );
838 }
839  
840 {
841 my %dflt = (
842 0 ▶ parser => sub { return 'script' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
843 0 ▶ script => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
844 0 confess 'Programming error - script argument required';
845 },
846 0 ▶ verbatim => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
847 0 my ( $self ) = @_;
848 0 return $self->get( 'verbatim' );
849 },
850 5 );
851  
852 4 ▶ sub _script {
Subroutine _script: called CC=14 SCAR=32.3
853 4 my ( $self, %arg ) = @_;
854  
855 4 foreach my $key ( keys %dflt ) {
856 defined $arg{$key}
857 12 ▶ or $arg{$key} = $dflt{$key}->( $self );
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{$key} or $arg{$key} = $dflt{$key}->($self) 12 0 12
858 }
859  
860 4 my $debug = $self->get( 'debug' );
861  
862 4 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn qq[Debug - script\n$arg{"script"} ] 0 4 4
863 and warn "Debug - script\n$arg{script} ";
864  
865 my $resp = $self->_retrieve( 'simbad/sim-script', {
866 submit => 'submit+script',
867 script => $arg{script},
868 },
869 4 );
870  
871 4 ▶ my $rslt = $resp->content
Branch: 1 branch
BranchTrueFalseTotal
my $rslt = $resp->content or return 4 0 4
872 or return;
873  
874 4 ▶ unless ( $arg{verbatim} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg{"verbatim"}) 4 0 4
875 4 ▶ $rslt =~ s/.*?::data:+\s*//sm or croak $rslt;
Branch: 1 branch
BranchTrueFalseTotal
$rslt =~ s/.*?::data:+\s*//ms or croak($rslt) 4 0 4
876 }
877  
878 $debug
879 4 ▶ and warn "Debug - result:\n$rslt ";
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "Debug - result:\n$rslt " 0 4 4
880  
881 4 ▶ HAVE_DOUBLE_ENCODED
Branch: 1 branch
BranchTrueFalseTotal
&HAVE_DOUBLE_ENCODED() and $rslt = XML::DoubleEncodedEntities::decode($rslt) 0 4 4
882 and $rslt = XML::DoubleEncodedEntities::decode ($rslt);
883 4 ▶ if ( my $parser = $self->_get_parser( $arg{parser} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parser = $self->_get_parser($arg{'parser'})) { } 4 0 4
884 4 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn qq[Debug - Parser $arg{"parser"}] 0 4 4
885 and warn "Debug - Parser $arg{parser}";
886 ## $rslt =~ s/.*?::data:+.?$//sm or croak $rslt;
887 4 my @rslt = $parser->($rslt);
888 $debug
889 4 ▶ and eval { ## no critic (RequireCheckingReturnValueOfEval)
Branch: 1 branch
BranchTrueFalseTotal
$debug and eval { do { require YAML; warn "Debug - Parsed to:\n", YAML::Dump(\@rslt), " " } } 0 4 4
890 0 require YAML;
891 0 warn "Debug - Parsed to:\n", YAML::Dump( \@rslt ), ' ';
892 };
893 4 ▶ return wantarray ? @rslt : \@rslt;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
894 } else {
895 0 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn qq[Debug - No parser for $arg{"parser"}] 0 0 0
896 and warn "Debug - No parser for $arg{parser}";
897 0 return $rslt;
898 }
899 }
900 }
901  
902 =item $value = $simbad->script_file ($filename);
903  
904 This method submits the given script file to SIMBAD, returning the
905 result of the script. Unlike script(), the argument is the name of the
906 file containing the script, not the text of the script. However, if a
907 parser for 'script' has been specified, it will be applied to the
908 output.
909  
910 =cut
911  
912 5 ▶sub script_file {
Subroutine script_file: called CC=6 SCAR=19.1
Pod documented
913 2 my ( $self, $file ) = @_;
914  
915 2 my $url = $self->__build_url( 'simbad/sim-script' );
916 2 my $rqst = POST $url,
917 Content_Type => 'form-data',
918 Content => [
919 submit => 'submit file',
920 scriptFile => [$file, undef],
921 # May need to specify Content_Type => application/octet-stream.
922 ];
923 2 my $resp = $self->_retrieve( $rqst );
924  
925 2 ▶ my $rslt = $resp->content or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rslt = $resp->content or return 2 0 2
926 2 ▶ unless ($self->get ('verbatim')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get("verbatim")) 2 0 2
927 2 ▶ $rslt =~ s/.*?::data:+\s*//sm or croak $rslt;
Branch: 1 branch
BranchTrueFalseTotal
$rslt =~ s/.*?::data:+\s*//ms or croak($rslt) 2 0 2
928 }
929 2 ▶ if (my $parser = $self->_get_parser ('script')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parser = $self->_get_parser('script')) { } 2 0 2
930 ## $rslt =~ s/.*?::data:+.?$//sm or croak $rslt;
931 ## $rslt =~ s/\s+//sm;
932 2 my @rslt = $parser->($rslt);
933 2 ▶ return wantarray ? @rslt : \@rslt;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
934 } else {
935 0 return $rslt;
936 }
937  
938 }
939  
940 =item $simbad->set ($name => $value ...);
941  
942 This method sets the value of the given L<attributes|/Attributes>. More
943 than one name/value pair may be specified. If called as a static method,
944 it sets the default value of the attribute.
945  
946 =cut
947  
948 { # Begin local symbol block.
949  
950 8 ▶ my $ckpn = sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
951 8 ▶ (looks_like_number ($_[2]) && $_[2] >= 0)
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_[2]) and $_[2] >= 0 or croak("Attribute '$_[1]' must be a non-negative number") 8 0 8
Condition33%1/3
&looks_like_number($_[2]) and $_[2] >= 0
!ll&&!rl&&r
008
MC/DC0%0/2
&looks_like_number($_[2]) and $_[2] >= 0
&looks_like_number($_[2]) $_[2] >= 0
Truth table33%1/3
A and B
A= &looks_like_number($_[2])B= $_[2] >= 0
ABresult
0X0
100
111
952 or croak "Attribute '$_[1]' must be a non-negative number";
953 8 +$_[2];
954 5 };
955  
956 5 my %mutator = (
957 format => \&_set_hash,
958 parser => \&_set_hash,
959 scheme => \&_set_scheme,
960 url_args => \&_set_hash,
961 );
962  
963 5 my %server_map = (
964 harvard => 'simbad.cfa.harvard.edu',
965 strasbourg => 'simbad.u-strasbg.fr',
966 );
967  
968 my %transform = (
969 delay => ($have_time_hires ?
970 $ckpn :
971 0 ▶ sub {+sprintf '%d', $ckpn->(@_) + .5}),
Subroutine __ANON__: not called CC=1 SCAR=6.9
972 18 ▶ format => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
973 ## my ( $self, $name, $val, $key ) = @_;
974 18 my ( $self, undef, $val ) = @_; # Name and key unused
975 18 ▶ if ($val !~ m/\W/ && (my $code = eval {
Branch: 1 branch
BranchTrueFalseTotal
if (not $val =~ /\W/ and my $code = eval { do { $self->_get_coderef($val) } }) 2 16 18
Condition66%2/3
not $val =~ /\W/ and my $code = eval { do { $self->_get_coderef($val) } }
!ll&&!rl&&r
1602
MC/DC50%1/2
not $val =~ /\W/ and my $code = eval { do { $self->_get_coderef($val) } }
not $val =~ /\W/ my $code = eval { do { $self->_get_coderef($val) } }
Truth table66%2/3
not(A) and B
A= not $val =~ /\W/B= my $code = eval { do { $self->_get_coderef($val) } }
ABresult
0X0
100
111
976 2 $self->_get_coderef ($val)})) {
977 2 $val = $code->();
978 }
979 18 $val;
980 },
981 6 ▶ parser => sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
982 ## my ( $self, $name, $val, $key ) = @_;
983 6 my ( $self, undef, $val ) = @_; # Name and key unused
984 6 ▶ if (!ref $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $val) { } 6 0 6
elsif ('CODE' ne ref $val) { } 0 0 0
985 6 ▶ unless ($val =~ m/::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /::/) 2 4 6
986 2 my $pkg = $self->_parse_subroutine_name ($val);
987 2 $val = $pkg . '::' . $val;
988 }
989 6 $self->_get_coderef ($val); # Just to see if we can.
990 } elsif ( CODE_REF ne ref $val ) {
991 0 croak "Error - $_[1] value must be scalar or code reference";
992 }
993 6 $val;
994 },
995 19 ▶ server => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
996 ## my ( $self, $name, $val, $key ) = @_;
997 19 my ( undef, undef, $val ) = @_; # Invocant name and key unused
998 return ( defined( $val ) && exists $server_map{$val} ) ?
999 19 ▶ $server_map{$val} : $val;
Branch: 1 branch
BranchTrueFalseTotal
defined $val && exists $server_map{$val} ? : 0 19 19
Condition33%1/3
defined $val and exists $server_map{$val}
!ll&&!rl&&r
0190
MC/DC0%0/2
defined $val and exists $server_map{$val}
defined $val exists $server_map{$val}
Truth table33%1/3
A and B
A= defined $valB= exists $server_map{$val}
ABresult
0X0
100
111
1000 }
1001 5 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$have_time_hires ? : 5 0 5
1002  
1003 5 foreach my $key (keys %static) {
1004 62 ▶ $transform{$key} ||= sub {$_[2]};
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$transform{$key} ||= sub { $_[2]; }
l!l&&r!l&&!r
20450
MC/DC0%0/2
$transform{$key} ||= sub { $_[2]; }
$transform{$key} sub { $_[2]; }
Truth table66%2/3
A ||= B
A= $transform{$key}B= sub { $_[2]; }
ABresult
1X1
011
000
1005 89 ▶ $mutator{$key} ||= sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1006 89 ▶ my $hash = ref $_[0] ? $_[0] : \%static;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 89 0 89
1007 89 $hash->{$_[1]} = $transform{$_[1]}->(@_)
1008 65 ▶ };
Condition66%2/3
$mutator{$key} ||= sub { my $hash = ref $_[0] ? $_[0] : \%static; $$hash{$_[1]} = $transform{$_[1]}(@_); }
l!l&&r!l&&!r
20450
MC/DC0%0/2
$mutator{$key} ||= sub { my $hash = ref $_[0] ? $_[0] : \%static; $$hash{$_[1]} = $transform{$_[1]}(@_); }
$mutator{$key} sub { my $hash = ref $_[0] ? $_[0] : \%static; $$hash{$_[1]} = $transform{$_[1]}(@_); }
Truth table66%2/3
A ||= B
A= $mutator{$key}B= sub { my $hash = ref $_[0] ? $_[0] : \%static; $$hash{$_[1]} = $transform{$_[1]}(@_); }
ABresult
1X1
011
000
1009 }
1010  
1011 36 ▶ sub set {
Subroutine set: called CC=4 SCAR=14.2
Pod documented
1012 36 my ($self, @args) = @_;
1013 0 croak "Error - First argument must be an @{[__PACKAGE__]} object"
1014 36 ▶ unless eval {$self->isa(__PACKAGE__)};
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->isa(__PACKAGE__) } } 0 36 36
1015 36 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 132 36 168
1016 132 my $name = shift @args;
1017 croak "Error - Attribute '$name' is unknown"
1018 132 ▶ unless exists $mutator{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mutator{$name} 0 132 132
1019 132 $mutator{$name}->($self, $name, shift @args);
1020 }
1021 36 return $self;
1022 }
1023  
1024 32 ▶ sub _set_hash {
Subroutine _set_hash: called CC=8 SCAR=21.4
1025 32 my ($self, $name, $value) = @_;
1026 32 ▶ my $hash = ref $self ? $self : \%static;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 32 0 32
1027 32 ▶ unless (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 8 24 32
1028 8 ▶ $value = {$value =~ m/=/ ?
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /=/ ? : 8 0 8
1029 split ('=', $value, 2) : ($value => undef)};
1030 }
1031 32 ▶ $hash->{$name} = {} if $value->{clear};
Branch: 1 branch
BranchTrueFalseTotal
if $value->{"clear"} 0 32 32
1032 32 delete $value->{clear};
1033 32 foreach my $key (keys %$value) {
1034 56 my $val = $value->{$key};
1035 56 ▶ if (!defined $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 56 56
elsif ($val) { } 24 32 56
1036 0 delete $hash->{$name}{$key};
1037 } elsif ($val) {
1038 $hash->{$name}{$key} =
1039 24 $transform{$name}->($self, $name, $value->{$key}, $key);
1040 } else {
1041 32 $hash->{$name}{$key} = '';
1042 }
1043 }
1044 32 return;
1045 }
1046  
1047 11 ▶ sub _set_scheme {
Subroutine _set_scheme: called CC=3 SCAR=11.8
1048 11 my ( $self, $name, $value ) = @_;
1049 11 ▶ if ( my $msg = _is_scheme_valid( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = _is_scheme_valid($value)) 0 11 11
1050 0 croak $msg;
1051 }
1052 11 ▶ my $hash = ref $self ? $self : \%static;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 11 0 11
1053 11 $hash->{$name} = lc $value;
1054 11 return;
1055 }
1056  
1057 } # End local symbol block.
1058  
1059 =item $value = $simbad->url_query ($type => ...)
1060  
1061 This method performs a query by URL, returning the results. The type
1062 is one of:
1063  
1064 id = query by identifier,
1065 coo = query by coordinates,
1066 ref = query by references,
1067 sam = query by criteria.
1068  
1069 The arguments depend on on the type, and are documented at
1070 L<http://simbad.u-strasbg.fr/guide/sim-url.htx>. They are
1071 specified as name => value. For example:
1072  
1073 $simbad->url_query (id =>
1074 Ident => 'Arcturus',
1075 NbIdent => 1
1076 );
1077  
1078 Note that in an id query you must specify 'Ident' explicitly. This is
1079 true in general, because it is not always possible to derive the first
1080 argument name from the query type, and consistency was chosen over
1081 brevity.
1082  
1083 The output.format argument can be defaulted based on the object's type
1084 setting as follows:
1085  
1086 txt becomes 'ASCII',
1087 vo becomes 'VOTable'.
1088  
1089 Any other value is passed verbatim.
1090  
1091 If the query succeeds, the results will be passed to the appropriate
1092 parser if any. The reverse of the above translation is done to determine
1093 the appropriate parser, so the 'vo' parser (if any) is called if
1094 output.format is 'VOTable', and the 'txt' parser (if any) is called if
1095 output.format is 'ASCII'. If output.format is 'HTML', you will need to
1096 explicitly set up a parser for that.
1097  
1098 The type of HTTP interaction depends on the setting of the L<post|/post>
1099 attribute: if true a POST is done; otherwise all arguments are tacked
1100 onto the end of the URL and a GET is done.
1101  
1102 =cut
1103  
1104 { # Begin local symbol block.
1105  
1106 5 my %type_map = ( # Map SOAP type parameter to URL output.format.
1107 txt => 'ASCII',
1108 vo => 'VOTable',
1109 );
1110 5 my %type_unmap = reverse %type_map;
1111  
1112 # Perl::Critic objects to the use of @_ (rather than values
1113 # unpacked from it) but the parity check lets me give a less
1114 # unfriendly error message. CAVEAT: do NOT modify the contents
1115 # of @_, since this will be seen by the caller. Modifying @_
1116 # itself is fine.
1117 0 ▶ sub url_query { ## no critic (RequireArgUnpacking)
Subroutine url_query: not called CC=10 SCAR=47.0
Pod documented
1118 0 ▶ @_ % 2 and croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and croak("Error - url_query needs an even number of arguments after the query\n type.\n") 0 0 0
1119 Error - url_query needs an even number of arguments after the query
1120 type.
1121 eod
1122 0 my ($self, $query, %args) = @_;
1123 ### my $debug = $self->get ('debug');
1124 0 my $dflt = $self->get ('url_args');
1125 0 foreach my $key (keys %$dflt) {
1126 0 ▶ exists ($args{$key}) or $args{$key} = $dflt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$key} or $args{$key} = $dflt->{$key} 0 0 0
1127 }
1128 0 ▶ unless ($args{'output.format'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"output.format"}) 0 0 0
1129 0 my $type = $self->get ('type');
1130 0 ▶ $args{'output.format'} = $type_map{$type} || $type;
Condition0%0/3
$type_map{$type} || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$type_map{$type} || $type
$type_map{$type} $type
Truth table0%0/3
A || B
A= $type_map{$type}B= $type
ABresult
1X1
011
000
1131 }
1132 0 my $resp = $self->_retrieve( "simbad/sim-$query", \%args );
1133  
1134 0 my $rslt = $resp->content();
1135  
1136 0 ▶ HAVE_DOUBLE_ENCODED
Branch: 1 branch
BranchTrueFalseTotal
&HAVE_DOUBLE_ENCODED() and $rslt = XML::DoubleEncodedEntities::decode($rslt) 0 0 0
1137 and $rslt = XML::DoubleEncodedEntities::decode( $rslt );
1138  
1139 0 my $parser;
1140 0 ▶ if (my $type = $type_unmap{$args{'output.format'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $type_unmap{$args{"output.format"}}) 0 0 0
1141 0 $parser = $self->_get_parser ($type);
1142 0 ▶ return wantarray ? ($parser->($rslt)) : [$parser->($rslt)]
Branch: 2 branches
BranchTrueFalseTotal
if $parser 0 0 0
wantarray ? : 0 0 0
1143 if $parser;
1144 }
1145  
1146 0 return $rslt;
1147 }
1148  
1149 } # End local symbol block.
1150  
1151 ########################################################################
1152 #
1153 # Utility routines
1154 #
1155  
1156 # __build_url
1157 #
1158 # Builds a URL based on the currently-set scheme and server, and
1159 # the fragment provided as an argument. If the fragment is an
1160 # HTTP::Request object it is simply returned.
1161  
1162 5 ▶sub __build_url {
Subroutine __build_url: called CC=3 SCAR=11.0
1163 20 my ( $self, $fragment ) = @_;
1164 20 ▶ defined $fragment
Branch: 1 branch
BranchTrueFalseTotal
defined $fragment or $fragment = "" 19 1 20
1165 or $fragment = '';
1166 20 ▶ eval { $fragment->isa( 'HTTP::Request' ) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $fragment->isa("HTTP::Request") } } and return $fragment 2 18 20
1167 and return $fragment;
1168 18 $fragment =~ s< \A / ><>smx; # Defensive programming
1169 18 return sprintf '%s://%s/%s', $self->get( 'scheme' ),
1170 $self->get( 'server' ), $fragment;
1171 }
1172  
1173 # _callers_caller();
1174 #
1175 # Returns the name of the subroutine that called the caller.
1176 # Results undefined if not called from a subroutine nested at
1177 # least two deep.
1178  
1179 0 ▶sub _callers_caller {
Subroutine _callers_caller: not called CC=3 SCAR=24.8
1180 0 my $inx = 1;
1181 0 my $caller;
1182 0 foreach ( 1 .. 2 ) {
1183 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
"(eval)" eq $caller and do { do { $caller = (caller $inx++)[3] } } 0 0 0
1184 0 $caller = ( caller $inx++ )[3]
1185 } while '(eval)' eq $caller;
1186 }
1187 0 return $caller;
1188 }
1189  
1190 # $self->_delay
1191 #
1192 # Delays the desired amount of time before issuing the next
1193 # query.
1194  
1195 {
1196 5 my %last;
1197 9 ▶ sub _delay {
Subroutine _delay: called CC=3 SCAR=11.0
1198 9 my $self = shift;
1199 9 ▶ my $last = $last{$self->{server}} ||= 0;
Condition100%2/2
$last{$self->{"server"}} ||= 0
l!l
63
MC/DC100%1/1
$last{$self->{"server"}} ||= 0
$last{$self->{"server"}}
Truth table100%2/2
A
A= $last{$self->{"server"}}
Aresult
11
00
1200 9 ▶ if ((my $delay = $last + $self->{delay} - time) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $delay = $last + $self->{"delay"} - &Astro::SIMBAD::Client::time()) > 0) 6 3 9
1201 6 sleep ($delay);
1202 }
1203 9 return ($last{$self->{server}} = time);
1204 }
1205 }
1206  
1207 # $self->_deprecation_notice( $type, $name );
1208 #
1209 # This method centralizes deprecation. Type is 'attribute' or
1210 # 'method'. Deprecation is driven of the %deprecate hash. Values
1211 # are:
1212 # false - no warning
1213 # 1 - warn on first use
1214 # 2 - warn on each use
1215 # 3 - die on each use.
1216 #
1217 # $self->_deprecation_in_progress( $type, $name )
1218 #
1219 # This method returns true if the deprecation is in progress. In
1220 # practice this means the %deprecate value is defined.
1221 # This is currently unused and commented out
1222  
1223 {
1224  
1225 5 my %deprecate = (
1226 attribute => {
1227 scheme => {
1228 http => 1,
1229 },
1230 },
1231 method => {
1232 query => 2,
1233 },
1234 );
1235  
1236 17 ▶ sub _deprecation_notice {
Subroutine _deprecation_notice: called CC=12 SCAR=45.6
1237 17 my ( undef, $type, $name, $repl ) = @_; # Invocant unused
1238 17 ▶ $deprecate{$type} or return;
Branch: 1 branch
BranchTrueFalseTotal
$deprecate{$type} or return 12 5 17
1239 12 ▶ $deprecate{$type}{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
$deprecate{$type}{$name} or return 0 12 12
1240 0 my $msg;
1241 0 ▶ if ( ref( $deprecate{$type}{$name} ) eq HASH_REF ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $deprecate{$type}{$name} eq 'HASH') { } 0 0 0
1242 0 ▶ $deprecate{$type}{$name}{$repl}
Branch: 1 branch
BranchTrueFalseTotal
$deprecate{$type}{$name}{$repl} or return 0 0 0
1243 or return;
1244 $msg = sprintf 'The %s %s value %s is %s', $name, $type, $repl,
1245 0 ▶ $deprecate{$type}{$name} > 2 ? 'removed' : 'deprecated';
Branch: 1 branch
BranchTrueFalseTotal
$deprecate{$type}{$name} > 2 ? : 0 0 0
1246 $deprecate{$type}{$name}{$repl} == 1
1247 0 ▶ and $deprecate{$type}{$name}{$repl} = 0;
Branch: 1 branch
BranchTrueFalseTotal
$deprecate{$type}{$name}{$repl} == 1 and $deprecate{$type}{$name}{$repl} = 0 0 0 0
1248 } else {
1249 $msg = sprintf 'The %s %s is %s', $name, $type,
1250 0 ▶ $deprecate{$type}{$name} > 2 ? 'removed' : 'deprecated';
Branch: 1 branch
BranchTrueFalseTotal
$deprecate{$type}{$name} > 2 ? : 0 0 0
1251 0 ▶ defined $repl
Branch: 1 branch
BranchTrueFalseTotal
defined $repl and $msg .= "; use $repl instead" 0 0 0
1252 and $msg .= "; use $repl instead";
1253 $deprecate{$type}{$name} == 1
1254 0 ▶ and $deprecate{$type}{$name} = 0;
Branch: 1 branch
BranchTrueFalseTotal
$deprecate{$type}{$name} == 1 and $deprecate{$type}{$name} = 0 0 0 0
1255 }
1256 0 ▶ $deprecate{$type}{$name} >= 3
Branch: 1 branch
BranchTrueFalseTotal
$deprecate{$type}{$name} >= 3 and croak($msg) 0 0 0
1257 and croak( $msg );
1258 0 ▶ warnings::enabled( 'deprecated' )
Branch: 1 branch
BranchTrueFalseTotal
warnings::enabled("deprecated") and carp($msg) 0 0 0
1259 and carp( $msg );
1260 0 return;
1261 }
1262  
1263 =begin comment
1264  
1265 sub _deprecation_in_progress {
1266 my ( undef, $type, $name ) = @_; # Invocant unused
1267 $deprecate{$type} or return;
1268 return defined $deprecate{$type}{$name};
1269 }
1270  
1271 =end comment
1272  
1273 =cut
1274  
1275 }
1276  
1277 # $ref = $self->_get_coderef ($string)
1278 #
1279 # Translates the given string into a code reference, loading
1280 # modules if needed. If the string is not a fully-qualified
1281 # subroutine name, it is assumed to be in the namespace of
1282 # the first caller not in this namespace. Failed loads are
1283 # cached so that they will not be tried again.
1284  
1285 {
1286  
1287 5 ▶ sub _get_coderef {
Subroutine _get_coderef: called CC=6 SCAR=21.4
1288 14 my $self = shift;
1289 14 my $parser = shift;
1290 14 ▶ if ($parser && !ref $parser) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser and not ref $parser) 14 0 14
Condition33%1/3
$parser and not ref $parser
!ll&&!rl&&r
0014
MC/DC0%0/2
$parser and not ref $parser
$parser not ref $parser
Truth table33%1/3
A and not(B)
A= $parserB= not ref $parser
ABresult
0X0
100
111
1291 14 my ($pkg, $code) =
1292 $self->_parse_subroutine_name ($parser);
1293 14 ▶ unless (($parser = $pkg->can ($code)) || !$self->get ('autoload')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parser = $pkg->can($code) or not $self->get("autoload")) 0 14 14
Condition33%1/3
$parser = $pkg->can($code) or not $self->get("autoload")
l!l&&r!l&&!r
1400
MC/DC0%0/2
$parser = $pkg->can($code) or not $self->get("autoload")
$parser = $pkg->can($code) not $self->get("autoload")
Truth table33%1/3
A or not(B)
A= $parser = $pkg->can($code)B= not $self->get("autoload")
ABresult
1X1
011
000
1294 0 _load_module ($pkg);
1295 0 $parser = $pkg->can ($code);
1296 }
1297 14 ▶ $parser or croak "Error - ${pkg}::$code undefined";
Branch: 1 branch
BranchTrueFalseTotal
$parser or croak("Error - ${pkg}::$code undefined") 14 0 14
1298 }
1299 14 return $parser;
1300 }
1301  
1302 }
1303  
1304 # $parser = $self->_get_parser ($type)
1305  
1306 # returns the code reference to the parser for the given type of
1307 # data, or false if none. An exception is thrown if the value
1308 # is a string which does not specify a defined subroutine.
1309  
1310 5 ▶sub _get_parser {
Subroutine _get_parser: called CC=1 SCAR=0.0
1311 6 my ($self, $type) = @_;
1312 6 return $self->_get_coderef ($self->get ('parser')->{$type});
1313 }
1314  
1315 # Return false if the argument is a URI scheme we know how to deal with;
1316 # otherwise return an error message. The optional second argument is a
1317 # template for the message, with a single '%s' that gets the actual
1318 # value of the scheme.
1319  
1320 {
1321 5 my %supported;
1322  
1323 5 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
1324 5 %supported = map { $_ => 1 } qw{ http https };
1325 }
1326  
1327 17 ▶ sub _is_scheme_valid {
Subroutine _is_scheme_valid: called CC=5 SCAR=17.5
1328 17 my ( $scheme, $msg ) = @_;
1329 17 ▶ $scheme = lc( $scheme || '' );
Condition50%1/2
$scheme || ''
l!l
170
MC/DC0%0/1
$scheme || ''
$scheme
Truth table50%1/2
A
A= $scheme
Aresult
11
00
1330 17 __PACKAGE__->_deprecation_notice( attribute => 'query', 'http' );
1331 17 ▶ $msg ||= q<Unsupported scheme '%s'>;
Condition100%2/2
$msg ||= "Unsupported scheme '%s'"
l!l
512
MC/DC100%1/1
$msg ||= "Unsupported scheme '%s'"
$msg
Truth table100%2/2
A
A= $msg
Aresult
11
00
1332 17 ▶ $supported{$scheme}
Branch: 1 branch
BranchTrueFalseTotal
$supported{$scheme} or return sprintf($msg, $scheme) 17 0 17
1333 or return sprintf $msg, $scheme;
1334 17 ▶ LWP::Protocol::implementor( $scheme )
Branch: 1 branch
BranchTrueFalseTotal
LWP::Protocol::implementor($scheme) and return 17 0 17
1335 and return;
1336 0 $msg .= "; have you installed LWP::Protocol::$scheme?";
1337 0 return sprintf $msg, $scheme;
1338 }
1339 }
1340  
1341 # $rslt = _load_module($name)
1342 #
1343 # This subroutine loads the named module using 'require'. It
1344 # croaks if the load fails, or returns the result of the
1345 # 'require' if it succeeds. Results are cached, so subsequent
1346 # calls simply do what the first one did.
1347  
1348 { # Local symbol block. Oh, for 5.10 and state variables.
1349 5 my %error;
1350 5 my %rslt;
1351 10 ▶ sub _load_module {
Subroutine _load_module: called CC=4 SCAR=15.2
1352 10 my ($module) = @_;
1353 10 ▶ exists $error{$module} and croak $error{$module};
Branch: 1 branch
BranchTrueFalseTotal
exists $error{$module} and croak($error{$module}) 0 10 10
1354 10 ▶ exists $rslt{$module} and return $rslt{$module};
Branch: 1 branch
BranchTrueFalseTotal
exists $rslt{$module} and return $rslt{$module} 0 10 10
1355 10 $rslt{$module} = eval "require $module";
1356 10 ▶ $@ and croak ($error{$module} = $@);
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak($error{$module} = $@) 10 0 10
1357 0 return $rslt{$module};
1358 }
1359 } # End local symbol block.
1360  
1361 # $ua = $self->_get_user_agent();
1362 #
1363 # This subroutine returns an LWP::UserAgent object with its agent
1364 # string set to the default, with our class name and version
1365 # appended in parentheses.
1366  
1367 5 ▶sub _get_user_agent {
Subroutine _get_user_agent: called CC=1 SCAR=0.0
1368 9 my ( $self ) = @_;
1369 9 my $ua = LWP::UserAgent->new(
1370 agent => $self->agent(),
1371 timeout => $self->get( 'timeout' ),
1372 );
1373 9 return $ua;
1374 }
1375  
1376 # ($package, $subroutine) = $self->_parse_subroutine_name ($name);
1377 #
1378 # This method parses the given name, and returns the package name
1379 # in which the subroutine is defined and the subroutine name. If
1380 # the $name is a bare subroutine name, the package is the calling
1381 # package unless that package contains no such subroutine but
1382 # $self->can($name) is true, in which case the package is
1383 # ref($self).
1384 #
1385 # If called in scalar context, the package is returned.
1386  
1387 16 ▶sub _parse_subroutine_name {
Subroutine _parse_subroutine_name: called CC=9 SCAR=22.3
1388 16 my ($self, $parser) = @_;
1389 16 my @parts = split '::', $parser;
1390 16 my $code = pop @parts;
1391 16 my $pkg = join '::', @parts;
1392 16 ▶ unless ($pkg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg) 4 12 16
1393 4 my %tried = (__PACKAGE__, 1);
1394 4 my $inx = 1;
1395 4 ▶ while ($pkg = (caller ($inx++))[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg = (caller $inx++)[0]) 28 4 32
1396 28 ▶ next if $tried{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if $tried{$pkg} 18 10 28
1397 10 $tried{$pkg} = 1;
1398 10 ▶ last if $pkg->can ($code);
Branch: 1 branch
BranchTrueFalseTotal
if $pkg->can($code) 0 10 10
1399 }
1400 4 ▶ $pkg = ref $self if !$pkg && $self->can ($code);
Branch: 1 branch
BranchTrueFalseTotal
if not $pkg and $self->can($code) 4 0 4
Condition33%1/3
not $pkg and $self->can($code)
!ll&&!rl&&r
004
MC/DC0%0/2
not $pkg and $self->can($code)
not $pkg $self->can($code)
Truth table33%1/3
not(A) and B
A= not $pkgB= $self->can($code)
ABresult
0X0
100
111
1401 4 ▶ defined $pkg or croak <<eod;
Branch: 1 branch
BranchTrueFalseTotal
defined $pkg or croak("Error - '${parser}' yields undefined package name.\n") 4 0 4
1402 Error - '$parser' yields undefined package name.
1403 eod
1404 4 @parts = split '::', $pkg;
1405 }
1406 16 ▶ return wantarray ? ($pkg, $code) : $pkg;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 2 16
1407 }
1408  
1409 # my $resp = $self->_retrieve( $fragment, \%args );
1410 #
1411 # Build a URL from the contents of the 'scheme' and 'server'
1412 # attributes, and the given fragment, and retrieve the data from
1413 # that URL. The \%args argument is optional.
1414 #
1415 # The return is an HTTP::Response object. If the response is
1416 # indicates that the request is unsuccessful we croak with the URL
1417 # (if that can be retrieved) and the status line.
1418 #
1419 # The details depend on the arguments and the state of the
1420 # invocant as follows:
1421 #
1422 # If $url is an HTTP::Request object, it is executed and the
1423 # response returned. Otherwise
1424 #
1425 # If \%args is present and not empty, and the 'post' attribute is
1426 # true, an HTTP post() request is done to the URL, sending the
1427 # data. Otherwise
1428 #
1429 # If there are arguments they are appended to the URL, and an HTTP
1430 # get() is done to the URL.
1431  
1432 9 ▶sub _retrieve {
Subroutine _retrieve: called CC=13 SCAR=29.6
1433 9 my ($self, $fragment, $args) = @_;
1434 9 my $url = $self->__build_url( $fragment );
1435 9 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
45
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
1436 9 my $debug = $self->get( 'debug' );
1437 9 my $ua = $self->_get_user_agent();
1438 9 $self->_delay();
1439 9 my $resp;
1440 9 ▶ if (eval {$url->isa('HTTP::Request')}) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { $url->isa('HTTP::Request') } }) { } 2 7 9
elsif ($self->get('post') and %$args) { } 4 3 7
1441 2 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "Debug ", _callers_caller(), "executing ", $url->as_string, "\n" 0 2 2
1442 and print 'Debug ', _callers_caller(), 'executing ',
1443 $url->as_string, "\n";
1444 2 $resp = $ua->request ($url);
1445 ▶ } elsif ($self->get ('post') && %$args) {
Condition66%2/3
$self->get("post") and %$args
!ll&&!rl&&r
034
MC/DC50%1/2
$self->get("post") and %$args
$self->get("post") %$args
Truth table66%2/3
A and B
A= $self->get("post")B= %$args
ABresult
0X0
100
111
1446 4 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 4 4
1447 0 print 'Debug ', _callers_caller(), " posting to $url\n";
1448 0 foreach my $key (sort keys %$args) {
1449 0 print " $key => $args->{$key}\n";
1450 }
1451 }
1452 4 $resp = $ua->post ($url, $args);
1453 } else {
1454 3 my $join = '?';
1455 3 foreach my $key (sort keys %$args) {
1456 $url .= $join . _escape_uri( $key ) . '=' . _escape_uri (
1457 0 $args->{$key} );
1458 0 $join = '&';
1459 }
1460 $debug
1461 3 ▶ and print 'Debug ', _callers_caller(), " getting from $url\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "Debug ", _callers_caller(), " getting from $url\n" 0 3 3
1462 3 $resp = $ua->get( $url );
1463 }
1464 9 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "Debug - request: ", $resp->request->as_string, "\n" 0 9 9
1465 and print 'Debug - request: ', $resp->request()->as_string(), "\n";
1466  
1467 9 ▶ $resp->is_success()
Branch: 1 branch
BranchTrueFalseTotal
$resp->is_success and return $resp 9 0 9
1468 and return $resp;
1469  
1470 0 ▶ my $rq = $resp->request()
Branch: 1 branch
BranchTrueFalseTotal
my $rq = $resp->request or croak($resp->status_line) 0 0 0
1471 or croak $resp->status_line();
1472 0 my $u = $rq->uri();
1473 0 croak "$u: ", $resp->status_line();
1474 }
1475  
1476 5 1;
1477  
1478 __END__
↑ Summary Client/WSQueryInterfaceService.pm »