| 1 | package Data::Printer::Config; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use Data::Printer::Common; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | sub load_rc_file { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_rc_file: called CC=6 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | my ($filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | if (!$filename) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | $filename = _get_first_rc_file_available(); | |||||||||||||||||||||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | return unless $filename && -e $filename && !-d $filename; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$filename and -e $filename and not -d $filename
$filename and -e $filename
MC/DC0%0/3
$filename and -e $filename and not -d $filename
$filename
-e $filename
not -d $filename
Truth table25%1/4
A and B and not(C)
A= $filenameB= -e $filenameC= not -d $filename
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | if (open my $fh, '<', $filename) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # slurp the file: | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | my $rc_data; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | { local $/ = undef; $rc_data = <$fh> } | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | return _str2data($filename, $rc_data); | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | Data::Printer::Common::_warn(undef, "error opening '$filename': $!"); | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 11 | ▶ | sub _get_first_rc_file_available { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_first_rc_file_available: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 11 | ▶ | return $ENV{DATAPRINTERRC} if exists $ENV{DATAPRINTERRC}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # look for a .dataprinter file on the project home up until we reach '/' | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 10 | my $dir = _project_home(); | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 10 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 10 | ▶ | while (defined $dir) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 58 | my $file = File::Spec->catfile($dir, '.dataprinter'); | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 58 | ▶ | return $file if -f $file; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 57 | my @path = File::Spec->splitdir($dir); | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 57 | ▶ | last unless @path; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 57 | my $updir = File::Spec->catdir(@path[0..$#path-1]); | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 57 | ▶ | last if !defined $updir || $updir eq $dir; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $updir or $updir eq $dir
MC/DC50%1/2
not defined $updir or $updir eq $dir
not defined $updir
$updir eq $dir
Truth table66%2/3
not(A) or B
A= not defined $updirB= $updir eq $dir
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 49 | $dir = $updir; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # still here? look for .dataprinter on the user's HOME: | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 9 | return File::Spec->catfile( _my_home(), '.dataprinter'); | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 10 | ▶ | sub _my_cwd { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_my_cwd: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 10 | require Cwd; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 10 | my $cwd = Cwd::getcwd(); | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | # try harder if we can't access the current dir. | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 10 | ▶ | $cwd = Cwd::cwd() unless defined $cwd; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 10 | return $cwd; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | ▶ | sub _project_home { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_project_home: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | require Cwd; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 10 | my $path; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | ▶ | if ($0 eq '-e' || $0 eq '-') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$0 eq "-e" or $0 eq "-"
MC/DC50%1/2
$0 eq "-e" or $0 eq "-"
$0 eq "-e"
$0 eq "-"
Truth table66%2/3
A or B
A= $0 eq "-e"B= $0 eq "-"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | my $cwd = _my_cwd(); | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | $path = Cwd::abs_path($cwd) if defined $cwd; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 9 | my $script = $0; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 9 | ▶ | return unless -f $script; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 9 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 9 | require File::Basename; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | # we need the full path if we have chdir'd: | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 9 | ▶ | $script = File::Spec->catfile(_my_cwd(), $script) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | unless File::Spec->file_name_is_absolute($script); | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | my (undef, $maybe_path) = File::Basename::fileparse($script); | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | ▶ | $path = Cwd::abs_path($maybe_path) if defined $maybe_path; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | return $path; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # adapted from File::HomeDir && File::HomeDir::Tiny | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 13 | ▶ | sub _my_home { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_my_home: called CC=17 SCAR=46.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 13 | my ($testing) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 13 | ▶ | if ($testing) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | require File::Temp; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | require File::Spec; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | my $BASE = File::Temp::tempdir( CLEANUP => 1 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | my $home = File::Spec->catdir( $BASE, 'my_home' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | $ENV{HOME} = $home; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | mkdir($home, 0755) unless -d $home; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | return $home; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ▶ | elsif ($^O eq 'MSWin32' and "$]" < 5.016) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$^O eq "MSWin32" and "$]" < "5.016"
MC/DC0%0/2
$^O eq "MSWin32" and "$]" < "5.016"
$^O eq "MSWin32"
"$]" < "5.016"
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= "$]" < "5.016"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | return $ENV{HOME} || $ENV{USERPROFILE}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'HOME'} || $ENV{'USERPROFILE'}
MC/DC0%0/2
$ENV{'HOME'} || $ENV{'USERPROFILE'}
$ENV{'HOME'}
$ENV{'USERPROFILE'}
Truth table0%0/3
A || B
A= $ENV{'HOME'}B= $ENV{'USERPROFILE'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | elsif ($^O eq 'MacOS') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $error = _tryme(sub { require Mac::SystemDirectory; 1 }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return Mac::SystemDirectory::HomeDirectory() unless $error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # this is the most common case, for most breeds of unix, as well as | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # MSWin32 in more recent perls. | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 13 | ▶ | my $home = (<~>)[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 10 | ▶ | return $home if $home; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # desperate measures that should never be needed. | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if (exists $ENV{LOGDIR} and $ENV{LOGDIR}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $ENV{"LOGDIR"} and $ENV{"LOGDIR"}
MC/DC0%0/2
exists $ENV{"LOGDIR"} and $ENV{"LOGDIR"}
exists $ENV{"LOGDIR"}
$ENV{"LOGDIR"}
Truth table0%0/3
A and B
A= exists $ENV{"LOGDIR"}B= $ENV{"LOGDIR"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $home = $ENV{LOGDIR}; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if (not $home and exists $ENV{HOME} and $ENV{HOME}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $home and exists $ENV{"HOME"} and $ENV{"HOME"}
not $home and exists $ENV{"HOME"}
MC/DC0%0/3
not $home and exists $ENV{"HOME"} and $ENV{"HOME"}
not $home
exists $ENV{"HOME"}
$ENV{"HOME"}
Truth table0%0/4
not(A) and B and C
A= not $homeB= exists $ENV{"HOME"}C= $ENV{"HOME"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $home = $ENV{HOME}; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # Light desperation on any (Unixish) platform | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | SCOPE: { $home = (getpwuid($<))[7] if not defined $home } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (defined $home and ! -d $home ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $home and not -d $home
MC/DC0%0/2
defined $home and not -d $home
defined $home
not -d $home
Truth table0%0/3
A and not(B)
A= defined $homeB= not -d $home
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $home = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return $home; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | sub _file_mode_is_restricted { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_file_mode_is_restricted: called CC=4 SCAR=21.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | my ($filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | my $mode_raw = (stat($filename))[2]; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | return 0 unless defined $mode_raw; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $mode = sprintf('%04o', $mode_raw & 07777); | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | return (length($mode) == 4 && substr($mode, 2, 2) eq '00') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $mode == 4 and substr($mode, 2, 2) eq "00"
MC/DC0%0/2
length $mode == 4 and substr($mode, 2, 2) eq "00"
length $mode == 4
substr($mode, 2, 2) eq "00"
Truth table0%0/3
A and B
A= length $mode == 4B= substr($mode, 2, 2) eq "00"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 8 | ▶ | sub _str2data { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_str2data: called CC=21 SCAR=30.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 8 | my ($filename, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 8 | my $config = { _ => {} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 8 | my $counter = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 8 | my $filter; | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | my $can_use_filters; | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 8 | my $ns = '_'; | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | # based on Config::Tiny | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 8 | foreach ( split /(?:\015{1,2}\012|\015|\012)/, $content ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | 183 | $counter++; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 183 | ▶ | if (defined $filter) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 13 | ▶ | if ( /^end filter\s*$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 6 | ▶ | if (!defined $can_use_filters) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 6 | $can_use_filters = _file_mode_is_restricted($filename); | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 6 | ▶ | if ($can_use_filters) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | my $sub_str = 'sub { my ($obj, $ddp) = @_; ' | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | . $filter->{code_str} | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 5 | . '}' | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | ; | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 5 | push @{$config->{$ns}{filters}}, +{ $filter->{name} => eval $sub_str }; | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | Data::Printer::Common::_warn(undef, "ignored filter '$filter->{name}' from rc file '$filename': file is readable/writeable by others"); | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 6 | $filter = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | elsif ( /^begin\s+filter/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | Data::Printer::Common::_warn(undef, "error reading rc file '$filename' line $counter: found 'begin filter' inside another filter definition ($filter->{name}). Are you missing an 'end filter' on line " . ($counter - 1) . '?'); | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | return {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 6 | $filter->{code_str} .= $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | elsif ( /^\s*(?:\#|\;|$)/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | next # skip comments and empty lines | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 54 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | elsif ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # Create the sub-hash if it doesn't exist. | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # Without this, sections without keys will not | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # appear at all in the completed struct. | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 18 | ▶ | $config->{$ns = $1} ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$config{$ns = $1} ||= {}
MC/DC0%0/1
$$config{$ns = $1} ||= {}
$$config{$ns = $1}
Truth table50%1/2
A
A= $$config{$ns = $1}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | elsif ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # Handle properties: | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 90 | my ($path_str, $value) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | # turn a.b.c.d into {a}{b}{c}{d} | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 90 | my @subpath = split /\./, $path_str; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 90 | my $current = $config->{$ns}; | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # remove single/double (enclosing) quotes | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 90 | $value =~ s/\A(['"])(.*)\1\z/$2/; | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # the root "filters" key is a special case, because we want | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | # it to always be an arrayref. In other words: | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # filters = abc,def --> filters => ['abc', 'def'] | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # filters = abc --> filters => ['abc'] | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # filters = --> filters => [] | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 90 | ▶ | if (@subpath == 1 && $subpath[0] eq 'filters') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@subpath == 1 and $subpath[0] eq "filters"
MC/DC100%2/2
@subpath == 1 and $subpath[0] eq "filters"
@subpath == 1
$subpath[0] eq "filters"
Truth table100%3/3
A and B
A= @subpath == 1B= $subpath[0] eq "filters"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 18 | $value = [ split /\s*,\s*/ => $value ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 90 | ▶ | while (my $subpath = shift @subpath) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 144 | ▶ | if (@subpath > 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 54 | ▶ | $current->{$subpath} ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$current->{$subpath} ||= {}
MC/DC100%1/1
$current->{$subpath} ||= {}
$current->{$subpath}
Truth table100%2/2
A
A= $current->{$subpath}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 54 | $current = $current->{$subpath}; | |||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 90 | $current->{$subpath} = $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | elsif ( /^begin\s+filter\s+([^\s]+)\s*$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 7 | my $filter_name = $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 7 | $filter = { name => $filter_name, code_str => '' }; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | Data::Printer::Common::_warn(undef, "error reading rc file '$filename': syntax error at line $counter: $_"); | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | if ($counter == 1 && /\A\s*\{/s) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$counter == 1 and /\A\s*\{/s
MC/DC0%0/2
$counter == 1 and /\A\s*\{/s
$counter == 1
/\A\s*\{/s
Truth table33%1/3
A and B
A= $counter == 1B= /\A\s*\{/s
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | Data::Printer::Common::_warn( | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | "\nRC file format changed in 1.00. Usually all it takes is:\n" | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | . " cp $filename $filename.old && perl -MData::Printer::Config -E 'say Data::Printer::Config::convert(q($filename.old))' > $filename\n" | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | . "Please visit https://metacpan.org/pod/Data::Printer::Config for details.\n" | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | return {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # now that we have loaded the config, we must expand | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # all existing 'rc_file' and 'profile' statements and | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # merge them together. | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 6 | foreach my $ns (keys %$config) { | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | $config->{$ns} = _expand_profile($config->{$ns}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 24 | ▶ | if exists $config->{$ns}{profile}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 6 | return $config; | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 825 | ▶ | sub _merge_options { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merge_options: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 825 | my ($old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | 825 | ▶ | if (ref $new eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 211 | my %merged; | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | 211 | ▶ | my $to_merge = ref $old eq 'HASH' ? $old : {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 211 | foreach my $k (keys %$new, keys %$to_merge) { | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | # if the key exists in $new, we recurse into it: | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 618 | ▶ | if (exists $new->{$k}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 449 | $merged{$k} = _merge_options($to_merge->{$k}, $new->{$k}); | |||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # otherwise we keep the old version (recursing in case of refs) | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 169 | $merged{$k} = _merge_options(undef, $to_merge->{$k}); | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 211 | return \%merged; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | elsif (ref $new eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | # we'll only use the array on $new, but we still need to recurse | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # in case array elements contain other data structures. | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 35 | my @merged; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | 35 | foreach my $element (@$new) { | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | 56 | push @merged, _merge_options(undef, $element); | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 35 | return \@merged; | |||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 579 | return $new; | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 7 | ▶ | sub _expand_profile { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_profile: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 7 | my ($options, $ddp) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | 7 | my $profile = delete $options->{profile}; | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | 7 | ▶ | if ($profile !~ /\A[a-zA-Z0-9:]+\z/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | Data::Printer::Common::_warn($ddp,"invalid profile name '$profile'"); | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 5 | my $class = 'Data::Printer::Profile::' . $profile; | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | 5 | ▶ | my $error = Data::Printer::Common::_tryme(sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 5 | my $load_error = Data::Printer::Common::_tryme("use $class; 1;"); | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | 5 | ▶ | die $load_error if defined $load_error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 4 | my $expanded = $class->profile(); | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | 4 | ▶ | die "profile $class did not return a HASH reference" unless ref $expanded eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 4 | $options = Data::Printer::Config::_merge_options($expanded, $options); | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | 5 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | 5 | ▶ | if (defined $error) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 1 | Data::Printer::Common::_warn($ddp, "unable to load profile '$profile': $error"); | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 7 | return $options; | |||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # converts the old format to the new one | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 4 | ▶ | sub convert { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
convert: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 4 | my ($filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | 4 | ▶ | Data::Printer::Common::_die("please provide a .dataprinter file path") | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | unless $filename; | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 3 | ▶ | Data::Printer::Common::_die("file '$filename' not found") | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $filename and not -d $filename
MC/DC50%1/2
-e $filename and not -d $filename
-e $filename
not -d $filename
Truth table66%2/3
A and not(B)
A= -e $filenameB= not -d $filename
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | unless -e $filename && !-d $filename; | ||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 2 | ▶ | open my $fh, '<', $filename | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | or Data::Printer::Common::_die("error reading file '$filename': $!"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 2 | my $rc_data; | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 2 | { local $/; $rc_data = <$fh> } | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 2 | my $config = eval $rc_data; | |||||||||||||||||||||||||||||||||||||||||||||
| 293 | 2 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | Data::Printer::Common::_die("error loading file '$filename': $@"); | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 296 | ▶ | elsif (!ref $config or ref $config ne 'HASH') { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
not ref $config or ref $config ne "HASH"
MC/DC50%1/2
not ref $config or ref $config ne "HASH"
not ref $config
ref $config ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $configB= ref $config ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 1 | Data::Printer::Common::_die("error loading file '$filename': config file must return a hash reference"); | |||||||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | return _convert('', $config); | |||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 13 | ▶ | sub _convert { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_convert: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 13 | my ($key_str, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | 13 | ▶ | if (ref $value eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 5 | ▶ | $key_str = 'colors' if $key_str eq 'color'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 5 | my $str = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | 5 | foreach my $k (sort keys %$value) { | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | 12 | ▶ | $str .= _convert(($key_str ? "$key_str.$k" : $k), $value->{$k}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 5 | return $str; | |||||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 8 | ▶ | if ($key_str && $key_str eq 'filters.-external' && ref $value eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%4/6
$key_str and $key_str eq "filters.-external" and ref $value eq "ARRAY"
$key_str and $key_str eq "filters.-external"
MC/DC33%1/3
$key_str and $key_str eq "filters.-external" and ref $value eq "ARRAY"
$key_str
$key_str eq "filters.-external"
ref $value eq "ARRAY"
Truth table50%2/4
A and B and C
A= $key_strB= $key_str eq "filters.-external"C= ref $value eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 1 | return 'filters = ' . join(', ' => @$value) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | elsif (ref $value) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 2 | Data::Printer::Common::_warn( | |||||||||||||||||||||||||||||||||||||||||||||
| 319 | undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | " [*] path '$key_str': expected scalar, found " . ref($value) | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | . ". Filters must be in their own class now, loaded with 'filter'.\n" | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | . "If you absolutely must put custom filters in, use the 'begin filter'" | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | . " / 'end filter' options manually, as explained in the documentation," | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | . " making sure your .dataprinter file is not readable nor writeable to" | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | . " anyone other than your user." | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 5 | ▶ | $value = "'$value'" if $value =~ /\s/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 5 | return "$key_str = $value\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 336 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||