| 1 | package Bio::Gonzales::Util::Cerial; | ||||||||||
| 2 | |||||||||||
| 3 | 20 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 20 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 20 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 20 | ▶ | use Bio::Gonzales::Util::File qw/open_on_demand/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 20 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 20 | ▶ | use YAML::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 20 | ▶ | use JSON::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 20 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 20 | ▶ | use Storable qw/nstore_fd fd_retrieve/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 20 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 20 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 20 | our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); | |||||||||
| 18 | 20 | our $VERSION = '0.062'; # VERSION | |||||||||
| 19 | |||||||||||
| 20 | 20 | @EXPORT = qw( | |||||||||
| 21 | ythaw yfreeze yslurp yspew | ||||||||||
| 22 | jthaw jfreeze jslurp jspew | ||||||||||
| 23 | stoslurp stospew | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | BEGIN { | ||||||||||
| 27 | 20 | ▶ | *yfreeze = \&YAML::XS::Dump; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 20 | *ythaw = \&YAML::XS::Load; | |||||||||
| 29 | #*jfreeze = \&JSON::XS::encode_json; | ||||||||||
| 30 | 20 | *jthaw = \&JSON::XS::decode_json; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 20 | our $JSON = JSON::XS->new->indent(1)->utf8->allow_nonref; | |||||||||
| 34 | |||||||||||
| 35 | sub jfreeze { | ||||||||||
| 36 | 2 | ▶ | my $r; | ||||||||
|
Subroutine
jfreeze: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||
| 37 | 2 | my @d = @_; | |||||||||
| 38 | try { | ||||||||||
| 39 | 2 | ▶ | $r = $JSON->encode(@d); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 40 | } | ||||||||||
| 41 | catch { | ||||||||||
| 42 | 0 | ▶ | confess Dumper \@d; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 43 | 2 | }; | |||||||||
| 44 | |||||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | sub _spew { | ||||||||||
| 48 | 4 | ▶ | my $dest = shift; | ||||||||
|
Subroutine
_spew: called CC=3 SCAR=11.0
| |||||||||||
| 49 | 4 | my $data = shift; | |||||||||
| 50 | |||||||||||
| 51 | 4 | my ( $fh, $was_open ) = open_on_demand( $dest, '>' ); | |||||||||
| 52 | 4 | ▶ | binmode $fh, ':utf8' unless ( ref $fh eq 'IO::Zlib' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 4 | local $/ = "\n"; | |||||||||
| 54 | |||||||||||
| 55 | 4 | print $fh $data; | |||||||||
| 56 | 4 | ▶ | $fh->close unless $was_open; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | sub _slurp { | ||||||||||
| 60 | 5 | ▶ | my $src = shift; | ||||||||
|
Subroutine
_slurp: called CC=3 SCAR=11.1
| |||||||||||
| 61 | 5 | my ( $fh, $was_open ) = open_on_demand( $src, '<' ); | |||||||||
| 62 | 5 | ▶ | binmode $fh, ':utf8' unless ( ref $fh eq 'IO::Zlib' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 5 | local $/ = "\n"; | |||||||||
| 64 | |||||||||||
| 65 | 5 | my $data = do { local $/; <$fh> }; | |||||||||
| 66 | |||||||||||
| 67 | 5 | ▶ | $fh->close unless $was_open; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 5 | return $data; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 4 | ▶ | sub yslurp { return ythaw( _slurp(shift) ) } | ||||||||
|
Subroutine
yslurp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 72 | 1 | ▶ | sub jslurp { return jthaw( _slurp(shift) ) } | ||||||||
|
Subroutine
jslurp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 2 | ▶ | sub yspew { my $file = shift; _spew( $file, yfreeze( $_[0] ) ) } | ||||||||
|
Subroutine
yspew: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 74 | 2 | ▶ | sub jspew { my $file = shift; _spew( $file, jfreeze( $_[0] ) ) } | ||||||||
|
Subroutine
jspew: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 75 | |||||||||||
| 76 | sub stospew { | ||||||||||
| 77 | 0 | ▶ | my $dest = shift; | ||||||||
|
Subroutine
stospew: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 78 | 0 | my $data = shift; | |||||||||
| 79 | |||||||||||
| 80 | 0 | my ( $fh, $was_open ) = open_on_demand( $dest, '>' ); | |||||||||
| 81 | 0 | nstore_fd( $data, $fh ); | |||||||||
| 82 | 0 | ▶ | $fh->close unless ($was_open); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | sub stoslurp { | ||||||||||
| 86 | 0 | ▶ | my $src = shift; | ||||||||
|
Subroutine
stoslurp: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 87 | 0 | my ( $fh, $was_open ) = open_on_demand( $src, '<' ); | |||||||||
| 88 | 0 | my $data = fd_retrieve($fh); | |||||||||
| 89 | 0 | ▶ | $fh->close unless $was_open; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | return $data; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | __END__ | ||||||||||
| 94 | |||||||||||
| 95 | =head1 NAME | ||||||||||
| 96 | |||||||||||
| 97 | Bio::Gonzales::Util::Cerial - convenience functions for yaml and json IO | ||||||||||
| 98 | |||||||||||
| 99 | =head1 SYNOPSIS | ||||||||||
| 100 | |||||||||||
| 101 | use Bio::Gonzales::Util::Cerial; | ||||||||||
| 102 | |||||||||||
| 103 | # YAML IO | ||||||||||
| 104 | my $yaml_string = yfreeze(\%data); | ||||||||||
| 105 | my $data = ythaw($yaml_string); | ||||||||||
| 106 | |||||||||||
| 107 | yspew($filename, \%data); | ||||||||||
| 108 | my $data = yslurp($filename); | ||||||||||
| 109 | |||||||||||
| 110 | # JSON IO | ||||||||||
| 111 | my $json_string = jfreeze(\%data); | ||||||||||
| 112 | my $data = jthaw($json_string); | ||||||||||
| 113 | |||||||||||
| 114 | jspew($filename, \%data); | ||||||||||
| 115 | my $data = jslurp($filename); | ||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | =head1 DESCRIPTION | ||||||||||
| 119 | |||||||||||
| 120 | =item B<< $yaml_string = yfreeze(\%data) >> | ||||||||||
| 121 | |||||||||||
| 122 | Serialize data structure as yaml string | ||||||||||
| 123 | |||||||||||
| 124 | =item B<< $data = ythaw($yaml_string) >> | ||||||||||
| 125 | |||||||||||
| 126 | UNserialize data structure from yaml string | ||||||||||
| 127 | |||||||||||
| 128 | =item B<< yspew($filename, \%data) >> | ||||||||||
| 129 | |||||||||||
| 130 | Serialize data structure as yaml string to a file | ||||||||||
| 131 | |||||||||||
| 132 | =item B<< my $data = yslurp($filename) >> | ||||||||||
| 133 | |||||||||||
| 134 | UNserialize data structure from yaml file | ||||||||||
| 135 | |||||||||||
| 136 | =item B<< my $json_string = jfreeze(\%data) >> | ||||||||||
| 137 | |||||||||||
| 138 | Serialize data structure as json string | ||||||||||
| 139 | |||||||||||
| 140 | =item B<< my $data = jthaw($json_string) >> | ||||||||||
| 141 | |||||||||||
| 142 | UNserialize data structure from json string | ||||||||||
| 143 | |||||||||||
| 144 | =item B<< jspew($filename, \%data) >> | ||||||||||
| 145 | |||||||||||
| 146 | Serialize data structure as json string to a file | ||||||||||
| 147 | |||||||||||
| 148 | =item B<< my $data = jslurp($filename) >> | ||||||||||
| 149 | |||||||||||
| 150 | UNserialize data structure from json file | ||||||||||
| 151 | |||||||||||
| 152 | =head1 EXPORT | ||||||||||
| 153 | |||||||||||
| 154 | The following functions are exported by default | ||||||||||
| 155 | |||||||||||
| 156 | ythaw | ||||||||||
| 157 | yfreeze | ||||||||||
| 158 | yslurp | ||||||||||
| 159 | yspew | ||||||||||
| 160 | |||||||||||
| 161 | jthaw | ||||||||||
| 162 | jfreeze | ||||||||||
| 163 | jslurp | ||||||||||
| 164 | jspew | ||||||||||
| 165 | |||||||||||
| 166 | =cut | ||||||||||