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.0001';
4 4     4   2398 use 5.010;
  4         10  
5 4     4   11 use strict;
  4         5  
  4         81  
6 4     4   11 use warnings FATAL => 'all';
  4         4  
  4         96  
7              
8 4     4   10 use Exporter 'import';
  4         2  
  4         237  
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   1568 use Statistics::R::IO::REXPFactory;
  4         7  
  4         149  
16 4     4   1518 use Statistics::R::IO::Rserve;
  4         7  
  4         99  
17 4     4   1961 use IO::Uncompress::Gunzip ();
  4         108778  
  4         87  
18 4     4   1889 use IO::Uncompress::Bunzip2 ();
  4         11919  
  4         68  
19 4     4   18 use IO::Socket::INET ();
  4         4  
  4         48  
20 4     4   14 use Carp;
  4         4  
  4         1644  
21              
22              
23             sub readRDS {
24 265 50   265 1 148406 open (my $f, shift) or croak $!;
25 265         634 binmode $f;
26 265         777 my ($data, $rc) = '';
27 265         3102 while ($rc = read($f, $data, 8192, length $data)) {}
28 265 50       507 croak $! unless defined $rc;
29 265 100       997 if (substr($data, 0, 2) eq "\x1f\x8b") {
    100          
    100          
30             ## gzip-compressed file
31 53         120 seek($f, 0, 0);
32 53         251 IO::Uncompress::Gunzip::gunzip $f, \$data;
33             }
34             elsif (substr($data, 0, 3) eq 'BZh') {
35             ## bzip2-compressed file
36 53         123 seek($f, 0, 0);
37 53         261 IO::Uncompress::Bunzip2::bunzip2 $f, \$data;
38             }
39             elsif (substr($data, 0, 6) eq "\xfd7zXZ\0") {
40 53         6808 croak "xz-compressed RDS files are not supported";
41             }
42            
43 212         105328 my ($value, $state) = @{Statistics::R::IO::REXPFactory::unserialize($data)};
  212         569  
44 212 50       415 croak 'Could not parse RDS file' unless $state;
45 212 50       513 croak 'Unread data remaining in the RDS file' unless $state->eof;
46 212         1123 $value
47             }
48              
49              
50             sub readRData {
51 28 50   28 1 36986 open (my $f, shift) or croak $!;
52 28         74 binmode $f;
53 28         50 my ($data, $rc) = '';
54 28         472 while ($rc = read($f, $data, 8192, length $data)) {}
55 28 50       62 croak $! unless defined $rc;
56 28 100       146 if (substr($data, 0, 2) eq "\x1f\x8b") {
    100          
    100          
57             ## gzip-compressed file
58 7         20 seek($f, 0, 0);
59 7         41 IO::Uncompress::Gunzip::gunzip $f, \$data;
60             }
61             elsif (substr($data, 0, 3) eq 'BZh') {
62             ## bzip2-compressed file
63 7         19 seek($f, 0, 0);
64 7         32 IO::Uncompress::Bunzip2::bunzip2 $f, \$data;
65             }
66             elsif (substr($data, 0, 6) eq "\xfd7zXZ\0") {
67 7         1077 croak "xz-compressed RData files are not supported";
68             }
69            
70 21 50       15994 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         25 my ($value, $state) = @{Statistics::R::IO::REXPFactory::unserialize(substr($data, 5))};
  21         89  
76 21 50       67 croak 'Could not parse RData file' unless $state;
77 21 50       52 croak 'Unread data remaining in the RData file' unless $state->eof;
78 21         122 Statistics::R::IO::REXPFactory::tagged_pairlist_to_rexp_hash $value;
79             }
80              
81              
82             sub evalRserve {
83 50     50 1 192184 my ($rexp, $server) = (shift, shift);
84              
85 50   50     649 my $rserve = Statistics::R::IO::Rserve->new($server // {});
86              
87 50         1570 $rserve->eval($rexp)
88             }
89              
90             1; # End of Statistics::R::IO
91              
92             __END__