File Coverage

blib/lib/Statistics/R/IO.pm
Criterion Covered Total %
statement 65 66 98.4
branch 21 30 70.0
condition 1 2 50.0
subroutine 13 13 100.0
pod 3 3 100.0
total 103 114 90.3


line stmt bran cond sub pod time code
1             package Statistics::R::IO;
2             # ABSTRACT: Perl interface to serialized R data
3             $Statistics::R::IO::VERSION = '1.0';
4 4     4   2673 use 5.010;
  4         12  
5 4     4   14 use strict;
  4         3  
  4         78  
6 4     4   12 use warnings FATAL => 'all';
  4         2  
  4         105  
7              
8 4     4   11 use Exporter 'import';
  4         4  
  4         217  
9              
10             our @EXPORT = qw( );
11             our @EXPORT_OK = qw( readRDS readRData evalRserve );
12              
13             our %EXPORT_TAGS = ( all => [ @EXPORT_OK ], );
14              
15 4     4   1689 use Statistics::R::IO::REXPFactory;
  4         7  
  4         172  
16 4     4   1584 use Statistics::R::IO::Rserve;
  4         9  
  4         103  
17 4     4   2224 use IO::Uncompress::Gunzip ();
  4         112268  
  4         91  
18 4     4   1985 use IO::Uncompress::Bunzip2 ();
  4         12471  
  4         68  
19 4     4   21 use IO::Socket::INET ();
  4         4  
  4         57  
20 4     4   11 use Carp;
  4         7  
  4         1699  
21              
22              
23             sub readRDS {
24 265 50   265 1 149333 open (my $f, shift) or croak $!;
25 265         590 binmode $f;
26 265         433 my ($data, $rc) = '';
27 265         3456 while ($rc = read($f, $data, 8192, length $data)) {}
28 265 50       451 croak $! unless defined $rc;
29 265 100       958 if (substr($data, 0, 2) eq "\x1f\x8b") {
    100          
    100          
30             ## gzip-compressed file
31 53         122 seek($f, 0, 0);
32 53         263 IO::Uncompress::Gunzip::gunzip $f, \$data;
33             }
34             elsif (substr($data, 0, 3) eq 'BZh') {
35             ## bzip2-compressed file
36 53         133 seek($f, 0, 0);
37 53         237 IO::Uncompress::Bunzip2::bunzip2 $f, \$data;
38             }
39             elsif (substr($data, 0, 6) eq "\xfd7zXZ\0") {
40 53         6537 croak "xz-compressed RDS files are not supported";
41             }
42            
43 212         99669 my ($value, $state) = @{Statistics::R::IO::REXPFactory::unserialize($data)};
  212         523  
44 212 50       365 croak 'Could not parse RDS file' unless $state;
45 212 50       386 croak 'Unread data remaining in the RDS file' unless $state->eof;
46 212         1056 $value
47             }
48              
49              
50             sub readRData {
51 28 50   28 1 43399 open (my $f, shift) or croak $!;
52 28         81 binmode $f;
53 28         56 my ($data, $rc) = '';
54 28         532 while ($rc = read($f, $data, 8192, length $data)) {}
55 28 50       74 croak $! unless defined $rc;
56 28 100       173 if (substr($data, 0, 2) eq "\x1f\x8b") {
    100          
    100          
57             ## gzip-compressed file
58 7         33 seek($f, 0, 0);
59 7         55 IO::Uncompress::Gunzip::gunzip $f, \$data;
60             }
61             elsif (substr($data, 0, 3) eq 'BZh') {
62             ## bzip2-compressed file
63 7         21 seek($f, 0, 0);
64 7         39 IO::Uncompress::Bunzip2::bunzip2 $f, \$data;
65             }
66             elsif (substr($data, 0, 6) eq "\xfd7zXZ\0") {
67 7         1283 croak "xz-compressed RData files are not supported";
68             }
69            
70 21 50       18082 if (substr($data, 0, 5) ne "RDX2\n") {
71 0         0 croak 'File does not start with the RData magic number: ' .
72             unpack('H*', substr($data, 0, 5));
73             }
74              
75 21         33 my ($value, $state) = @{Statistics::R::IO::REXPFactory::unserialize(substr($data, 5))};
  21         149  
76 21 50       81 croak 'Could not parse RData file' unless $state;
77 21 50       119 croak 'Unread data remaining in the RData file' unless $state->eof;
78 21         147 Statistics::R::IO::REXPFactory::tagged_pairlist_to_rexp_hash $value;
79             }
80              
81              
82             sub evalRserve {
83 50     50 1 191033 my ($rexp, $server) = (shift, shift);
84              
85 50   50     637 my $rserve = Statistics::R::IO::Rserve->new($server // {});
86              
87 50         1464 $rserve->eval($rexp)
88             }
89              
90             1; # End of Statistics::R::IO
91              
92             __END__