Serialize.pm

filter: Statementstmt 93.2% 96 / 103 Branchbran 75.9% 41 / 54 Conditioncond 69.7% 30 / 43 MC/DCmcdc 45.8% 11 / 24 Subroutinesub 95.2% 20 / 21 Podpod n/a totaltotal 80.8% 198 / 245 SCAR 40.4
CC
47
Cov
84%
__ANON__
19.6
__ANON__
15.1
__ANON__
14.8
CRAP 56.9
↑ Summary Serialize/Serializer.pm »
1 package File::Serialize;
2 10 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: DWIM file serialization/deserialization
4 10 $File::Serialize::VERSION = '1.5.0';
5 10 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use feature 'current_sub';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use Class::Load qw/ load_class /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use List::AllUtils qw/ uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use List::Util 1.41 qw/ pairgrep first none any pairmap /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 use Module::Pluggable
18 10 require => 1,
19 sub_name => '_all_serializers',
20 search_path => __PACKAGE__ . '::Serializer'
21 10 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 10 our @EXPORT = qw/ serialize_file deserialize_file transerialize_file /;
26  
27 10 our $implicit_transform;
28  
29 sub _generate_serialize_file {
30 39 ▶ my( undef, undef, undef, $global )= @_;
Subroutine _generate_serialize_file: called CC=1 SCAR=0.1
31  
32 return sub {
33 30 ▶ my( $file, $content, $options ) = @_;
Subroutine __ANON__: called CC=17 SCAR=29.7
34  
35 30 ▶ $options = { format => $options } if $options and not ref $options;
Branch: 1 branch
BranchTrueFalseTotal
if $options and not ref $options 0 30 30
Condition66%2/3
$options and not ref $options
!ll&&!rl&&r
14160
MC/DC0%0/2
$options and not ref $options
$options not ref $options
Truth table66%2/3
A and not(B)
A= $optionsB= not ref $options
ABresult
0X0
100
111
36  
37 30 ▶ $options = { %$global, %{ $options||{} } } if $global;
Branch: 2 branches
BranchTrueFalseTotal
if $global 30 0 30
$options or {} 14 16 30
38 # default to utf8 => 1
39 30 ▶ $options->{utf8} //= 1;
Condition100%2/2
$options->{"utf8"} //= 1
l!l
129
MC/DC100%1/1
$options->{"utf8"} //= 1
$options->{"utf8"}
Truth table100%2/2
A
A= $options->{"utf8"}
Aresult
11
00
40 30 ▶ $options->{allow_nonref} //= 1;
Condition50%1/2
$options->{"allow_nonref"} //= 1
l!l
030
MC/DC0%0/1
$options->{"allow_nonref"} //= 1
$options->{"allow_nonref"}
Truth table50%1/2
A
A= $options->{"allow_nonref"}
Aresult
11
00
41 30 ▶ $options->{pretty} //= 1;
Condition100%2/2
$options->{"pretty"} //= 1
l!l
723
MC/DC100%1/1
$options->{"pretty"} //= 1
$options->{"pretty"}
Truth table100%2/2
A
A= $options->{"pretty"}
Aresult
11
00
42 30 ▶ $options->{canonical} //= 1;
Condition100%2/2
$options->{"canonical"} //= 1
l!l
1515
MC/DC100%1/1
$options->{"canonical"} //= 1
$options->{"canonical"}
Truth table100%2/2
A
A= $options->{"canonical"}
Aresult
11
00
43  
44 30 ▶ $file = path($file) unless $file =~ /^-/ or ref $file eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /^-/u or ref $file eq "SCALAR" 26 4 30
Condition66%2/3
$file =~ /^-/u or ref $file eq "SCALAR"
l!l&&r!l&&!r
0426
MC/DC50%1/2
$file =~ /^-/u or ref $file eq "SCALAR"
$file =~ /^-/u ref $file eq "SCALAR"
Truth table66%2/3
A or B
A= $file =~ /^-/uB= ref $file eq "SCALAR"
ABresult
1X1
011
000
45  
46 30 my $serializer = _serializer($file, $options);
47  
48 $file = path( join '.', $file, $serializer->extension )
49 29 ▶ if $options->{add_extension} and $file ne '-'
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_extension"} and $file ne "-" and ref $file ne "SCALAR" 0 29 29
Condition33%2/6
$options->{"add_extension"} and $file ne "-" and ref $file ne "SCALAR"
!ll&&!rl&&r
2900
$options->{"add_extension"} and $file ne "-"
!ll&&!rl&&r
2900
MC/DC0%0/3
$options->{"add_extension"} and $file ne "-" and ref $file ne "SCALAR"
$options->{"add_extension"} $file ne "-" ref $file ne "SCALAR"
Truth table25%1/4
A and B and C
A= $options->{"add_extension"}B= $file ne "-"C= ref $file ne "SCALAR"
ABCresult
0XX0
10X0
1100
1111
50 and ref $file ne 'SCALAR';
51  
52 29 ▶ my $method = $options->{utf8} ? 'spew_utf8' : 'spew';
Branch: 1 branch
BranchTrueFalseTotal
$options->{'utf8'} ? : 28 1 29
53  
54 29 my $serialized = $serializer->serialize($content,$options);
55  
56 29 ▶ return print $serialized if $file eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "-" 0 29 29
57  
58 29 ▶ if( ref $file eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'SCALAR') { } 4 25 29
59 4 $$file = $serialized;
60 }
61 else {
62 25 $file->$method($serialized);
63 }
64 }
65 39 }
66  
67 sub _generate_deserialize_file {
68 39 ▶ my( undef, undef, undef, $global ) = @_;
Subroutine _generate_deserialize_file: called CC=1 SCAR=0.2
69  
70 return sub {
71 36 ▶ my( $file, $options ) = @_;
Subroutine __ANON__: called CC=12 SCAR=27.1
72  
73 36 ▶ $file = path($file) unless $file eq '-' or ref $file eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless $file eq "-" or ref $file eq "SCALAR" 27 9 36
Condition66%2/3
$file eq "-" or ref $file eq "SCALAR"
l!l&&r!l&&!r
0927
MC/DC50%1/2
$file eq "-" or ref $file eq "SCALAR"
$file eq "-" ref $file eq "SCALAR"
Truth table66%2/3
A or B
A= $file eq "-"B= ref $file eq "SCALAR"
ABresult
1X1
011
000
74  
75 36 ▶ $options = { %$global, %{ $options||{} } } if $global;
Branch: 2 branches
BranchTrueFalseTotal
if $global 36 0 36
$options or {} 22 14 36
76 36 ▶ $options->{utf8} //= 1;
Condition50%1/2
$options->{"utf8"} //= 1
l!l
036
MC/DC0%0/1
$options->{"utf8"} //= 1
$options->{"utf8"}
Truth table50%1/2
A
A= $options->{"utf8"}
Aresult
11
00
77 36 ▶ $options->{allow_nonref} //= 1;
Condition100%2/2
$options->{"allow_nonref"} //= 1
l!l
135
MC/DC100%1/1
$options->{"allow_nonref"} //= 1
$options->{"allow_nonref"}
Truth table100%2/2
A
A= $options->{"allow_nonref"}
Aresult
11
00
78  
79 36 my $method = 'slurp' . ( '_utf8' ) x !! $options->{utf8};
80  
81 36 my $serializer = _serializer($file, $options);
82  
83 $file = path( join '.', $file, $serializer->extension )
84 36 ▶ if $options->{add_extension} and $file ne '-' and ref $file ne 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_extension"} and $file ne "-" and ref $file ne "SCALAR" 0 36 36
Condition33%2/6
$options->{"add_extension"} and $file ne "-" and ref $file ne "SCALAR"
!ll&&!rl&&r
3600
$options->{"add_extension"} and $file ne "-"
!ll&&!rl&&r
3600
MC/DC0%0/3
$options->{"add_extension"} and $file ne "-" and ref $file ne "SCALAR"
$options->{"add_extension"} $file ne "-" ref $file ne "SCALAR"
Truth table25%1/4
A and B and C
A= $options->{"add_extension"}B= $file ne "-"C= ref $file ne "SCALAR"
ABCresult
0XX0
10X0
1100
1111
85  
86 return $serializer->deserialize(
87 36 ▶ $file =~ /^-/ ? do { local $/ = <STDIN> }
Branch: 2 branches
BranchTrueFalseTotal
$file =~ /^-/u ? : 0 36 36
ref $file eq 'SCALAR' ? : 9 27 36
88 : ref $file eq 'SCALAR' ? $$file
89 : $file->$method,
90 $options
91 );
92 }
93 39 }
94  
95 sub _generate_transerialize_file {
96  
97 16 ▶ my $serialize_file = _generate_serialize_file(@_);
Subroutine _generate_transerialize_file: called CC=1 SCAR=0.0
98 16 my $deserialize_file = _generate_deserialize_file(@_);
99  
100  
101 return sub {
102 12 ▶ my( $in, @chain ) = @_;
Subroutine __ANON__: called CC=14 SCAR=27.0
103 12 ▶ my $data = ref($in) ? $in : $deserialize_file->($in);
Branch: 1 branch
BranchTrueFalseTotal
ref $in ? : 11 1 12
104  
105 12 ▶ while( my $step = shift @chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step = shift @chain) 22 12 34
106 22 ▶ if ( ref $step eq 'CODE' ) {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $step eq 'CODE') { } 8 14 22
elsif (ref $step eq 'ARRAY') { } 1 13 14
elsif (not ref $step or ref($step) =~ /Path::Tiny/u) { } 8 5 13
elsif (ref $step eq 'HASH') { } 4 1 5
elsif (ref $step eq 'SCALAR') { } 1 0 1
107 8 local $_ = $data;
108 8 ▶ if( $implicit_transform ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Serialize::implicit_transform) { } 0 8 8
109 0 $step->($data);
110 0 $data = $_;
111 }
112 else {
113 8 $data = $step->($data);
114 }}
115 elsif ( ref $step eq 'ARRAY' ) {
116 1 ▶ die "subranch step can only be the last step of the chain"
Branch: 1 branch
BranchTrueFalseTotal
if @chain 0 1 1
117 if @chain;
118 1 for my $branch( @$step ) {
119 2 __SUB__->($data,@$branch);
120 }
121 }
122 ▶ elsif ( not ref $step or ref($step) =~ /Path::Tiny/ ) {
Condition100%3/3
not ref $step or ref($step) =~ /Path::Tiny/u
l!l&&r!l&&!r
625
MC/DC100%2/2
not ref $step or ref($step) =~ /Path::Tiny/u
not ref $step ref($step) =~ /Path::Tiny/u
Truth table100%3/3
not(A) or B
A= not ref $stepB= ref($step) =~ /Path::Tiny/u
ABresult
1X1
011
000
123 8 ▶ die "filename '$step' not at the end of the chain"
Branch: 1 branch
BranchTrueFalseTotal
unless @chain <= 1 0 8 8
124 unless @chain <= 1;
125  
126 8 $serialize_file->( $step, $data, shift @chain );
127 }
128 elsif ( ref $step eq 'HASH' ) {
129 4 ▶ while( my ($f,$o) = each %$step ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$step) 6 4 10
130 6 $serialize_file->($f,$data,$o);
131 }
132 }
133 elsif ( ref $step eq 'SCALAR' ) {
134 1 $$step = $data;
135 }
136 else {
137 0 die "wrong chain argument";
138 }
139 }
140  
141 }
142 16 }
143  
144 sub _all_operative_formats {
145 0 ▶ my $self = shift;
Subroutine _all_operative_formats: not called CC=1 SCAR=6.9
146 0 return uniq map { $_->extension } $self->_all_operative_formats;
147 }
148  
149 sub _all_operative_serializers {
150 sort {
151 448 ▶ $b->precedence <=> $a->precedence
Branch: 1 branch
BranchTrueFalseTotal
$b->precedence <=> $a->precedence or $a cmp $b 448 0 448
152 or
153 $a cmp $b
154 }
155 504 grep { $_->is_operative }
156 56 ▶ grep { $_->precedence }
Subroutine _all_operative_serializers: called CC=2 SCAR=6.9
157 __PACKAGE__->_all_serializers;
158 }
159  
160 sub _serializer {
161 66 ▶ my( $self, $options ) = @_;
Subroutine _serializer: called CC=6 SCAR=17.9
162  
163 10 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 66 ▶ my $serializers = $options->{serializers} || [ __PACKAGE__->_all_operative_serializers ];
Condition100%2/2
$options->{'serializers'} || [__PACKAGE__->_all_operative_serializers]
l!l
1056
MC/DC0%0/1
$options->{'serializers'} || [__PACKAGE__->_all_operative_serializers]
$options->{'serializers'}
Truth table50%1/2
A
A= $options->{'serializers'}
Aresult
11
00
166 66 s/^\+/File::Serialize::Serializer::/ for @$serializers;
167  
168 66 ▶ my $format = $options->{format} || ( ( ref $self ? $self->basename : $self ) =~ /\.(\w+)$/ )[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 53 0 53
Condition100%3/3
$options->{'format'} || ((ref $self ? $self->basename : $self) =~ /\.(\w+)$/u)[0]
l!l&&r!l&&!r
13521
MC/DC100%2/2
$options->{'format'} || ((ref $self ? $self->basename : $self) =~ /\.(\w+)$/u)[0]
$options->{'format'} ((ref $self ? $self->basename : $self) =~ /\.(\w+)$/u)[0]
Truth table100%3/3
A || B
A= $options->{'format'}B= ((ref $self ? $self->basename : $self) =~ /\.(\w+)$/u)[0]
ABresult
1X1
011
000
169  
170 66 ▶ return( first { $_->does_extension($format) } @$serializers
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%2/2
&first(sub { $_->does_extension($format); } , @$serializers) || die("no serializer found for $format")
l!l
651
MC/DC100%1/1
&first(sub { $_->does_extension($format); } , @$serializers) || die("no serializer found for $format")
&first(sub { $_->does_extension($format); } , @$serializers)
Truth table100%2/2
A
A= &first(sub { $_->does_extension($format); } , @$serializers)
Aresult
11
00
171 or die "no serializer found for $format"
172 );
173 }
174  
175 10 1;
176  
177 __END__
↑ Summary Serialize/Serializer.pm »