| 1 | package PDL::IO::Sereal; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | require Exporter; | |||||||||||||||||||||||||||
| 7 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 8 | 2 | our @EXPORT_OK = qw(rsereal wsereal); | |||||||||||||||||||||||||||
| 9 | 2 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use constant DEBUG => $ENV{PDL_IO_SEREAL_DEBUG} ? 1 : 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use PDL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use PDL::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use PDL::IO::Misc qw(bswap2 bswap4 bswap8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Sereal::Encoder qw(encode_sereal); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Sereal::Decoder qw(decode_sereal); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | $Carp::Internal{ (__PACKAGE__) }++; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 26 | 2 | my $package = shift; | |||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||
| 28 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | *{'PDL::wsereal'} = \&wsereal if grep { /^(:all|wsereal)$/ } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 2 | *{'PDL::FREEZE'} = \&_FREEZE; | |||||||||||||||||||||||||||
| 31 | 2 | *{'PDL::THAW'} = \&_THAW; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 2 | ▶ | __PACKAGE__->export_to_level(1, $package, @_) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub wsereal { | ||||||||||||||||||||||||||
|
Subroutine
wsereal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 1 | my ($pdl, $filename) = @_; | |||||||||||||||||||||||||||
| 38 | 1 | my $sereal = encode_sereal($pdl, {freeze_callbacks=>1, compress=>Sereal::Encoder::SRL_ZLIB}); | |||||||||||||||||||||||||||
| 39 | 1 | _write_file($filename, $sereal); | |||||||||||||||||||||||||||
| 40 | 1 | return $pdl; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | sub rsereal { | ||||||||||||||||||||||||||
|
Subroutine
rsereal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 1 | my $filename = shift; | |||||||||||||||||||||||||||
| 45 | 1 | my $sereal = _read_file($filename); | |||||||||||||||||||||||||||
| 46 | 1 | my $pdl = decode_sereal($sereal); | |||||||||||||||||||||||||||
| 47 | 1 | return $pdl; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub _FREEZE { | ||||||||||||||||||||||||||
|
Subroutine
_FREEZE: called CC=7 SCAR=24.6
| |||||||||||||||||||||||||||||
| 51 | 2 | my ($self, $serializer) = @_; | |||||||||||||||||||||||||||
| 52 | 2 | my $ref = $self->get_dataref; | |||||||||||||||||||||||||||
| 53 | 2 | my $out = { | |||||||||||||||||||||||||||
| 54 | version => 1, | ||||||||||||||||||||||||||||
| 55 | dims => [$self->dims], | ||||||||||||||||||||||||||||
| 56 | type_name => $self->type->ioname, | ||||||||||||||||||||||||||||
| 57 | type_size => PDL::Core::howbig($self->type), | ||||||||||||||||||||||||||||
| 58 | packed_data => $$ref, | ||||||||||||||||||||||||||||
| 59 | native_one => pack('L', 1), | ||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if (ref $self->hdr eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | $out->{hash_hdr} = $self->hdr; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 2 | ▶ | if ($self->isa("HASH")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | for (keys %$self) { | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | next if $_ eq 'PDL'; # "PDL" is reserved | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $out->{hash_main}{$_} = $self->{$_}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 2 | ▶ | if ($self->badflag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | $out->{bad_flag} = $self->badflag; | |||||||||||||||||||||||||||
| 72 | 0 | $out->{bad_value} = $self->badvalue; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 2 | ▶ | $out->{hdrcpy_flag} = 1 if $self->hdrcpy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | return $out; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | sub _THAW { | ||||||||||||||||||||||||||
|
Subroutine
_THAW: called CC=32 SCAR=50.8
| |||||||||||||||||||||||||||||
| 79 | 2 | my ($class, $serializer, $data) = @_; | |||||||||||||||||||||||||||
| 80 | 2 | ▶ | croak "THAW: bad input data" unless ref $data eq 'HASH' && looks_like_number($data->{version}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $data eq "HASH" and &looks_like_number($data->{"version"})
MC/DC0%0/2
ref $data eq "HASH" and &looks_like_number($data->{"version"})
ref $data eq "HASH"
&looks_like_number($data->{"version"})
Truth table33%1/3
A and B
A= ref $data eq "HASH"B= &looks_like_number($data->{"version"})
| |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | if ($data->{version} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | croak "THAW: invalid type_name" unless defined $data->{type_name} && !ref $data->{type_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $data->{"type_name"} and not ref $data->{"type_name"}
MC/DC0%0/2
defined $data->{"type_name"} and not ref $data->{"type_name"}
defined $data->{"type_name"}
not ref $data->{"type_name"}
Truth table33%1/3
A and not(B)
A= defined $data->{"type_name"}B= not ref $data->{"type_name"}
| |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | croak "THAW: invalid type_size" unless defined $data->{type_size} && looks_like_number($data->{type_size}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $data->{"type_size"} and &looks_like_number($data->{"type_size"})
MC/DC0%0/2
defined $data->{"type_size"} and &looks_like_number($data->{"type_size"})
defined $data->{"type_size"}
&looks_like_number($data->{"type_size"})
Truth table33%1/3
A and B
A= defined $data->{"type_size"}B= &looks_like_number($data->{"type_size"})
| |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | croak "THAW: invalid dims" unless ref $data->{dims} eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | croak "THAW: invalid native_one" unless defined $data->{native_one} && !ref $data->{native_one}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $data->{"native_one"} and not ref $data->{"native_one"}
MC/DC0%0/2
defined $data->{"native_one"} and not ref $data->{"native_one"}
defined $data->{"native_one"}
not ref $data->{"native_one"}
Truth table33%1/3
A and not(B)
A= defined $data->{"native_one"}B= not ref $data->{"native_one"}
| |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | croak "THAW: invalid packed_data" unless defined $data->{packed_data} && !ref $data->{packed_data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $data->{"packed_data"} and not ref $data->{"packed_data"}
MC/DC0%0/2
defined $data->{"packed_data"} and not ref $data->{"packed_data"}
defined $data->{"packed_data"}
not ref $data->{"packed_data"}
Truth table33%1/3
A and not(B)
A= defined $data->{"packed_data"}B= not ref $data->{"packed_data"}
| |||||||||||||||||||||||||||||
| 87 | 2 | my $type = PDL::Type->new($data->{type_name}); | |||||||||||||||||||||||||||
| 88 | 2 | ▶ | croak "THAW: unsupported type" unless $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 2 | my $type_sz = PDL::Core::howbig($type); | |||||||||||||||||||||||||||
| 90 | 2 | ▶ | croak "THAW: type '$data->{type_name}' size mismatch ($type_sz != $data->{type_size})" unless $type_sz == $data->{type_size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | my $native_one = unpack('L', $data->{native_one}); | |||||||||||||||||||||||||||
| 92 | 2 | ▶ | croak "THAW: unknown endianness" unless $native_one == 0x01000000 || $native_one == 0x00000001; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$native_one == 16777216 or $native_one == 1
MC/DC0%0/2
$native_one == 16777216 or $native_one == 1
$native_one == 16777216
$native_one == 1
Truth table33%1/3
A or B
A= $native_one == 16777216B= $native_one == 1
| |||||||||||||||||||||||||||||
| 93 | 2 | ▶ | my $do_swap = $native_one == 0x01000000 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | my $pdl = PDL::new_from_specification($class, $type, @{$data->{dims}}); | |||||||||||||||||||||||||||
| 95 | 2 | my $dataref = $pdl->get_dataref; | |||||||||||||||||||||||||||
| 96 | 2 | ▶ | croak "THAW: data size mismatch" unless length $$dataref == length $data->{packed_data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 2 | $$dataref = $data->{packed_data}; | |||||||||||||||||||||||||||
| 98 | 2 | ▶ | if ($do_swap && $type_sz > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$do_swap and $type_sz > 1
MC/DC0%0/2
$do_swap and $type_sz > 1
$do_swap
$type_sz > 1
Truth table33%1/3
A and B
A= $do_swapB= $type_sz > 1
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | bswap2($pdl) if($type_sz==2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | bswap4($pdl) if($type_sz==4); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | bswap8($pdl) if($type_sz==8); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 2 | $pdl->upd_data; | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | if (ref $data->{hash_hdr} eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 2 | $pdl->sethdr($data->{hash_hdr}); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 2 | ▶ | if ($pdl->isa("HASH") && ref $data->{hash_main} eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$pdl->isa("HASH") and ref $data->{"hash_main"} eq "HASH"
MC/DC0%0/2
$pdl->isa("HASH") and ref $data->{"hash_main"} eq "HASH"
$pdl->isa("HASH")
ref $data->{"hash_main"} eq "HASH"
Truth table33%1/3
A and B
A= $pdl->isa("HASH")B= ref $data->{"hash_main"} eq "HASH"
| |||||||||||||||||||||||||||||
| 108 | 0 | for (keys %{$data->{hash_main}}) { | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | next if $_ eq 'PDL'; # "PDL" is reserved | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | $pdl->{$_} = $data->{hash_main}{$_}; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 2 | ▶ | if ($data->{bad_flag}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $pdl->badflag($data->{bad_flag}); | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | $pdl->badvalue($data->{bad_value}) if defined $data->{bad_value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 2 | ▶ | $pdl->hdrcpy(1) if $data->{hdrcpy_flag}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 2 | return $pdl; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||
| 121 | 0 | croak "THAW: invalid version"; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | ▶ | sub _read_file { | ||||||||||||||||||||||||||
|
Subroutine
_read_file: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 126 | 1 | my ($filename) = @_; | |||||||||||||||||||||||||||
| 127 | 1 | ▶ | open my $fh, '<', $filename or croak "rsereal: cannot open '$filename': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1 | my $rv; | |||||||||||||||||||||||||||
| 129 | 1 | my $data = ''; | |||||||||||||||||||||||||||
| 130 | 1 | ▶ | while ($rv = sysread($fh, my $buffer, 102400, 0)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 55 | $data .= $buffer | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 1 | ▶ | croak "rsereal: cannot read file '$filename': $!" if !defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 1 | return $data; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | sub _write_file { | ||||||||||||||||||||||||||
|
Subroutine
_write_file: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 138 | 1 | my ($filename, $data) = @_; | |||||||||||||||||||||||||||
| 139 | 1 | ▶ | open my $fh, '>', $filename or croak "wsereal: cannot open '$filename': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 1 | my $rv = syswrite($fh, $data); | |||||||||||||||||||||||||||
| 141 | 1 | ▶ | croak "wsereal: cannot write '$filename': $!" if !defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 2 | 1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||