| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.10.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Config::FromHash; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.0800'; # VERSION | |||||||||||||||||||||||||||
| 8 | # ABSTRACT: Read config files containing hashes | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use File::Basename(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Hash::Merge(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=20 SCAR=35.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 2 | my($class, %args) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | $args{'data'} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"data"} ||= {}
MC/DC0%0/1
$args{"data"} ||= {}
$args{"data"}
Truth table50%1/2
A
A= $args{"data"}
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | $args{'sep'} ||= qr{/}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"sep"} ||= qr"/"
MC/DC0%0/1
$args{"sep"} ||= qr"/"
$args{"sep"}
Truth table50%1/2
A
A= $args{"sep"}
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | $args{'require_all_files'} ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"require_all_files"} ||= 0
MC/DC0%0/1
$args{"require_all_files"} ||= 0
$args{"require_all_files"}
Truth table50%1/2
A
A= $args{"require_all_files"}
| |||||||||||||||||||||||||||||
| 21 | 2 | $args{'config_files'} = []; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | if(exists $args{'filename'} && exists $args{'filenames'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args{"filename"} and exists $args{"filenames"}
MC/DC0%0/2
exists $args{"filename"} and exists $args{"filenames"}
exists $args{"filename"}
exists $args{"filenames"}
Truth table66%2/3
A and B
A= exists $args{"filename"}B= exists $args{"filenames"}
| |||||||||||||||||||||||||||||
| 24 | 0 | die "Don't use both 'filename' and 'filenames'."; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | if(exists $args{'environment'} && exists $args{'environments'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"environment"} and exists $args{"environments"}
MC/DC0%0/2
exists $args{"environment"} and exists $args{"environments"}
exists $args{"environment"}
exists $args{"environments"}
Truth table33%1/3
A and B
A= exists $args{"environment"}B= exists $args{"environments"}
| |||||||||||||||||||||||||||||
| 27 | 0 | die "Don't use both 'environment' and 'environments'."; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | $args{'filenames'} = $args{'filename'} if exists $args{'filename'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | $args{'environments'} = $args{'environment'} if exists $args{'environment'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | if(exists $args{'filenames'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | if(ref $args{'filenames'} ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $args{'filenames'} = [ $args{'filenames'} ]; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||
| 40 | 1 | $args{'filenames'} = []; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | if(exists $args{'environments'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if(ref $args{'environments'} ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | $args{'environments'} = [ $args{'environments'} ]; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||
| 49 | 2 | $args{'environments'} = [ undef ]; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | my $self = bless \%args => $class; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | Hash::Merge::set_behavior('LEFT_PRECEDENT'); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | if(scalar @{ $args{'filenames'} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | foreach my $environment (reverse @{ $args{'environments'} }) { | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | FILE: | ||||||||||||||||||||||||||||
| 61 | 1 | foreach my $config_file (reverse @{ $args{'filenames'} }) { | |||||||||||||||||||||||||||
| 62 | 1 | my($filename, $directory, $extension) = File::Basename::fileparse($config_file, qr{\.[^.]+$}); | |||||||||||||||||||||||||||
| 63 | 1 | ▶ | my $new_filename = $directory . $filename . (defined $environment ? ".$environment" : '') . $extension; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if(!-e $new_filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | die "$new_filename does not exist" if $self->require_all_files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | next FILE; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | push @{ $args{'config_files'} } => $new_filename; | |||||||||||||||||||||||||||
| 71 | 1 | $args{'data'} = Hash::Merge::merge($self->parse($new_filename, $args{'data'})); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | return $self; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub data { | ||||||||||||||||||||||||||
|
Subroutine
data: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 0 | return shift->{'data'}; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 5 | my $path = shift; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 5 | ▶ | if(!defined $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | warn "No path defined - nothing to return"; | |||||||||||||||||||||||||||
| 91 | 0 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 5 | my @parts = split $self->{'sep'} => $path; | |||||||||||||||||||||||||||
| 95 | 5 | my $hash = $self->{'data'}; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 5 | foreach my $part (@parts) { | |||||||||||||||||||||||||||
| 98 | 8 | ▶ | if(ref $hash eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 8 | $hash = $hash->{ $part }; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 0 | die "Can't resolve path '$path' to '$part'"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 5 | return $hash; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub config_files { | ||||||||||||||||||||||||||
|
Subroutine
config_files: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 0 | return @{ $self->{'config_files'} }; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 115 | 1 | my $file = shift; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | my $contents = path($file)->slurp_utf8; | |||||||||||||||||||||||||||
| 118 | 1 | my($parsed, $error) = $self->eval($contents); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | ▶ | die "Can't parse <$file>: $error" if $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | die "<$file> doesn't contain hash" if ref $parsed ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | return $parsed; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | sub eval { | ||||||||||||||||||||||||||
|
Subroutine
eval: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | 1 | my $contents = shift; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | return (eval $contents, $@); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub require_all_files { | ||||||||||||||||||||||||||
|
Subroutine
require_all_files: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 0 | return shift->{'require_all_files'}; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | 1; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||