blib/lib/YAML/LoadBundle.pm

filter: Statementstmt 88.5% 208 / 235 Branchbran 70.4% 86 / 122 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 89.2% 25 / 28 Podpod 100% 4 / 4 totaltotal 81.4% 333 / 409 CC 68 SCAR 27.8
CC
68
Cov
82%
load_yaml_bundle
39.1
_yaml_cache_peek
14.4
load_yaml
11.2
CRAP 16.1
↑ Summary
1 package YAML::LoadBundle;
2 # ABSTRACT: Load a directory of YAML files as a bundle
3 5 $YAML::LoadBundle::VERSION = '0.001';
4 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Cwd qw( abs_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Digest::SHA1 qw( sha1_hex sha1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Hash::Merge::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Scalar::Util qw( reftype refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Storable qw( freeze );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use YAML::XS qw(Load);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #use Data::Visitor::Callback;
18  
19 5 our @EXPORT_OK = qw(
20 load_yaml
21 load_yaml_bundle
22 add_yaml_observer
23 remove_yaml_observer
24 );
25 5 our %EXPORT_TAGS = ( all => [@EXPORT_OK] );
26  
27 5 our $CacheDir;
28 5 ▶$CacheDir = $ENV{YAML_LOADBUNDLE_CACHEDIR} unless defined $CacheDir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $YAML::LoadBundle::CacheDir 5 0 5
29  
30 5 my @load_yaml_observers;
31  
32 sub add_yaml_observer {
33 1 ▶ my $observer = shift;
Subroutine add_yaml_observer: called CC=2 SCAR=7.1
Pod documented
34 1 ▶ die "Observer must be a code ref." unless ref($observer) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $observer eq "CODE" 0 1 1
35 1 push @load_yaml_observers, $observer;
36 }
37  
38 sub _notify_yaml_observers {
39 5 ▶ my $file = shift;
Subroutine _notify_yaml_observers: called CC=2 SCAR=6.9
40 5 for my $observer (@load_yaml_observers) {
41 1 $observer->($file);
42 }
43 }
44  
45 sub remove_yaml_observer {
46 1 ▶ my $observer = shift;
Subroutine remove_yaml_observer: called CC=2 SCAR=7.0
Pod documented
47 1 ▶ die "Observer must be a code ref." unless ref($observer) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $observer eq "CODE" 0 1 1
48 1 my $obref = refaddr $observer;
49  
50 @load_yaml_observers = grep {
51 1 refaddr($_) != $obref
52 } @load_yaml_observers;
53 }
54  
55 sub load_yaml {
56 27 ▶ my ($arg, $dont_cache) = @_;
Subroutine load_yaml: called CC=11 SCAR=24.2
Pod documented
57 27 my @yaml;
58 my $cache_mtime;
59 27 my %params;
60  
61 27 ▶ if (ref $arg) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $arg) { } 2 25 27
elsif ($arg =~ /\n/) { } 20 5 25
elsif (-f $arg and -s _) { } 5 0 5
62 2 @yaml = <$arg>;
63 }
64 elsif ($arg =~ /\n/) {
65 20 my $digest = sha1($arg);
66 20 $cache_mtime = 1;
67 20 my $perl = _yaml_cache_peek($digest, $cache_mtime);
68 20 ▶ return $perl if defined $perl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $perl 4 16 20
69 16 open my $fh, '<', \$arg;
70 16 @yaml = <$fh>;
71 16 $arg = $digest;
72 16 $params{no_disk_cache} = 1;
73 }
74 ▶ elsif (-f $arg and -s _) {
Condition33%1/3
-f $arg and -s _
!ll&&!rl&&r
005
MC/DC0%0/2
-f $arg and -s _
-f $arg -s _
Truth table33%1/3
A and B
A= -f $argB= -s _
ABresult
0X0
100
111
75 # $arg is a file path.
76 5 _notify_yaml_observers($arg);
77  
78 5 my $mtime = (stat _)[9];
79 5 my $perl = _yaml_cache_peek($arg, $mtime);
80 5 ▶ return $perl if defined $perl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $perl 1 4 5
81  
82 4 ▶ open my $fh, $arg
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $arg or croak("Can't open YAML file ${arg}: $!") 0 4 4
83 or croak "Can't open YAML file $arg: $!";
84 4 @yaml = <$fh>;
85 4 $cache_mtime = $mtime;
86 }
87 else {
88 0 croak "Can't load empty/missing YAML file: $arg.";
89 }
90  
91 22 my $perl;
92 22 eval { $perl = Load(join '', @yaml) };
93  
94 22 ▶ die "$@\nYAML File: $arg\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 21 22
95  
96 # Can't cache/flatten empty YAML
97 21 ▶ return unless $perl;
Branch: 1 branch
BranchTrueFalseTotal
unless $perl 0 21 21
98  
99 # TODO: this is a temporary fix. previous functionaly skipped caching if a
100 # second arg was passed into load_yaml. a recent refactor introduced a bug
101 # that caused the code to never cache. as a temporary workaround we will
102 # just set $cache_mtime to 0 if there's a second arg to this method. this
103 # will tell _unravel_and_cache to skip the caching step.
104 21 ▶ $cache_mtime = 0 if $dont_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $dont_cache 1 20 21
105 21 $perl = _unravel_and_cache($arg, $perl, $cache_mtime, %params);
106  
107 21 return $perl;
108 }
109  
110 my $shallow_merge = sub {
111 18 ▶ my ($left, $right) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
112 18 ▶ if (reftype($left) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($left) eq "ARRAY") 1 17 18
113 1 $left = { map %$_, @$left };
114 }
115 return (
116 18 ▶ (map { %$_ } (reftype($left) eq 'ARRAY' ? @$left : $left)),
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($left) eq 'ARRAY' ? : 0 18 18
117 %$right
118 );
119 5 };
120 my $deep_merge = sub {
121 17 ▶ my ($left, $right) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
122 17 ▶ return %{ Hash::Merge::Simple->merge(
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($left) eq 'ARRAY' ? : 1 16 17
123 (reftype($left) eq 'ARRAY' ? @$left : $left),
124 $right,
125 ) };
126 5 };
127  
128 # in order of priority:
129 5 my @SPECIAL = qw(
130 -merge
131 export
132 -export
133 import
134 -import
135 );
136 5 my %SPECIAL = (
137 -import => $shallow_merge,
138 import => $shallow_merge,
139 -export => $shallow_merge,
140 export => $shallow_merge,
141 -merge => $deep_merge,
142 );
143  
144 {
145 package YAML::LoadBundle::YAMLDV;
146 5 $YAML::LoadBundle::YAMLDV::VERSION = '0.001';
147 5 ▶use base qw(Data::Visitor::Callback);
Subroutine BEGIN: called CC=1 SCAR=0.0
148 # something in the way Data::Visitor tracks references it's already seen
149 # causes bizarre bugs -- it seems like memory addresses can get reused,
150 # meaning Data::Visitor grafts data into random places later down the
151 # tree.
152 # We don't have any circularity here, which is what D::V's refaddr cache
153 # exists to handle.
154 # For D::V 0.27, we could get the same result without subclassing like
155 # this:
156 # seen => sub {
157 # my ($v, $data, $result) = @_;
158 # return $v->visit_no_rec_check($data);
159 # },
160 247322 ▶ sub visit { shift->visit_no_rec_check(@_) }
Subroutine visit: called CC=1 SCAR=0.0
161 }
162  
163 5 sub _visit_hash {
164 12427 ▶ my ($v, $data) = @_;
Subroutine _visit_hash: called CC=7 SCAR=19.5
165 12427 my $result = {%$data};
166  
167 12427 ▶ while (my @keys = grep { exists $result->{$_} } @SPECIAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@keys) = grep({exists $result->{$_};} @SPECIAL)) 17 12427 12444
168 17 for my $key (@keys) {
169 19 my $handler = $SPECIAL{$key};
170 19 my $val = delete $result->{$key};
171 19 ▶ next unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 19 19
172 19 %$result = $handler->($v->visit($val), $result);
173 }
174 }
175  
176 12427 ▶ if (keys %$result == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$result == 1) 36 12391 12427
177 36 ▶ if (my $arrs = $result->{-flatten}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $arrs = $result->{'-flatten'}) { } 3 33 36
elsif (my $hrefs = $result->{'-flattenhash'}) { } 1 32 33
178 3 return [ map { @{ $v->visit($_) } } @$arrs ];
179 }
180 elsif (my $hrefs = $result->{-flattenhash}) {
181 1 return { map { %{ $v->visit($_) } } @$hrefs };
182 }
183 }
184  
185 12423 return $result;
186 }
187  
188 {
189 5 my %YAML_cache;
190 sub _unravel_and_cache {
191 23 ▶ my ($path, $perl, $cache_mtime, %params) = @_;
Subroutine _unravel_and_cache: called CC=7 SCAR=19.6
192  
193 23 my $v = YAML::LoadBundle::YAMLDV->new(
194 hash => \&_visit_hash,
195 );
196 23 $perl = do {
197 23 ▶ local $DB::deep = 500 if defined $DB::deep;
Branch: 1 branch
BranchTrueFalseTotal
if defined $DB::deep 0 23 23
198 23 $v->visit($perl);
199 };
200  
201 # TODO: need a better way to explicitly not cache here
202 23 ▶ if ($cache_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_mtime) 20 3 23
203 20 my $frozen = Storable::freeze($perl);
204 20 $YAML_cache{$path} = [ $cache_mtime, $frozen ];
205 20 ▶ if ($CacheDir and not $params{no_disk_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($YAML::LoadBundle::CacheDir and not $params{"no_disk_cache"}) 2 18 20
Condition100%3/3
$YAML::LoadBundle::CacheDir and not $params{"no_disk_cache"}
!ll&&!rl&&r
1442
MC/DC100%2/2
$YAML::LoadBundle::CacheDir and not $params{"no_disk_cache"}
$YAML::LoadBundle::CacheDir not $params{"no_disk_cache"}
Truth table100%3/3
A and not(B)
A= $YAML::LoadBundle::CacheDirB= not $params{"no_disk_cache"}
ABresult
0X0
100
111
206 2 my $cache_file = join "/", $CacheDir, sha1_hex($path);
207  
208 2 eval { mkdir $CacheDir };
209 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 2 2
210 0 warn "Can't write yaml cache: $@";
211 }
212 else {
213 2 ▶ open my $fh, '>', $cache_file or die "Cannot open $cache_file for writing $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cache_file or die "Cannot open $cache_file for writing $!" 0 2 2
214 2 print $fh $frozen;
215 }
216 }
217 }
218  
219 23 return $perl;
220 }
221  
222 sub _yaml_cache_peek {
223 27 ▶ my ($path, $mtime) = @_;
Subroutine _yaml_cache_peek: called CC=7 SCAR=26.7
224  
225 27 my $cache = $YAML_cache{$path};
226 27 ▶ if ($cache) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cache) { } 5 22 27
elsif ($YAML::LoadBundle::CacheDir) { } 7 15 22
227 5 my ($oldtime, $oldyaml) = @$cache;
228 5 ▶ return Storable::thaw($oldyaml) if $oldtime == $mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $oldtime == $mtime 5 0 5
229 }
230 elsif ($CacheDir) {
231 7 my $cache_file = join "/", $CacheDir, sha1_hex($path);
232 7 ▶ if (-f $cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cache_file) 0 7 7
233 0 my $cache_time = (stat $cache_file)[9];
234 0 ▶ if ($cache_time >= $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_time >= $mtime) { } 0 0 0
235 0 open my $fh, "<$cache_file";
236 0 my $file_contents = do { local $/; <$fh> };
237 0 my $thawed = Storable::thaw($file_contents);
238 0 $YAML_cache{$path} = [ $mtime, $file_contents ];
239 0 ▶ return $thawed if $cache_time >= $mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $cache_time >= $mtime 0 0 0
240 }
241 else {
242 0 unlink $cache_file;
243 }
244 }
245 }
246  
247 22 return;
248 }
249 }
250  
251 {
252 5 my %default_options = (
253 follow_symlinks_when => 'bundled',
254 follow_symlinks_fail => 'error',
255 conf_suffixes => [ 'conf', 'yml' ],
256 max_depth => 20,
257 );
258  
259 my %symlink_skipper = (
260 0 ▶ error => sub { croak "Symlink $_[1] was skipped.\nYAML Bundle: $_[0]\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 ▶ warn => sub { carp "Symlink $_[1] was skipped.\nYAML Bundle: $_[0]\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 ▶ ignore => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
263 5 );
264  
265 sub _merge_bundle {
266 16 ▶ my ($current, $nested) = @_;
Subroutine _merge_bundle: called CC=4 SCAR=16.4
267  
268 16 ▶ if (ref($nested) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $nested eq 'ARRAY') { } 0 16 16
269 0 ▶ $current = [] unless defined $current;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current 0 0 0
270 0 return +{ $deep_merge->($current, $nested) };
271 }
272 else {
273 16 ▶ $current = {} unless defined $current;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current 14 2 16
274 16 return +{ $deep_merge->($current, $nested) };
275 }
276 }
277  
278 sub load_yaml_bundle {
279 18 ▶ my ($path, $given_options) = @_;
Subroutine load_yaml_bundle: called CC=29 SCAR=36.7
Pod documented
280 18 my $cache_mtime;
281  
282 # Setup the default configuration
283 my %options = (
284 18 ▶ %{ $given_options || {} },
Branch: 1 branch
BranchTrueFalseTotal
$given_options or {} 2 16 18
285 %default_options,
286 );
287  
288 # Add _vars to the options to allow recursive calls to share state.
289 4 $options{_match_suffix} = join "|", map { quotemeta } @{ $options{conf_suffixes} }
290 18 ▶ unless defined $options{_match_suffix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"_match_suffix"} 2 16 18
291 18 $options{max_depth}--;
292  
293 # Calculate the absolute base path to start from
294 18 ▶ unless (defined $options{_original_path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{"_original_path"}) 2 16 18
295 2 $options{_original_path} = abs_path($path);
296 2 $options{_original_path_length} = length $options{_original_path};
297  
298 # This is the top call, so check the cache
299 2 my $this_mtime;
300 2 $cache_mtime = 0;
301 find({
302 follow_fast => 1,
303 wanted => sub {
304 22 ▶ if (/^.*\.(?:$options{_match_suffix})\z/s) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (/^.*\.(?:$options{"_match_suffix"})\z/s) 10 12 22
305 10 $this_mtime = (lstat _)[9];
306 10 ▶ $cache_mtime = $this_mtime if $this_mtime > $cache_mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $this_mtime > $cache_mtime 2 8 10
307 }
308 },
309 },
310 $options{_original_path},
311 4 grep { -f $_ }
312 4 map { "$options{_original_path}.$_" }
313 2 @{ $options{conf_suffixes} }
314 );
315 2 my $perl = _yaml_cache_peek($path, $cache_mtime);
316  
317 2 ▶ return $perl if defined $perl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $perl 0 2 2
318 }
319  
320 18 my $symlink_skipper = $symlink_skipper{ $options{follow_symlinks_fail} };
321  
322 # Stop, we've gone too far.
323 18 ▶ if ($options{max_depth} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"max_depth"} < 0) 0 18 18
324 0 carp "Reached maximum path search depth while at $path.\nYAML Bundle: $options{_original_path}\n";
325 0 return;
326 }
327  
328 18 my $perl;
329  
330 # Do we have a top level .conf/.yml/.whatever in the bundle?
331 18 for my $suffix (@{ $options{conf_suffixes} }) {
332 36 my $file = $path . '.' . $suffix;
333 36 ▶ if (-f $file and -s _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file and -s _) 10 26 36
Condition66%2/3
-f $file and -s _
!ll&&!rl&&r
26010
MC/DC50%1/2
-f $file and -s _
-f $file -s _
Truth table66%2/3
A and B
A= -f $fileB= -s _
ABresult
0X0
100
111
334  
335 # If $perl is already defined, we have a case where multiple
336 # configuration files are present, which is not a defined case.
337 10 ▶ carp "Multiple configuration files match $path. This will lead to unexpected results.\nYAML Bundle: $options{_original_path}\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $perl 0 10 10
338 if defined $perl;
339  
340 # We don't use load_yml because we don't want the intermediate
341 # pieces cached and it does a lot of work we'd repeat anyway.
342  
343 10 ▶ open my $fh, $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or croak("Can't open YAML file ${file}: $!") 0 10 10
344 or croak "Can't open YAML file $file: $!";
345 10 my $yaml = do { local $/; <$fh> };
346  
347 10 $perl = eval { Load($yaml) };
348 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
349 0 croak "Eror in file $file: $@\nYAML Bundle: $options{_original_path}\n";
350 }
351 }
352 }
353  
354 # if no file found, we have to start somewhere
355 18 ▶ $perl = {} unless defined $perl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perl 8 10 18
356  
357 # If this is a directory, let's suck in all the nested configs
358 18 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) 12 6 18
359 12 ▶ opendir my $dir_fh, $path or croak "Cannot opendir $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir_fh, $path or croak("Cannot opendir ${path}: $!") 0 12 12
360  
361 # Saves us from duplicating work while recursing...
362 12 my %closed_list;
363  
364 12 ▶ ENTRY: while (my $entry = readdir $dir_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dir_fh)) 44 12 56
365  
366 # Ignore all dot files
367 44 ▶ next if $entry =~ m{^[.]};
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^[.]/ 24 20 44
368  
369 20 my $nested_path = abs_path("$path/$entry");
370  
371 20 ▶ if (not defined $nested_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nested_path) 0 20 20
372 0 croak "Broken symlink or other problem while locating $path/$entry.\nYAML Bundle: $options{_original_path}\n";
373 }
374  
375 # If bundled, make sure this abs path is in the root bas path
376 20 ▶ if ($options{follow_symlinks_when} eq 'bundled') {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'follow_symlinks_when'} eq 'bundled') { } 20 0 20
elsif ($options{'follow_symlinks_when'} eq 'never') { } 0 0 0
377 20 ▶ unless (substr($nested_path, 0, $options{_original_path_length}) eq $options{_original_path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($nested_path, 0, $options{"_original_path_length"}) eq $options{"_original_path"}) 0 20 20
378 0 $symlink_skipper->($options{_original_path}, "$path/$entry");
379 0 next ENTRY;
380 }
381 }
382  
383 # If never, skip any symlink
384 elsif ($options{follow_symlinks_when} eq 'never') {
385 0 ▶ if (-l "$path/$entry") {
Branch: 1 branch
BranchTrueFalseTotal
if (-l "$path/$entry") 0 0 0
386 0 $symlink_skipper->($options{_original_path}, "$path/$entry");
387 0 next ENTRY;
388 }
389 }
390  
391 # Is this a directory? If so, load that as a bundle.
392 20 ▶ if (-d $nested_path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $nested_path) { } 10 10 20
elsif (-f $nested_path and $entry =~ s/[.](?:$options{'_match_suffix'})$//) { } 10 0 10
393 10 ▶ next ENTRY if $closed_list{$nested_path};
Branch: 1 branch
BranchTrueFalseTotal
if $closed_list{$nested_path} 4 6 10
394  
395 # We don't follow symlinks to directories. This is a naive way
396 # to prevent infinite recursion.
397 6 ▶ if (-l "$path/$entry") {
Branch: 1 branch
BranchTrueFalseTotal
if (-l "$path/$entry") 0 6 6
398 0 croak "Symlink to directory $path/$entry is not permitted.\nYAML Bundle: $options{_original_path}\n";
399 }
400  
401 # Load the nested bundle and merge.
402 6 $closed_list{$nested_path}++;
403 $perl->{ $entry } = _merge_bundle(
404 6 $perl->{ $entry },
405 load_yaml_bundle($nested_path, \%options)
406 );
407 }
408  
409 # Is this a file with the right suffix?
410 ▶ elsif (-f $nested_path and $entry =~ s/[.](?:$options{_match_suffix})$//) {
Condition33%1/3
-f $nested_path and $entry =~ s/[.](?:$options{"_match_suffix"})$//
!ll&&!rl&&r
0010
MC/DC0%0/2
-f $nested_path and $entry =~ s/[.](?:$options{"_match_suffix"})$//
-f $nested_path $entry =~ s/[.](?:$options{"_match_suffix"})$//
Truth table33%1/3
A and B
A= -f $nested_pathB= $entry =~ s/[.](?:$options{"_match_suffix"})$//
ABresult
0X0
100
111
411 10 my $nested_path_minus_suffix = $nested_path;
412 10 $nested_path_minus_suffix =~ s/[.](?:$options{_match_suffix})$//;
413 10 ▶ next ENTRY if $closed_list{$nested_path_minus_suffix};
Branch: 1 branch
BranchTrueFalseTotal
if $closed_list{$nested_path_minus_suffix} 0 10 10
414  
415 # Load the nested bundle and merge.
416 10 $closed_list{$nested_path_minus_suffix}++;
417 $perl->{ $entry } = _merge_bundle(
418 10 $perl->{ $entry },
419 load_yaml_bundle($nested_path_minus_suffix, \%options)
420 );
421 }
422  
423 # What the hey? Carp about this...
424 else {
425 0 carp "Ignoring unexpected path $nested_path of unknown type.\nYAML Bundle: $options{_original_path}\n";
426 }
427 }
428 }
429  
430 # Only unravel our format layer and cache the top
431 # $cache_mtime is only set in the call _original_path is set
432 18 ▶ if ($cache_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_mtime) 2 16 18
433 2 $perl = _unravel_and_cache($options{_original_path}, $perl, $cache_mtime);
434 }
435  
436 18 return $perl;
437 }
438 }
439  
440 5 1;
441  
442 __END__
443  
444 =pod
445  
446 =encoding UTF-8
447  
448 =head1 NAME
449  
450 YAML::LoadBundle - Load a directory of YAML files as a bundle
451  
452 =head1 VERSION
453  
454 version 0.001
455  
456 =head1 SYNOPSIS
457  
458 use YAML::LoadBundle qw(:all);
459  
460 my $hash = load_yaml_bundle( "/path/to/yaml_bundle/dir/" );
461  
462 =head1 DESCRIPTION
463  
464 Adds additonal features to YAML::XS to allow splitting a YAML file into
465 multiple files in a common directory.
466 This helps with readability when the file is large.
467  
468 It also provides more advanced merging features than the standard YAML spec.
469  
470 =head1 Export
471  
472 Nothing is exported by default, but all the functions listed
473 below are available for export.
474  
475 All will be exported with :all.
476  
477 =head1 Exported Functions
478  
479 =head2 Load
480  
481 =head3 load_yaml
482  
483 load_yaml($filename)
484 load_yaml(\*FILEHANDLE)
485 load_yaml($yaml_data)
486  
487 Parses a YAML file (or string) with extra error-checking.
488  
489 When passed a $filename, the %YAML_cache will cache a dclone'd
490 copy of the result for later retrieval, unless the file has
491 been modified since the last load. This can be prevented by
492 passing anything else as a 2nd parameter, if you know that the
493 file will never be reloaded.
494  
495 When passed a string, the %YAML_cache will cache a copy of the result,
496 using the SHA1 digest of the string as the hash key.
497 Strings are only cached in memory, not on disk.
498  
499 After loading the YAML into a Perl data structure, some postprocessing is done
500 on specially-named keys in hash references. Each is merged into their
501 containing hash reference, though at different times and with different
502 strategies.
503  
504 =over
505  
506 =item * C<-import>
507  
508 shallow merge (e.g. C<%x = (%y, %x)>)
509  
510 =item * C<-export>
511  
512 shallow merge
513  
514 =item * C<-merge>
515  
516 deep merge (see L<Hash::Merge::Simple>)
517  
518 =item * <-flatten>
519  
520 some_key: { -flatten: [*SomeArrayRef, *SomeOtherArrayRef] }
521  
522 Instead of doing any kind of special hash merging, this special key takes an
523 arrayref of arrayrefs, merges all their contents into one large arrayref, then
524 replaces its entire surrounding hash with the arrayref.
525  
526 In other words, the example above would look like this in Perl:
527  
528 { some_key => [@$some_array_ref, @$some_other_array_ref] }
529  
530 =item * C<-flattenhash>
531  
532 some_key: { -flattenhash: [*SomeHashRef, *SomeOtherHashRef] }
533  
534 Sometimes it is desirable to import more than one hash object,
535 but cannot due to limitations of keynames like -import.
536 If you really want this behavior, add this flag.
537  
538 =back
539  
540 C<import> and C<export> are backwards-compatibility synonyms for C<-import>
541 and C<-export>.
542  
543 Like normal list assignment in Perl, the right-hand side takes precedence
544 (pseudocode):
545  
546 %hash = deep_merge(%merge, shallow_merge(%import, %export, %hash));
547  
548 Instead of a hash reference, any of these keys may contain an array reference
549 of hash references, in which case those hash references are merged using
550 whatever strategy normally applies (e.g. deep merge for C<-merge>).
551  
552 =head3 load_yaml_bundle
553  
554 load_yaml_bundle($path, \%options)
555  
556 Similar to L</load_yaml>, but loads YAML from a bundle of configuration files.
557 This may be a single file, a directory containing configuration files,
558 or a whole directory tree of configuration files.
559  
560 The given path names the base location for the bundle.
561 This starts by loading a file with the given name followed by a configuration
562 prefix (either C<.yml> or C<.conf> by default).
563 It then checks to see if there is a directory with the same name as the path.
564 It then repeats the loading process for all nested files and directories where
565 the file and directory names become the keys into which the configuration is
566 injected.
567  
568 For example, given a directory layout like this:
569  
570 conf/base.conf
571 conf/base/common.conf
572 conf/base/user.conf
573 conf/base/user/accounts.conf
574  
575 A hash would be returned mapped something like this (pseudo-code):
576  
577 {
578 common => load_yaml("conf/base/common.conf"),
579 user => {
580 accounts => load_yaml("conf/base/user/accounts.conf"),
581 %{ load_yaml("conf/base/user.conf") }
582 },
583 %{ load_yaml("conf/base.conf") }
584 }
585  
586 However, the actual merge will be a deep merge.
587  
588 The usual semantics related to import, export, and merging apply to these files
589 as they do in L</load_yaml>.
590  
591 Symlinks can be used to share the data between multiple keys.
592 By default, symlinks will be followed, but will cause an error if any of them
593 are outside the root path of the bundle.
594 If a symlink is permitted, this will follow symlinks to files or directories as
595 if they were the files or directories set locally, allowing the original key to
596 be renamed in any way desired.
597 It also means that symlinks to files must have a correct configuraiton file
598 suffix.
599  
600 This will ignore any file starting with a period.
601  
602 There are some options to modify the default behaviors:
603  
604 =over
605  
606 =item C<follow_symlinks_when>
607  
608 This may be set to any of the following strings:
609  
610 =over
611  
612 =item C<bundled>
613  
614 This is the default. Symlinks are followed, but only within the root.
615  
616 =item C<never>
617  
618 Do not follow symlinks.
619  
620 =item C<always>
621  
622 Always follow symlinks. Use this with caution.
623  
624 =back
625  
626 =item C<follow_symlinks_fail>
627  
628 This may be set to any of the following strings:
629  
630 =over
631  
632 =item C<error>
633  
634 This is the default. When symlinks are found, but not followed, croak.
635  
636 =item C<warn>
637  
638 When symlinks are found, but not followed, carp.
639  
640 =item C<ignore>
641  
642 When symlinks are found, but not followed... take no action at all.
643  
644 =back
645  
646 =item C<conf_suffixes>
647  
648 This routine only considers files that have the given suffxes. The default includes "conf" and "yml". The suffixes are given as an array reference of strings. (All directories will be followed, at least to the maximum depth.)
649  
650 =item C<max_depth>
651  
652 This defaults to 20 which is probably more than enough and mostly intended to prevent some sort of insane failure. If a directory is found at one further than the maximum depth, a warning will be issued.
653  
654 =back
655  
656 =head2 Observe
657  
658 =head3 add_yaml_observer
659  
660 add_yaml_observer(sub {
661 my $filename = shift;
662 warn "Yaml file $filename was just loaded.";
663 });
664  
665 Adds an observer sub that will be notified just prior to a yaml
666 file being loaded. Note that each observer is called even if
667 the yaml file is cached and does not need to be reloaded.
668  
669 =head3 _notify_yaml_observers
670  
671 Called internally to notify each waiting observer
672 that a new yaml file is being loaded.
673  
674 =head3 remove_yaml_observer
675  
676 remove_yaml_observer($subref);
677  
678 Removes an observer sub that was previously added via add_yaml_observer.
679  
680 =head2 Cache
681  
682 =head3 $YAML::LoadBundle::CacheDir
683  
684 Set this to a path that already exists of where to cache loaded files.
685  
686 $YAML::LoadBundle::CacheDir
687 = File::Spec->catdir( File::Spec->tmpdir, 'yaml-loadbundle' );
688  
689 Defaults to C<$ENV{YAML_LOADBUNDLE_CACHEDIR}>.
690  
691 If false, caching is disabled.
692  
693 =head1 AUTHOR
694  
695 Grant Street Group <developers@grantstreet.com>
696  
697 =head1 COPYRIGHT AND LICENSE
698  
699 This software is copyright (c) 2007-2016 by Grant Street Group.
700  
701 This is free software; you can redistribute it and/or modify it under
702 the same terms as the Perl 5 programming language system itself.
703  
704 =cut
↑ Summary