Serialize.pm

filter: Statementstmt 94.9% 94 / 99 Branchbran 76.9% 40 / 52 Conditioncond 69.7% 30 / 43 MC/DCmcdc 45.8% 11 / 24 Subroutinesub 95.2% 20 / 21 Podpod n/a totaltotal 81.5% 195 / 239 CC 46 SCAR 24.2
CC
46
Cov
85%
__ANON__
19.6
__ANON__
15.1
__ANON__
13.2
CRAP 11.3
↑ Summary Serialize/Serializer/Markdown.pm »
1 package File::Serialize;
2 10 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: DWIM file serialization/deserialization
4 10 $File::Serialize::VERSION = '1.4.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 sub _generate_serialize_file {
28 39 ▶ my( undef, undef, undef, $global )= @_;
Subroutine _generate_serialize_file: called CC=1 SCAR=0.1
29  
30 return sub {
31 30 ▶ my( $file, $content, $options ) = @_;
Subroutine __ANON__: called CC=17 SCAR=29.7
32  
33 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
34  
35 30 ▶ $options = { %$global, %{ $options||{} } } if $global;
Branch: 2 branches
BranchTrueFalseTotal
if $global 30 0 30
$options or {} 14 16 30
36 # default to utf8 => 1
37 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
38 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
39 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
40 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
41  
42 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
43  
44 30 my $serializer = _serializer($file, $options);
45  
46 $file = path( join '.', $file, $serializer->extension )
47 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
48 and ref $file ne 'SCALAR';
49  
50 29 ▶ my $method = $options->{utf8} ? 'spew_utf8' : 'spew';
Branch: 1 branch
BranchTrueFalseTotal
$options->{'utf8'} ? : 28 1 29
51  
52 29 my $serialized = $serializer->serialize($content,$options);
53  
54 29 ▶ return print $serialized if $file eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "-" 0 29 29
55  
56 29 ▶ if( ref $file eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'SCALAR') { } 4 25 29
57 4 $$file = $serialized;
58 }
59 else {
60 25 $file->$method($serialized);
61 }
62 }
63 39 }
64  
65 sub _generate_deserialize_file {
66 39 ▶ my( undef, undef, undef, $global ) = @_;
Subroutine _generate_deserialize_file: called CC=1 SCAR=0.2
67  
68 return sub {
69 36 ▶ my( $file, $options ) = @_;
Subroutine __ANON__: called CC=12 SCAR=27.1
70  
71 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
72  
73 36 ▶ $options = { %$global, %{ $options||{} } } if $global;
Branch: 2 branches
BranchTrueFalseTotal
if $global 36 0 36
$options or {} 22 14 36
74 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
75 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
76  
77 36 my $method = 'slurp' . ( '_utf8' ) x !! $options->{utf8};
78  
79 36 my $serializer = _serializer($file, $options);
80  
81 $file = path( join '.', $file, $serializer->extension )
82 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
83  
84 return $serializer->deserialize(
85 36 ▶ $file =~ /^-/ ? do { local $/ = <STDIN> }
Branch: 2 branches
BranchTrueFalseTotal
$file =~ /^-/u ? : 0 36 36
ref $file eq 'SCALAR' ? : 9 27 36
86 : ref $file eq 'SCALAR' ? $$file
87 : $file->$method,
88 $options
89 );
90 }
91 39 }
92  
93 sub _generate_transerialize_file {
94  
95 16 ▶ my $serialize_file = _generate_serialize_file(@_);
Subroutine _generate_transerialize_file: called CC=1 SCAR=0.0
96 16 my $deserialize_file = _generate_deserialize_file(@_);
97  
98  
99 return sub {
100 12 ▶ my( $in, @chain ) = @_;
Subroutine __ANON__: called CC=13 SCAR=25.8
101 12 ▶ my $data = ref($in) ? $in : $deserialize_file->($in);
Branch: 1 branch
BranchTrueFalseTotal
ref $in ? : 11 1 12
102  
103 12 ▶ while( my $step = shift @chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $step = shift @chain) 22 12 34
104 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
105 8 local $_ = $data;
106 8 $data = $step->($data);
107 }
108 elsif ( ref $step eq 'ARRAY' ) {
109 1 ▶ die "subranch step can only be the last step of the chain"
Branch: 1 branch
BranchTrueFalseTotal
if @chain 0 1 1
110 if @chain;
111 1 for my $branch( @$step ) {
112 2 __SUB__->($data,@$branch);
113 }
114 }
115 ▶ 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
116 8 ▶ die "filename '$step' not at the end of the chain"
Branch: 1 branch
BranchTrueFalseTotal
unless @chain <= 1 0 8 8
117 unless @chain <= 1;
118  
119 8 $serialize_file->( $step, $data, shift @chain );
120 }
121 elsif ( ref $step eq 'HASH' ) {
122 4 ▶ while( my ($f,$o) = each %$step ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$step) 6 4 10
123 6 $serialize_file->($f,$data,$o);
124 }
125 }
126 elsif ( ref $step eq 'SCALAR' ) {
127 1 $$step = $data;
128 }
129 else {
130 0 die "wrong chain argument";
131 }
132 }
133  
134 }
135 16 }
136  
137 sub _all_operative_formats {
138 0 ▶ my $self = shift;
Subroutine _all_operative_formats: not called CC=1 SCAR=6.9
139 0 return uniq map { $_->extension } $self->_all_operative_formats;
140 }
141  
142 sub _all_operative_serializers {
143 sort {
144 448 ▶ $b->precedence <=> $a->precedence
Branch: 1 branch
BranchTrueFalseTotal
$b->precedence <=> $a->precedence or $a cmp $b 448 0 448
145 or
146 $a cmp $b
147 }
148 504 grep { $_->is_operative }
149 56 ▶ grep { $_->precedence }
Subroutine _all_operative_serializers: called CC=2 SCAR=6.9
150 __PACKAGE__->_all_serializers;
151 }
152  
153 sub _serializer {
154 66 ▶ my( $self, $options ) = @_;
Subroutine _serializer: called CC=6 SCAR=17.9
155  
156 10 ▶ no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 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
159 66 s/^\+/File::Serialize::Serializer::/ for @$serializers;
160  
161 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
162  
163 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
164 or die "no serializer found for $format"
165 );
166 }
167  
168 10 1;
169  
170 __END__
↑ Summary Serialize/Serializer/Markdown.pm »