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