blib/lib/YAML/As/Parsed.pm

filter: Statementstmt 90.8% 328 / 362 (1 uncoverable) Branchbran 83.8% 181 / 216 Conditioncond 88.1% 37 / 42 MC/DCmcdc 78.5% 22 / 28 Subroutinesub 91.6% 33 / 36 Podpod 0.0% 0 / 11 totaltotal 86.6% 601 / 695 (1 uncoverable) CC 126 SCAR 24.9
CC
126
Cov
88%
_load_string
22.1
_load_hash
17.5
_load_array
17.2
CRAP 12.1
↑ Summary
1 11 ▶use 5.008001; # sane UTF-8 support
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package YAML::As::Parsed; # git description: v1.72-7-g8682f63
5  
6 11 our $VERSION = '1.00';
7  
8 11 ▶use Tie::OrderedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 424 ▶sub ordered_hash {
Subroutine ordered_hash: called CC=1 SCAR=0.0
Pod undocumented
11 424 my %hash = ();
12 424 tie(%hash, 'Tie::OrderedHash');
13 424 return \%hash;
14 }
15  
16 11 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 our @ISA = qw{ Exporter };
18 11 our @EXPORT = qw{ Load Dump };
19 11 our @EXPORT_OK = qw{ LoadFile DumpFile freeze thaw };
20  
21 159 ▶sub Dump {
Subroutine Dump: called CC=1 SCAR=0.0
Pod undocumented
22 159 return __PACKAGE__->new(@_)->_dump_string;
23 }
24  
25 112 ▶sub Load {
Subroutine Load: called CC=2 SCAR=6.9
Pod undocumented
26 112 my $self = __PACKAGE__->_load_string(@_);
27 112 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 53 59 112
28 53 return @$self;
29 } else {
30 # To match YAML.pm, return the last document
31 59 return $self->[-1];
32 }
33 }
34  
35 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
36 11 *freeze = \&Dump;
37 11 *thaw = \&Load;
38 }
39  
40 0 ▶sub DumpFile {
Subroutine DumpFile: not called CC=1 SCAR=6.9
Pod undocumented
41 0 my $file = shift;
42 0 return __PACKAGE__->new(@_)->_dump_file($file);
43 }
44  
45 0 ▶sub LoadFile {
Subroutine LoadFile: not called CC=2 SCAR=17.9
Pod undocumented
46 0 my $file = shift;
47 0 my $self = __PACKAGE__->_load_file($file);
48 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
49 0 return @$self;
50 } else {
51 # Return only the last document to match YAML.pm,
52 0 return $self->[-1];
53 }
54 }
55  
56 170 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
57 170 my $class = shift;
58 170 bless [ @_ ], $class;
59 }
60  
61  
62 221 ▶sub read_string {
Subroutine read_string: called CC=1 SCAR=0.0
Pod undocumented
63 221 my $self = shift;
64 221 $self->_load_string(@_);
65 }
66  
67 105 ▶sub write_string {
Subroutine write_string: called CC=1 SCAR=0.0
Pod undocumented
68 105 my $self = shift;
69 105 $self->_dump_string(@_);
70 }
71  
72 14 ▶sub read {
Subroutine read: called CC=1 SCAR=0.0
Pod undocumented
73 14 my $self = shift;
74 14 $self->_load_file(@_);
75 }
76  
77 4 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod undocumented
78 4 my $self = shift;
79 4 $self->_dump_file(@_);
80 }
81  
82 11 my @UNPRINTABLE = qw(
83 0 x01 x02 x03 x04 x05 x06 a
84 b t n v f r x0E x0F
85 x10 x11 x12 x13 x14 x15 x16 x17
86 x18 x19 x1A e x1C x1D x1E x1F
87 );
88  
89 11 my %UNESCAPES = (
90 0 => "\x00", z => "\x00", N => "\x85",
91 a => "\x07", b => "\x08", t => "\x09",
92 n => "\x0a", v => "\x0b", f => "\x0c",
93 r => "\x0d", e => "\x1b", '\\' => '\\',
94 );
95  
96  
97 11 my %QUOTE = map { $_ => 1 } qw{
98 null true false
99 };
100  
101 11 my $re_capture_double_quoted = qr/\"([^\\"]*(?:\\.[^\\"]*)*)\"/;
102 11 my $re_capture_single_quoted = qr/\'([^\']*(?:\'\'[^\']*)*)\'/;
103 11 my $re_capture_unquoted_key = qr/([^:]+(?::+\S(?:[^:]*|.*?(?=:)))*)(?=\s*\:(?:\s+|$))/;
104 11 my $re_trailing_comment = qr/(?:\s+\#.*)?/;
105 11 my $re_key_value_separator = qr/\s*:(?:\s+(?:\#.*)?|$)/;
106  
107 14 ▶sub _load_file {
Subroutine _load_file: called CC=11 SCAR=24.5
108 14 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 11 14
109  
110 # Check the file
111 14 ▶ my $file = shift or $class->_error( 'You did not specify a file name' );
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or $class->_error("You did not specify a file name") 13 1 14
112 13 ▶ $class->_error( "File '$file' does not exist" )
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 1 12 13
113 unless -e $file;
114 12 ▶ $class->_error( "'$file' is a directory, not a file" )
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 1 11 12
115 unless -f _;
116 11 ▶ $class->_error( "Insufficient permissions to read '$file'" )
Branch: 1 branch
BranchTrueFalseTotal
unless -r _ 0 11 11
117 unless -r _;
118  
119 # Open unbuffered with strict UTF-8 decoding and no translation layers
120 11 open( my $fh, "<:unix:encoding(UTF-8)", $file );
121 11 ▶ unless ( $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 11 11
122 0 $class->_error("Failed to open file '$file': $!");
123 }
124  
125 # flock if available (or warn if not possible for OS-specific reasons)
126 11 ▶ if ( _can_flock() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_can_flock()) 11 0 11
127 11 ▶ flock( $fh, Fcntl::LOCK_SH() )
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 1 or warn "Couldn't lock '${file}' for reading: $!" 11 0 11
128 or warn "Couldn't lock '$file' for reading: $!";
129 }
130  
131 # slurp the contents
132 11 my $contents = eval {
133 11 ▶ use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
134 11 local $/;
135 <$fh>
136 11 };
137 11 ▶ if ( my $err = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 9 11
138 2 $class->_error("Error reading from file '$file': $err");
139 }
140  
141 # close the file (release the lock)
142 9 ▶ unless ( close $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 0 9 9
143 0 $class->_error("Failed to close file '$file': $!");
144 }
145  
146 9 $class->_load_string( $contents );
147 }
148  
149 342 ▶sub _load_string {
Subroutine _load_string: called CC=22 SCAR=31.0
150 342 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 341 342
151 342 my $self = bless [], $class;
152 342 my $string = $_[0];
153 342 eval {
154 342 ▶ unless ( defined $string ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 1 341 342
155 1 die \"Did not provide a string to load";
156 }
157  
158 # Check if Perl has it marked as characters, but it's internally
159 # inconsistent. E.g. maybe latin1 got read on a :utf8 layer
160 341 ▶ if ( utf8::is_utf8($string) && ! utf8::valid($string) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($string) and not utf8::valid($string)) 1 340 341
Condition100%3/3
utf8::is_utf8($string) and not utf8::valid($string)
!ll&&!rl&&r
1571831
MC/DC100%2/2
utf8::is_utf8($string) and not utf8::valid($string)
utf8::is_utf8($string) not utf8::valid($string)
Truth table100%3/3
A and not(B)
A= utf8::is_utf8($string)B= not utf8::valid($string)
ABresult
0X0
100
111
161 1 die \<<'...';
162 Read an invalid UTF-8 string (maybe mixed UTF-8 and 8-bit character set).
163 Did you decode with lax ":utf8" instead of strict ":encoding(UTF-8)"?
164 ...
165 }
166  
167 # Ensure Unicode character semantics, even for 0x80-0xff
168 340 utf8::upgrade($string);
169  
170 # Check for and strip any leading UTF-8 BOM
171 340 $string =~ s/^\x{FEFF}//;
172  
173 # Check for some special cases
174 340 ▶ return $self unless length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string 4 336 340
175  
176 # Split the file into lines
177 336 my @lines = grep { ! /^\s*(?:\#.*)?\z/ }
178 split /(?:\015{1,2}\012|\015|\012)/, $string;
179  
180 # Strip the initial YAML header
181 336 ▶ @lines and $lines[0] =~ /^\%YAML[: ][\d\.]+.*\z/ and shift @lines;
Branch: 1 branch
BranchTrueFalseTotal
@lines and $lines[0] =~ /^\%YAML[: ][\d\.]+.*\z/ and shift @lines 3 333 336
Condition100%3/3
@lines and $lines[0] =~ /^\%YAML[: ][\d\.]+.*\z/
!ll&&!rl&&r
23313
MC/DC100%2/2
@lines and $lines[0] =~ /^\%YAML[: ][\d\.]+.*\z/
@lines $lines[0] =~ /^\%YAML[: ][\d\.]+.*\z/
Truth table100%3/3
A and B
A= @linesB= $lines[0] =~ /^\%YAML[: ][\d\.]+.*\z/
ABresult
0X0
100
111
182  
183 # A nibbling parser
184 336 my $in_document = 0;
185 336 ▶ while ( @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 356 324 680
186 # Do we have a document header?
187 356 ▶ if ( $lines[0] =~ /^---\s*(?:(.+)\s*)?\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] =~ /^---\s*(?:(.+)\s*)?\z/) 275 81 356
188 # Handle scalar documents
189 275 shift @lines;
190 275 ▶ if ( defined $1 and $1 !~ /^(?:\#.+|\%YAML[: ][\d\.]+)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1 and not $1 =~ /^(?:\#.+|\%YAML[: ][\d\.]+)\z/) 41 234 275
Condition100%3/3
defined $1 and not $1 =~ /^(?:\#.+|\%YAML[: ][\d\.]+)\z/
!ll&&!rl&&r
225941
MC/DC100%2/2
defined $1 and not $1 =~ /^(?:\#.+|\%YAML[: ][\d\.]+)\z/
defined $1 not $1 =~ /^(?:\#.+|\%YAML[: ][\d\.]+)\z/
Truth table100%3/3
A and not(B)
A= defined $1B= not $1 =~ /^(?:\#.+|\%YAML[: ][\d\.]+)\z/
ABresult
0X0
100
111
191 41 push @$self,
192 $self->_load_scalar( "$1", [ undef ], \@lines );
193 41 next;
194 }
195 234 $in_document = 1;
196 }
197  
198 315 ▶ if ( ! @lines or $lines[0] =~ /^(?:---|\.\.\.)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not @lines or $lines[0] =~ /^(?:---|\.\.\.)/) { } 12 303 315
elsif (not $in_document and @$self) { } 2 301 303
elsif ($lines[0] =~ /^\s*\-(?:\s|$|-+$)/) { } 65 236 301
elsif ($lines[0] =~ /^(\s*)\S/) { } 236 0 236
Condition100%3/3
not @lines or $lines[0] =~ /^(?:---|\.\.\.)/
l!l&&r!l&&!r
93303
MC/DC100%2/2
not @lines or $lines[0] =~ /^(?:---|\.\.\.)/
not @lines $lines[0] =~ /^(?:---|\.\.\.)/
Truth table100%3/3
not(A) or B
A= not @linesB= $lines[0] =~ /^(?:---|\.\.\.)/
ABresult
1X1
011
000
199 # A naked document
200 12 push @$self, undef;
201 12 ▶ while ( @lines and $lines[0] !~ /^---/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and not $lines[0] =~ /^---/) 0 12 12
Condition66%2/3
@lines and not $lines[0] =~ /^---/
!ll&&!rl&&r
930
MC/DC0%0/2
@lines and not $lines[0] =~ /^---/
@lines not $lines[0] =~ /^---/
Truth table66%2/3
A and not(B)
A= @linesB= not $lines[0] =~ /^---/
ABresult
0X0
100
111
202 0 shift @lines;
203 }
204 12 $in_document = 0;
205  
206 # XXX The final '-+$' is to look for -- which ends up being an
207 # error later.
208 ▶ } elsif ( ! $in_document && @$self ) {
Condition100%3/3
not $in_document and @$self
!ll&&!rl&&r
222792
MC/DC100%2/2
not $in_document and @$self
not $in_document @$self
Truth table100%3/3
not(A) and B
A= not $in_documentB= @$self
ABresult
0X0
100
111
209 # only the first document can be explicit
210 2 die \"YAML::As::Parsed failed to classify the line '$lines[0]'";
211 } elsif ( $lines[0] =~ /^\s*\-(?:\s|$|-+$)/ ) {
212 # An array at the root
213 65 my $document = [ ];
214 65 push @$self, $document;
215 65 $self->_load_array( $document, [ 0 ], \@lines );
216  
217 } elsif ( $lines[0] =~ /^(\s*)\S/ ) {
218 # A hash at the root
219 236 my $document = ordered_hash;
220 236 push @$self, $document;
221 236 $self->_load_hash( $document, [ length($1) ], \@lines );
222  
223 } else {
224 # Shouldn't get here. @lines have whitespace-only lines
225 # stripped, and previous match is a line with any
226 # non-whitespace. So this clause should only be reachable via
227 # a perlbug where \s is not symmetric with \S
228  
229 # uncoverable statement
230 0 die \"YAML::As::Parsed failed to classify the line '$lines[0]'";
231 }
232 }
233 };
234 342 my $err = $@;
235 342 ▶ if ( ref $err eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $err eq 'SCALAR') { } 14 328 342
elsif ($err) { } 0 328 328
236 14 $self->_error(${$err});
237 } elsif ( $err ) {
238 0 $self->_error($err);
239 }
240  
241 328 return $self;
242 }
243  
244 230 ▶sub _unquote_single {
Subroutine _unquote_single: called CC=2 SCAR=6.9
245 230 my ($self, $string) = @_;
246 230 ▶ return '' unless length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string 5 225 230
247 225 $string =~ s/\'\'/\'/g;
248 225 return $string;
249 }
250  
251 87 ▶sub _unquote_double {
Subroutine _unquote_double: called CC=3 SCAR=11.0
252 87 my ($self, $string) = @_;
253 87 ▶ return '' unless length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string 1 86 87
254 86 $string =~ s/\\"/"/g;
255 86 $string =~
256 s{\\([Nnever\\fartz0b]|x([0-9a-fA-F]{2}))}
257 74 ▶ {(length($1)>1)?pack("H2",$2):$UNESCAPES{$1}}gex;
Branch: 1 branch
BranchTrueFalseTotal
length $1 > 1 ? : 13 61 74
258 86 return $string;
259 }
260  
261 943 ▶sub _load_scalar {
Subroutine _load_scalar: called CC=17 SCAR=28.4
262 943 my ($self, $string, $indent, $lines) = @_;
263  
264 # Trim trailing whitespace
265 943 $string =~ s/\s*\z//;
266  
267 # Explitic null/undef
268 943 ▶ return undef if $string eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "~" 45 898 943
269  
270 # Single quote
271 898 ▶ if ( $string =~ /^$re_capture_single_quoted$re_trailing_comment\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^$re_capture_single_quoted$re_trailing_comment\z/) 183 715 898
272 183 return $self->_unquote_single($1);
273 }
274  
275 # Double quote.
276 715 ▶ if ( $string =~ /^$re_capture_double_quoted$re_trailing_comment\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^$re_capture_double_quoted$re_trailing_comment\z/) 58 657 715
277 58 return $self->_unquote_double($1);
278 }
279  
280 # Special cases
281 657 ▶ if ( $string =~ /^[\'\"!&]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^[\'\"!&]/) 2 655 657
282 2 die \"YAML::As::Parsed does not support a feature in line '$string'";
283 }
284 655 ▶ return {} if $string =~ /^{}(?:\s+\#.*)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^{}(?:\s+\#.*)?\z/ 10 645 655
285 645 ▶ return [] if $string =~ /^\[\](?:\s+\#.*)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\[\](?:\s+\#.*)?\z/ 10 635 645
286  
287 # Regular unquoted string
288 635 ▶ if ( $string !~ /^[>|]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /^[>|]/) 623 12 635
289 623 ▶ die \"YAML::As::Parsed found illegal characters in plain scalar: '$string'"
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^(?:-(?:\s|$)|[\@\%\`])/ or $string =~ /:(?:\s|$)/ 8 615 623
Condition100%3/3
$string =~ /^(?:-(?:\s|$)|[\@\%\`])/ or $string =~ /:(?:\s|$)/
l!l&&r!l&&!r
62615
MC/DC100%2/2
$string =~ /^(?:-(?:\s|$)|[\@\%\`])/ or $string =~ /:(?:\s|$)/
$string =~ /^(?:-(?:\s|$)|[\@\%\`])/ $string =~ /:(?:\s|$)/
Truth table100%3/3
A or B
A= $string =~ /^(?:-(?:\s|$)|[\@\%\`])/B= $string =~ /:(?:\s|$)/
ABresult
1X1
011
000
290 if $string =~ /^(?:-(?:\s|$)|[\@\%\`])/ or
291 $string =~ /:(?:\s|$)/;
292 615 $string =~ s/\s+#.*\z//;
293 615 return $string;
294 }
295  
296 # Error
297 12 ▶ die \"YAML::As::Parsed failed to find multi-line scalar content" unless @$lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lines 0 12 12
298  
299 # Check the indent depth
300 12 $lines->[0] =~ /^(\s*)/;
301 12 $indent->[-1] = length("$1");
302 12 ▶ if ( defined $indent->[-2] and $indent->[-1] <= $indent->[-2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indent->[-2] and $indent->[-1] <= $indent->[-2]) 0 12 12
Condition33%1/3
defined $indent->[-2] and $indent->[-1] <= $indent->[-2]
!ll&&!rl&&r
0120
MC/DC0%0/2
defined $indent->[-2] and $indent->[-1] <= $indent->[-2]
defined $indent->[-2] $indent->[-1] <= $indent->[-2]
Truth table33%1/3
A and B
A= defined $indent->[-2]B= $indent->[-1] <= $indent->[-2]
ABresult
0X0
100
111
303 0 die \"YAML::As::Parsed found bad indenting in line '$lines->[0]'";
304 }
305  
306 # Pull the lines
307 12 my @multiline = ();
308 12 ▶ while ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 34 5 39
309 34 $lines->[0] =~ /^(\s*)/;
310 34 ▶ last unless length($1) >= $indent->[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless length $1 >= $indent->[-1] 7 27 34
311 27 push @multiline, substr(shift(@$lines), $indent->[-1]);
312 }
313  
314 12 ▶ my $j = (substr($string, 0, 1) eq '>') ? ' ' : "\n";
Branch: 1 branch
BranchTrueFalseTotal
substr($string, 0, 1) eq '>' ? : 2 10 12
315 12 ▶ my $t = (substr($string, 1, 1) eq '-') ? '' : "\n";
Branch: 1 branch
BranchTrueFalseTotal
substr($string, 1, 1) eq '-' ? : 2 10 12
316 12 return join( $j, @multiline ) . $t;
317 }
318  
319 111 ▶sub _load_array {
Subroutine _load_array: called CC=16 SCAR=28.5
320 111 my ($self, $array, $indent, $lines) = @_;
321  
322 111 ▶ while ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 282 63 345
323 # Check for a new document
324 282 ▶ if ( $lines->[0] =~ /^(?:---|\.\.\.)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines->[0] =~ /^(?:---|\.\.\.)/) 13 269 282
325 13 ▶ while ( @$lines and $lines->[0] !~ /^---/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines and not $lines->[0] =~ /^---/) 5 13 18
Condition100%3/3
@$lines and not $lines->[0] =~ /^---/
!ll&&!rl&&r
3105
MC/DC100%2/2
@$lines and not $lines->[0] =~ /^---/
@$lines not $lines->[0] =~ /^---/
Truth table100%3/3
A and not(B)
A= @$linesB= not $lines->[0] =~ /^---/
ABresult
0X0
100
111
326 5 shift @$lines;
327 }
328 13 return 1;
329 }
330  
331 # Check the indent level
332 269 $lines->[0] =~ /^(\s*)/;
333 269 ▶ if ( length($1) < $indent->[-1] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $1 < $indent->[-1]) { } 26 243 269
elsif (length $1 > $indent->[-1]) { } 0 243 243
334 26 return 1;
335 } elsif ( length($1) > $indent->[-1] ) {
336 0 die \"YAML::As::Parsed found bad indenting in line '$lines->[0]'";
337 }
338  
339 243 ▶ if ( $lines->[0] =~ /^(\s*\-\s+)[^\'\"]\S*\s*:(?:\s+|$)/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($lines->[0] =~ /^(\s*\-\s+)[^\'\"]\S*\s*:(?:\s+|$)/) { } 32 211 243
elsif ($lines->[0] =~ /^\s*\-\s*\z/) { } 38 173 211
elsif ($lines->[0] =~ /^\s*\-(\s*)(.+?)\s*\z/) { } 169 4 173
elsif (defined $indent->[-2] and $indent->[-1] == $indent->[-2]) { } 4 0 4
340 # Inline nested hash
341 32 my $indent2 = length("$1");
342 32 $lines->[0] =~ s/-/ /;
343 32 push @$array, ordered_hash;
344 32 $self->_load_hash( $array->[-1], [ @$indent, $indent2 ], $lines );
345  
346 } elsif ( $lines->[0] =~ /^\s*\-\s*\z/ ) {
347 38 shift @$lines;
348 38 ▶ unless ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$lines) 2 36 38
349 2 push @$array, undef;
350 2 return 1;
351 }
352 36 ▶ if ( $lines->[0] =~ /^(\s*)\-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lines->[0] =~ /^(\s*)\-/) { } 4 32 36
elsif ($lines->[0] =~ /^(\s*)\S/) { } 32 0 32
353 4 my $indent2 = length("$1");
354 4 ▶ if ( $indent->[-1] == $indent2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent->[-1] == $indent2) { } 4 0 4
355 # Null array entry
356 4 push @$array, undef;
357 } else {
358 # Naked indenter
359 0 push @$array, [ ];
360 0 $self->_load_array(
361 $array->[-1], [ @$indent, $indent2 ], $lines
362 );
363 }
364  
365 } elsif ( $lines->[0] =~ /^(\s*)\S/ ) {
366 32 push @$array, ordered_hash;
367 32 $self->_load_hash(
368 $array->[-1], [ @$indent, length("$1") ], $lines
369 );
370  
371 } else {
372 0 die \"YAML::As::Parsed failed to classify line '$lines->[0]'";
373 }
374  
375 } elsif ( $lines->[0] =~ /^\s*\-(\s*)(.+?)\s*\z/ ) {
376 # Array entry with a value
377 169 shift @$lines;
378 169 push @$array, $self->_load_scalar(
379 "$2", [ @$indent, undef ], $lines
380 );
381  
382 ▶ } elsif ( defined $indent->[-2] and $indent->[-1] == $indent->[-2] ) {
Condition33%1/3
defined $indent->[-2] and $indent->[-1] == $indent->[-2]
!ll&&!rl&&r
004
MC/DC0%0/2
defined $indent->[-2] and $indent->[-1] == $indent->[-2]
defined $indent->[-2] $indent->[-1] == $indent->[-2]
Truth table33%1/3
A and B
A= defined $indent->[-2]B= $indent->[-1] == $indent->[-2]
ABresult
0X0
100
111
383 # This is probably a structure like the following...
384 # ---
385 # foo:
386 # - list
387 # bar: value
388 #
389 # ... so lets return and let the hash parser handle it
390 4 return 1;
391  
392 } else {
393 0 die \"YAML::As::Parsed failed to classify line '$lines->[0]'";
394 }
395 }
396  
397 63 return 1;
398 }
399  
400 424 ▶sub _load_hash {
Subroutine _load_hash: called CC=17 SCAR=28.6
401 424 my ($self, $hash, $indent, $lines) = @_;
402  
403 424 ▶ while ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 1072 254 1326
404 # Check for a new document
405 1072 ▶ if ( $lines->[0] =~ /^(?:---|\.\.\.)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines->[0] =~ /^(?:---|\.\.\.)/) 3 1069 1072
406 3 ▶ while ( @$lines and $lines->[0] !~ /^---/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines and not $lines->[0] =~ /^---/) 1 3 4
Condition100%3/3
@$lines and not $lines->[0] =~ /^---/
!ll&&!rl&&r
121
MC/DC100%2/2
@$lines and not $lines->[0] =~ /^---/
@$lines not $lines->[0] =~ /^---/
Truth table100%3/3
A and not(B)
A= @$linesB= not $lines->[0] =~ /^---/
ABresult
0X0
100
111
407 1 shift @$lines;
408 }
409 3 return 1;
410 }
411  
412 # Check the indent level
413 1069 $lines->[0] =~ /^(\s*)/;
414 1069 ▶ if ( length($1) < $indent->[-1] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $1 < $indent->[-1]) { } 159 910 1069
elsif (length $1 > $indent->[-1]) { } 0 910 910
415 159 return 1;
416 } elsif ( length($1) > $indent->[-1] ) {
417 0 die \"YAML::As::Parsed found bad indenting in line '$lines->[0]'";
418 }
419  
420 # Find the key
421 910 my $key;
422  
423 # Quoted keys
424 910 ▶ if ( $lines->[0] =~
Branch: 4 branches
BranchTrueFalseTotal
if ($lines->[0] =~ s/^\s*$re_capture_single_quoted$re_key_value_separator//) { } 47 863 910
elsif ($lines->[0] =~ s/^\s*$re_capture_double_quoted$re_key_value_separator//) { } 29 834 863
elsif ($lines->[0] =~ s/^\s*$re_capture_unquoted_key$re_key_value_separator//) { } 834 0 834
elsif ($lines->[0] =~ /^\s*\?/) { } 0 0 0
425 s/^\s*$re_capture_single_quoted$re_key_value_separator//
426 ) {
427 47 $key = $self->_unquote_single($1);
428 }
429 elsif ( $lines->[0] =~
430 s/^\s*$re_capture_double_quoted$re_key_value_separator//
431 ) {
432 29 $key = $self->_unquote_double($1);
433 }
434 elsif ( $lines->[0] =~
435 s/^\s*$re_capture_unquoted_key$re_key_value_separator//
436 ) {
437 834 $key = $1;
438 834 $key =~ s/\s+$//;
439 }
440 elsif ( $lines->[0] =~ /^\s*\?/ ) {
441 0 die \"YAML::As::Parsed does not support a feature in line '$lines->[0]'";
442 }
443 else {
444 0 die \"YAML::As::Parsed failed to classify line '$lines->[0]'";
445 }
446  
447 910 ▶ if ( exists $hash->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$key}) 1 909 910
448 1 warn "YAML::As::Parsed found a duplicate key '$key' in line '$lines->[0]'";
449 }
450  
451 # Do we have a value?
452 910 ▶ if ( length $lines->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $lines->[0]) { } 733 177 910
453 # Yes
454 733 $hash->{$key} = $self->_load_scalar(
455 shift(@$lines), [ @$indent, undef ], $lines
456 );
457 } else {
458 # An indent
459 177 shift @$lines;
460 177 ▶ unless ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$lines) 1 176 177
461 1 $hash->{$key} = undef;
462 1 return 1;
463 }
464 176 ▶ if ( $lines->[0] =~ /^(\s*)-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lines->[0] =~ /^(\s*)-/) { } 46 130 176
elsif ($lines->[0] =~ /^(\s*)./) { } 130 0 130
465 46 $hash->{$key} = [];
466 $self->_load_array(
467 46 $hash->{$key}, [ @$indent, length($1) ], $lines
468 );
469 } elsif ( $lines->[0] =~ /^(\s*)./ ) {
470 130 my $indent2 = length("$1");
471 130 ▶ if ( $indent->[-1] >= $indent2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent->[-1] >= $indent2) { } 6 124 130
472 # Null hash entry
473 6 $hash->{$key} = undef;
474 } else {
475 124 $hash->{$key} = ordered_hash;
476 $self->_load_hash(
477 124 $hash->{$key}, [ @$indent, length($1) ], $lines
478 );
479 }
480 }
481 }
482 }
483  
484 254 return 1;
485 }
486  
487  
488  
489 4 ▶sub _dump_file {
Subroutine _dump_file: called CC=6 SCAR=18.6
490 4 my $self = shift;
491  
492 4 require Fcntl;
493  
494 # Check the file
495 4 ▶ my $file = shift or $self->_error( 'You did not specify a file name' );
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or $self->_error("You did not specify a file name") 3 1 4
496  
497 3 my $fh;
498 # flock if available (or warn if not possible for OS-specific reasons)
499 3 ▶ if ( _can_flock() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_can_flock()) { } 3 0 3
500 # Open without truncation (truncate comes after lock)
501 3 my $flags = Fcntl::O_WRONLY()|Fcntl::O_CREAT();
502 3 ▶ sysopen( $fh, $file, $flags )
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fh, $file, $flags or $self->_error("Failed to open file '${file}' for writing: $!") 3 0 3
503 or $self->_error("Failed to open file '$file' for writing: $!");
504  
505 # Use no translation and strict UTF-8
506 3 binmode( $fh, ":raw:encoding(UTF-8)");
507  
508 3 ▶ flock( $fh, Fcntl::LOCK_EX() )
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or warn "Couldn't lock '${file}' for reading: $!" 3 0 3
509 or warn "Couldn't lock '$file' for reading: $!";
510  
511 # truncate and spew contents
512 3 truncate $fh, 0;
513 3 seek $fh, 0, 0;
514 }
515 else {
516 0 open $fh, ">:unix:encoding(UTF-8)", $file;
517 }
518  
519 # serialize and spew to the handle
520 3 print {$fh} $self->_dump_string;
521  
522 # close the file (release the lock)
523 3 ▶ unless ( close $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fh) 0 3 3
524 0 $self->_error("Failed to close file '$file': $!");
525 }
526  
527 3 return 1;
528 }
529  
530 267 ▶sub _dump_string {
Subroutine _dump_string: called CC=12 SCAR=24.9
531 267 my $self = shift;
532 267 ▶ return '' unless ref $self && @$self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self and @$self 4 263 267
Condition100%3/3
ref $self and @$self
!ll&&!rl&&r
13263
MC/DC100%2/2
ref $self and @$self
ref $self @$self
Truth table100%3/3
A and B
A= ref $selfB= @$self
ABresult
0X0
100
111
533  
534 # Iterate over the documents
535 263 my $indent = 0;
536 263 my @lines = ();
537  
538 263 eval {
539 263 foreach my $cursor ( @$self ) {
540 272 push @lines, '---';
541  
542 # An empty document
543 272 ▶ if ( ! defined $cursor ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $cursor) { } 7 265 272
elsif (not ref $cursor) { } 17 248 265
elsif (ref $cursor eq 'ARRAY') { } 31 217 248
elsif (ref $cursor eq 'HASH') { } 217 0 217
544 # Do nothing
545  
546 # A scalar document
547 } elsif ( ! ref $cursor ) {
548 17 $lines[-1] .= ' ' . $self->_dump_scalar( $cursor );
549  
550 # A list at the root
551 } elsif ( ref $cursor eq 'ARRAY' ) {
552 31 ▶ unless ( @$cursor ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$cursor) 1 30 31
553 1 $lines[-1] .= ' []';
554 1 next;
555 }
556 30 push @lines, $self->_dump_array( $cursor, $indent, {} );
557  
558 # A hash at the root
559 } elsif ( ref $cursor eq 'HASH' ) {
560 217 ▶ unless ( %$cursor ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$cursor) 1 216 217
561 1 $lines[-1] .= ' {}';
562 1 next;
563 }
564 216 push @lines, $self->_dump_hash( $cursor, $indent, {} );
565  
566 } else {
567 0 die \("Cannot serialize " . ref($cursor));
568 }
569 }
570 };
571 263 ▶ if ( ref $@ eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $@ eq 'SCALAR') { } 1 262 263
elsif ($@) { } 0 262 262
572 1 $self->_error(${$@});
573 } elsif ( $@ ) {
574 0 $self->_error($@);
575 }
576  
577 262 join '', map { "$_\n" } @lines;
578 }
579  
580 1085 ▶sub _has_internal_string_value {
Subroutine _has_internal_string_value: called CC=1 SCAR=0.0
581 1085 my $value = shift;
582 1085 my $b_obj = B::svref_2object(\$value); # for round trip problem
583 1085 return $b_obj->FLAGS & B::SVf_POK();
584 }
585  
586 1085 ▶sub _dump_scalar {
Subroutine _dump_scalar: called CC=9 SCAR=22.0
587 1085 my $string = $_[1];
588 1085 my $is_key = $_[2];
589 # Check this before checking length or it winds up looking like a string!
590 1085 my $has_string_flag = _has_internal_string_value($string);
591 1085 ▶ return '~' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 24 1061 1085
592 1061 ▶ return "''" unless length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $string 3 1058 1061
593 1058 ▶ if (Scalar::Util::looks_like_number($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($string)) 80 978 1058
594 # keys and values that have been used as strings get quoted
595 80 ▶ if ( $is_key || $has_string_flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_key or $has_string_flag) { } 56 24 80
Condition100%3/3
$is_key or $has_string_flag
l!l&&r!l&&!r
35324
MC/DC100%2/2
$is_key or $has_string_flag
$is_key $has_string_flag
Truth table100%3/3
A or B
A= $is_keyB= $has_string_flag
ABresult
1X1
011
000
596 56 return qq['$string'];
597 }
598 else {
599 24 return $string;
600 }
601 }
602 978 ▶ if ( $string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n]/) 98 880 978
603 98 $string =~ s/\\/\\\\/g;
604 98 $string =~ s/"/\\"/g;
605 98 $string =~ s/\n/\\n/g;
606 98 $string =~ s/[\x85]/\\N/g;
607 98 $string =~ s/([\x00-\x1f])/\\$UNPRINTABLE[ord($1)]/g;
608 98 $string =~ s/([\x7f-\x9f])/'\x' . sprintf("%X",ord($1))/ge;
609 98 return qq|"$string"|;
610 }
611 880 ▶ if ( $string =~ /(?:^[~!@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ or
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ or $QUOTE{$string}) 191 689 880
Condition100%3/3
$string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ or $QUOTE{$string}
l!l&&r!l&&!r
1874689
MC/DC100%2/2
$string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ or $QUOTE{$string}
$string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ $QUOTE{$string}
Truth table100%3/3
A or B
A= $string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/B= $QUOTE{$string}
ABresult
1X1
011
000
612 $QUOTE{$string}
613 ) {
614 191 return "'$string'";
615 }
616 689 return $string;
617 }
618  
619 53 ▶sub _dump_array {
Subroutine _dump_array: called CC=8 SCAR=21.5
620 53 my ($self, $array, $indent, $seen) = @_;
621 53 ▶ if ( $seen->{refaddr($array)}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seen{&refaddr($array)}++) 0 53 53
622 0 die \"YAML::As::Parsed does not support circular references";
623 }
624 53 my @lines = ();
625 53 foreach my $el ( @$array ) {
626 117 my $line = (' ' x $indent) . '-';
627 117 my $type = ref $el;
628 117 ▶ if ( ! $type ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 84 33 117
elsif ($type eq 'ARRAY') { } 1 32 33
elsif ($type eq 'HASH') { } 32 0 32
629 84 $line .= ' ' . $self->_dump_scalar( $el );
630 84 push @lines, $line;
631  
632 } elsif ( $type eq 'ARRAY' ) {
633 1 ▶ if ( @$el ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$el) { } 0 1 1
634 0 push @lines, $line;
635 0 push @lines, $self->_dump_array( $el, $indent + 1, $seen );
636 } else {
637 1 $line .= ' []';
638 1 push @lines, $line;
639 }
640  
641 } elsif ( $type eq 'HASH' ) {
642 32 ▶ if ( keys %$el ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$el) { } 31 1 32
643 31 push @lines, $line;
644 31 push @lines, $self->_dump_hash( $el, $indent + 1, $seen );
645 } else {
646 1 $line .= ' {}';
647 1 push @lines, $line;
648 }
649  
650 } else {
651 0 die \"YAML::As::Parsed does not support $type references";
652 }
653 }
654  
655 51 @lines;
656 }
657  
658 308 ▶sub _dump_hash {
Subroutine _dump_hash: called CC=8 SCAR=20.8
659 308 my ($self, $hash, $indent, $seen) = @_;
660 308 ▶ if ( $seen->{refaddr($hash)}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seen{&refaddr($hash)}++) 1 307 308
661 1 die \"YAML::As::Parsed does not support circular references";
662 }
663 307 my @lines = ();
664 307 foreach my $name ( sort keys %$hash ) {
665 538 my $el = $hash->{$name};
666 538 my $line = (' ' x $indent) . $self->_dump_scalar($name, 1) . ":";
667 538 my $type = ref $el;
668 538 ▶ if ( ! $type ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 448 90 538
elsif ($type eq 'ARRAY') { } 26 64 90
elsif ($type eq 'HASH') { } 64 0 64
669 448 $line .= ' ' . $self->_dump_scalar( $el );
670 448 push @lines, $line;
671  
672 } elsif ( $type eq 'ARRAY' ) {
673 26 ▶ if ( @$el ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$el) { } 23 3 26
674 23 push @lines, $line;
675 23 push @lines, $self->_dump_array( $el, $indent + 1, $seen );
676 } else {
677 3 $line .= ' []';
678 3 push @lines, $line;
679 }
680  
681 } elsif ( $type eq 'HASH' ) {
682 64 ▶ if ( keys %$el ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$el) { } 61 3 64
683 61 push @lines, $line;
684 61 push @lines, $self->_dump_hash( $el, $indent + 1, $seen );
685 } else {
686 3 $line .= ' {}';
687 3 push @lines, $line;
688 }
689  
690 } else {
691 0 die \"YAML::As::Parsed does not support $type references";
692 }
693 }
694  
695 306 @lines;
696 }
697  
698 11 our $errstr = '';
699  
700 21 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
701 21 require Carp;
702 21 $errstr = $_[1];
703 21 $errstr =~ s/ at \S+ line \d+.*//;
704 21 Carp::croak( $errstr );
705 }
706  
707 11 my $errstr_warned;
708 0 ▶sub errstr {
Subroutine errstr: not called CC=2 SCAR=17.9
Pod undocumented
709 0 require Carp;
710 0 ▶ Carp::carp( "YAML::As::Parsed->errstr and \$YAML::As::Parsed::errstr is deprecated" )
Branch: 1 branch
BranchTrueFalseTotal
unless $errstr_warned++ 0 0 0
711 unless $errstr_warned++;
712 0 $errstr;
713 }
714  
715 11 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
716  
717 my $HAS_FLOCK;
718 14 ▶sub _can_flock {
Subroutine _can_flock: called CC=3 SCAR=11.0
719 14 ▶ if ( defined $HAS_FLOCK ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $HAS_FLOCK) { } 12 2 14
720 12 return $HAS_FLOCK;
721 }
722 else {
723 2 require Config;
724 2 my $c = \%Config::Config;
725 2 $HAS_FLOCK = grep { $c->{$_} } qw/d_flock d_fcntl_can_lock d_lockf/;
726 2 ▶ require Fcntl if $HAS_FLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $HAS_FLOCK 2 0 2
727 2 return $HAS_FLOCK;
728 }
729 }
730  
731 11 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
732 11 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
733 11 local $@;
734 11 ▶ if ( eval { Scalar::Util->VERSION(1.18); } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { 'Scalar::Util'->VERSION(1.18) } }) { } 11 0 11
735 11 *refaddr = *Scalar::Util::refaddr;
736 }
737 else {
738 0 eval <<'END_PERL';
739 sub refaddr {
740 my $pkg = ref($_[0]) or return undef;
741 if ( !! UNIVERSAL::can($_[0], 'can') ) {
742 bless $_[0], 'Scalar::Util::Fake';
743 } else {
744 $pkg = undef;
745 }
746 "$_[0]" =~ /0x(\w+)/;
747 my $i = do { no warnings 'portable'; hex $1 };
748 bless $_[0], $pkg if defined $pkg;
749 $i;
750 }
751 END_PERL
752 }
753 }
754  
755 11 delete $YAML::As::Parsed::{refaddr};
756  
757 11 1;
758  
759 __END__
↑ Summary