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.0002';
4 4     4   2382 use 5.010;
  4         12  
5 4     4   22 use strict;
  4         7  
  4         97  
6 4     4   22 use warnings FATAL => 'all';
  4         7  
  4         148  
7              
8 4     4   21 use Exporter 'import';
  4         5  
  4         271  
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   1277 use Statistics::R::IO::REXPFactory;
  4         14  
  4         225  
16 4     4   1659 use Statistics::R::IO::Rserve;
  4         11  
  4         126  
17 4     4   1653 use IO::Uncompress::Gunzip ();
  4         113952  
  4         102  
18 4     4   1265 use IO::Uncompress::Bunzip2 ();
  4         11948  
  4         86  
19 4     4   28 use IO::Socket::INET ();
  4         7  
  4         57  
20 4     4   16 use Carp;
  4         8  
  4         1733  
21              
22              
23             sub readRDS {
24 265 50   265 1 230021 open (my $f, shift) or croak $!;
25 265         1086 binmode $f;
26 265         747 my ($data, $rc) = '';
27 265         3903 while ($rc = read($f, $data, 8192, length $data)) {}
28 265 50       711 croak $! unless defined $rc;
29 265 100       1219 if (substr($data, 0, 2) eq "\x1f\x8b") {
    100          
    100          
30             ## gzip-compressed file
31 53         188 seek($f, 0, 0);
32 53         248 IO::Uncompress::Gunzip::gunzip $f, \$data;
33             }
34             elsif (substr($data, 0, 3) eq 'BZh') {
35             ## bzip2-compressed file
36 53         186 seek($f, 0, 0);
37 53         263 IO::Uncompress::Bunzip2::bunzip2 $f, \$data;
38             }
39             elsif (substr($data, 0, 6) eq "\xfd7zXZ\0") {
40 53         6724 croak "xz-compressed RDS files are not supported";
41             }
42            
43 212         153993 my ($value, $state) = @{Statistics::R::IO::REXPFactory::unserialize($data)};
  212         691  
44 212 50       531 croak 'Could not parse RDS file' unless $state;
45 212 50       511 croak 'Unread data remaining in the RDS file' unless $state->eof;
46 212         1456 $value
47             }
48              
49              
50             sub readRData {
51 28 50   28 1 101236 open (my $f, shift) or croak $!;
52 28         168 binmode $f;
53 28         100 my ($data, $rc) = '';
54 28         814 while ($rc = read($f, $data, 8192, length $data)) {}
55 28 50       113 croak $! unless defined $rc;
56 28 100       212 if (substr($data, 0, 2) eq "\x1f\x8b") {
    100          
    100          
57             ## gzip-compressed file
58 7         30 seek($f, 0, 0);
59 7         36 IO::Uncompress::Gunzip::gunzip $f, \$data;
60             }
61             elsif (substr($data, 0, 3) eq 'BZh') {
62             ## bzip2-compressed file
63 7         27 seek($f, 0, 0);
64 7         38 IO::Uncompress::Bunzip2::bunzip2 $f, \$data;
65             }
66             elsif (substr($data, 0, 6) eq "\xfd7zXZ\0") {
67 7         1182 croak "xz-compressed RData files are not supported";
68             }
69            
70 21 50       26515 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         51 my ($value, $state) = @{Statistics::R::IO::REXPFactory::unserialize(substr($data, 5))};
  21         127  
76 21 50       88 croak 'Could not parse RData file' unless $state;
77 21 50       78 croak 'Unread data remaining in the RData file' unless $state->eof;
78 21         175 Statistics::R::IO::REXPFactory::tagged_pairlist_to_rexp_hash $value;
79             }
80              
81              
82             sub evalRserve {
83 50     50 1 341759 my ($rexp, $server) = (shift, shift);
84              
85 50   50     600 my $rserve = Statistics::R::IO::Rserve->new($server // {});
86              
87 50         1783 $rserve->eval($rexp)
88             }
89              
90             1; # End of Statistics::R::IO
91              
92             __END__