Statistics/R/IO/Base.pm

filter: Statementstmt 98.3% 59 / 60 Branchbran 84.3% 27 / 32 Conditioncond 75.0% 9 / 12 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 13 / 13 Podpod 25.0% 1 / 4 totaltotal 88.1% 111 / 126 CC 21 SCAR 17.3
CC
21
Cov
91%
BUILD
9.6
BUILDARGS
7.1
_read_and_uncompress
6.0
CRAP 5.6
« Statistics/R/REXP/S4.pm ↑ Summary Statistics/R/REXP/Environment.pm »
1 package Statistics::R::IO::Base;
2 # ABSTRACT: Common object methods for processing R files
3 2 $Statistics::R::IO::Base::VERSION = '1.0002';
4 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use IO::Uncompress::Gunzip ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IO::Uncompress::Bunzip2 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Class::Tiny::Antlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 2 has fh => (
18 is => 'ro',
19 );
20  
21  
22 534 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=7 SCAR=19.6
Pod undocumented
23 534 my $class = shift;
24 534 ▶ if ( scalar @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 530 4 534
elsif (@_ % 2) { } 0 4 4
25 530 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 530 0 530
26 530 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 233 297 530
elsif (ref $_[0] eq '') { } 295 2 297
27 233 return { %{ $_[0] } }
28 } elsif (ref $_[0] eq '') {
29 295 my $name = shift;
30 295 ▶ die "No such file '$name'" unless -r $name;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $name 2 293 295
31 293 my $fh = IO::File->new($name);
32 293 binmode $fh;
33 293 return { fh => $fh }
34 }
35 }
36 2 die "Single parameters to new() must be a HASH ref or filename scalar"
37 }
38 elsif ( @_ % 2 ) {
39 0 die "The new() method for $class expects a hash reference or a key/value list."
40 . " You passed an odd number of arguments\n";
41 }
42 else {
43 4 return {@_}
44 }
45 }
46  
47  
48 530 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=22.7
Pod undocumented
49 530 my ($self, $args) = @_;
50  
51 530 ▶ die "This is an abstract class and must be subclassed" if ref($self) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq __PACKAGE__ 0 530 530
52  
53 # Required methods
54 530 ▶ die "'read' method required" unless $self->can('read');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("read") 0 530 530
55  
56 # Required attribute types
57 530 ▶ die "Attribute 'fh' is required" unless defined($args->{fh});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"fh"} 2 528 530
58
59 die "Attribute 'fh' must be an instance of IO::Handle or an open filehandle" if
60 defined($args->{fh}) &&
61 !((ref($args->{fh}) eq "GLOB" && Scalar::Util::openhandle($args->{fh})) ||
62 528 ▶ (blessed($args->{fh}) && $args->{fh}->isa("IO::Handle")));
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"fh"} and not ref $args->{"fh"} eq "GLOB" && &Scalar::Util::openhandle($args->{"fh"}) || builtin::blessed($args->{"fh"}) && $args->{"fh"}->isa("IO::Handle") 2 526 528
Condition75%9/12
defined $args->{"fh"} and not ref $args->{"fh"} eq "GLOB" && &Scalar::Util::openhandle($args->{"fh"}) || builtin::blessed($args->{"fh"}) && $args->{"fh"}->isa("IO::Handle")
!ll&&!rl&&r
05262
ref $args->{'fh'} eq 'GLOB' && &Scalar::Util::openhandle($args->{'fh'}) || builtin::blessed($args->{'fh'}) && $args->{'fh'}->isa('IO::Handle')
l!l&&r!l&&!r
2332932
ref $args->{"fh"} eq "GLOB" and &Scalar::Util::openhandle($args->{"fh"})
!ll&&!rl&&r
2950233
builtin::blessed($args->{"fh"}) and $args->{"fh"}->isa("IO::Handle")
!ll&&!rl&&r
20293
MC/DC40%2/5
defined $args->{"fh"} and not ref $args->{"fh"} eq "GLOB" && &Scalar::Util::openhandle($args->{"fh"}) || builtin::blessed($args->{"fh"}) && $args->{"fh"}->isa("IO::Handle")
defined $args->{"fh"} ref $args->{"fh"} eq "GLOB" &Scalar::Util::openhandle($args->{"fh"}) builtin::blessed($args->{"fh"}) $args->{"fh"}->isa("IO::Handle")
Truth table37%3/8
A and not(B and C || D and E)
A= defined $args->{"fh"}B= ref $args->{"fh"} eq "GLOB"C= &Scalar::Util::openhandle($args->{"fh"})D= builtin::blessed($args->{"fh"})E= $args->{"fh"}->isa("IO::Handle")
ABCDEresult
0XXXX0
111XX0
10X110
110110
10X0X1
10X101
1100X1
110101
63 }
64  
65 526 ▶sub _read_and_uncompress {
Subroutine _read_and_uncompress: called CC=6 SCAR=17.9
66 526 my $self = shift;
67
68 526 my ($data, $rc) = '';
69 526 ▶ while ($rc = $self->fh->read($data, 8192, length $data)) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($rc = $self->fh->read($data, 8192, length $data)) 526 526 1052
70 526 ▶ croak $! unless defined $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 526 526
71 526 ▶ if (substr($data, 0, 2) eq "\x1f\x8b") {
Branch: 3 branches
BranchTrueFalseTotal
if (substr($data, 0, 2) eq "\037\213") { } 120 406 526
elsif (substr($data, 0, 3) eq 'BZh') { } 120 286 406
elsif (substr($data, 0, 6) eq "\3757zXZ\000") { } 60 226 286
72 ## gzip-compressed file
73 120 my $input = $data;
74 120 IO::Uncompress::Gunzip::gunzip \$input, \$data;
75 }
76 elsif (substr($data, 0, 3) eq 'BZh') {
77 ## bzip2-compressed file
78 120 my $input = $data;
79 120 IO::Uncompress::Bunzip2::bunzip2 \$input, \$data;
80 }
81 elsif (substr($data, 0, 6) eq "\xfd7zXZ\0") {
82 60 croak "xz-compressed R files are not supported";
83 }
84  
85 $data
86 466 }
87  
88  
89 994 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
90 994 my $self = shift;
91 994 $self->fh->close
92 }
93  
94  
95 530 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=6.9
Pod undocumented
96 530 my $self = shift;
97 ## TODO: should only close if given a filename (OR autoclose, if I
98 ## choose to implement it)
99 530 ▶ $self->close if $self->fh
Branch: 1 branch
BranchTrueFalseTotal
if $self->fh 528 2 530
100 }
101  
102  
103 # sub eof {
104 # my $self = shift;
105 # $self->position >= scalar @{$self->data};
106 # }
107  
108
109 2 1;
110  
111 __END__
« Statistics/R/REXP/S4.pm ↑ Summary Statistics/R/REXP/Environment.pm »