blib/lib/Test/GetVolatileData.pm

filter: Statementstmt 74.3% 29 / 39 Branchbran 28.5% 4 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 66.6% 40 / 60 CC 9 SCAR 28.8
CC
9
Cov
62%
get_data
27.0
BEGIN
1.0
BEGIN
1.0
CRAP 17.7
↑ Summary
1 package Test::GetVolatileData;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 require Exporter;
10  
11 2 our @ISA = qw(Exporter);
12 2 our @EXPORT = qw(get_data);
13  
14 2 our $VERSION = '2.001001';
15  
16 2 our $ERROR;
17  
18 6 ▶sub get_data {
Subroutine get_data: called CC=9 SCAR=33.0
Pod documented
19 6 undef $ERROR;
20  
21 6 my $url = shift;
22 6 my %args = @_;
23  
24 6 ▶ $args{num} = 1 unless exists $args{num};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"num"} 2 4 6
25 6 ▶ $args{num} =~ /\A\d+\z/
Branch: 1 branch
BranchTrueFalseTotal
$args{"num"} =~ /\A\d+\z/ or croak("'num' argument can take only positive integers") 6 0 6
26 or croak q{'num' argument can take only positive integers};
27  
28 6 my $res = LWP::UserAgent->new(
29 agent => 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:26.0) '
30 . 'Gecko/20100101 Firefox/26.0',
31 timeout => 30,
32 )->get( $url );
33  
34 6 ▶ unless ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 6 0 6
35 6 $ERROR = 'Network error: ' . $res->status_line;
36 6 return;
37 }
38  
39 0 my $c = $res->decoded_content;
40 0 ▶ my @data = split /\n/, defined($c) ? $c : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $c ? : 0 0 0
41 0 ▶ @data or do { $ERROR = 'Got empty data'; return };
Branch: 1 branch
BranchTrueFalseTotal
@data or do { $Test::GetVolatileData::ERROR = "Got empty data"; return } 0 0 0
42 0 my @return;
43 0 for ( 1.. $args{num} ) {
44 0 ▶ @data or last;
Branch: 1 branch
BranchTrueFalseTotal
@data or last 0 0 0
45 0 push @return, splice @data, int(rand @data), 1;
46 }
47  
48 0 ▶ return @return > 1 ? @return : $return[0];
Branch: 1 branch
BranchTrueFalseTotal
@return > 1 ? : 0 0 0
49 }
50  
51  
52 2 q|
53 Knock knock.
54  
55 Race condition.
56  
57 Who's there?
58 |;
59  
60 __END__
↑ Summary