| 1 | package Datafile::Hash; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | our @EXPORT_OK = qw(readhash writehash); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | our $VERSION = '1.08'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 42 | ▶ | sub _trim { $_[0] =~ s/^\s+|\s+$//gr if defined $_[0] && $_[1] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_trim: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and $_[1]
MC/DC0%0/2
defined $_[0] and $_[1]
defined $_[0]
$_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 12 | ▶ | sub readhash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
readhash: called CC=48 SCAR=38.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 12 | my ( $filename, $data, $opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 12 | ▶ | my $delim = $opts->{delimiter} // '='; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"delimiter"} // "="
MC/DC100%1/1
$opts->{"delimiter"} // "="
$opts->{"delimiter"}
Truth table100%2/2
A
A= $opts->{"delimiter"}
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 12 | ▶ | my $skip_empty = $opts->{skip_empty} // 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"skip_empty"} // 1
MC/DC100%1/1
$opts->{"skip_empty"} // 1
$opts->{"skip_empty"}
Truth table100%2/2
A
A= $opts->{"skip_empty"}
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 12 | ▶ | my $skip_headers = $opts->{skip_headers} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"skip_headers"} // 0
MC/DC100%1/1
$opts->{"skip_headers"} // 0
$opts->{"skip_headers"}
Truth table100%2/2
A
A= $opts->{"skip_headers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 12 | ▶ | my $key_fields = $opts->{key_fields} // 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"key_fields"} // 1
MC/DC100%1/1
$opts->{"key_fields"} // 1
$opts->{"key_fields"}
Truth table100%2/2
A
A= $opts->{"key_fields"}
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | ▶ | my $comment_char = $opts->{comment_char} // '#'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"comment_char"} // "#"
MC/DC100%1/1
$opts->{"comment_char"} // "#"
$opts->{"comment_char"}
Truth table100%2/2
A
A= $opts->{"comment_char"}
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 12 | my $search = $opts->{search}; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 12 | ▶ | my $verbose = $opts->{verbose} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"verbose"} // 0
MC/DC100%1/1
$opts->{"verbose"} // 0
$opts->{"verbose"}
Truth table100%2/2
A
A= $opts->{"verbose"}
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 12 | ▶ | my $group_mode = $opts->{group} // 2; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"group"} // 2
MC/DC100%1/1
$opts->{"group"} // 2
$opts->{"group"}
Truth table100%2/2
A
A= $opts->{"group"}
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 12 | my $ini_mode = 0; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 12 | ▶ | $ini_mode = 1 if $delim eq '='; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 12 | ▶ | $ini_mode = 1 if $delim eq ':'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 12 | my @messages = (); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 12 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | push @messages, "# readhash: delimiter='$delim', key_fields=$key_fields\n"; | |||||||||||||||||||||||||||||||||||||||||
| 33 | push @messages, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | "# opts: " . join( ", ", map { "$_=$opts->{$_}" } sort keys %$opts ) . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 12 | my @compiled_patterns = (); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 12 | ▶ | if ( defined $search && ( ref $search || length $search ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $search and ref $search || length $search
ref $search or length $search
MC/DC33%1/3
defined $search and ref $search || length $search
defined $search
ref $search
length $search
Truth table75%3/4
A and B or C
A= defined $searchB= ref $searchC= length $search
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | my @raw = ref $search eq 'ARRAY' ? @$search : ($search); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | for my $pat (@raw) { | |||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | ▶ | next unless defined $pat; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | my $regex = ref $pat eq 'Regexp' ? $pat : qr/\Q$pat\E/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | push @compiled_patterns, $regex; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 12 | ▶ | return (-1, [ "datafile::hash::readhash: second argument (\$data) must be a HASH reference"]) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 10 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 10 | ▶ | unless (open($fh, '<:encoding(UTF-8)', $filename)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | return ( 0, ["WARNING: cannot open '$filename': $!"] ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 8 | %$data = (); | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 8 | my $entry_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | my $current_path = []; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 8 | my %structured = (); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 8 | my %groups_seen = (); | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | ▶ | while ( my $line = <$fh> ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 60 | ▶ | next if $line =~ /^\s*\Q$comment_char\E/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 51 | ▶ | if ( $skip_headers > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | $skip_headers--; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 48 | $line =~ s/[\r\n\s]+$//; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 48 | ▶ | next if $skip_empty && $line eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$skip_empty and $line eq ""
MC/DC100%2/2
$skip_empty and $line eq ""
$skip_empty
$line eq ""
Truth table100%3/3
A and B
A= $skip_emptyB= $line eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 44 | ▶ | if (@compiled_patterns) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | my $all_matched = 1; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | for my $regex (@compiled_patterns) { | |||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | $all_matched = 0 unless $line =~ $regex; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | last unless $all_matched; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | next unless $all_matched; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 36 | ▶ | if ( $line =~ /^\[\s*(.+?)\s*\]$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 8 | my $section = _trim( $1, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 8 | @$current_path = split /\./, $section; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 8 | my $path = ''; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 8 | for my $part (@$current_path) { | |||||||||||||||||||||||||||||||||||||||||
| 84 | 9 | ▶ | $path = $path ? "$path.$part" : $part; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 9 | $groups_seen{$path}++; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | ▶ | push @messages, "- entering section [$section]\n" if $verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 8 | next; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 28 | ▶ | unless ($ini_mode) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | my @fields = split /\Q${delim}\E/, $line, -1; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | ▶ | next if @fields < $key_fields + 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $key = join $delim, @fields[ 0 .. $key_fields - 1 ]; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $value = join $delim, @fields[ $key_fields .. $#fields ]; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $data->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 26 | ▶ | if ( $key_fields > 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | push @messages, | |||||||||||||||||||||||||||||||||||||||||
| 102 | "- warning: ignoring key_fields = $key_fields in INI mode\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | $key_fields = 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 26 | my @fields = split /\Q$delim\E/, $line, 2; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 26 | ▶ | next if @fields < 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 17 | my $key = join $delim, map { _trim( $_, 1 ) } @fields[ 0 .. $key_fields - 1 ]; | |||||||||||||||||||||||||||||||||||||||||
| 108 | my $value = join $delim, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 17 | map { _trim( $_, 1 ) } @fields[ $key_fields .. $#fields ]; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 17 | ▶ | if ( $delim eq '=' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 17 | $value =~ s/^"(.*)"$/$1/; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 17 | $value =~ s/\\"/"/g; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 17 | my $ref = \%structured; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 17 | ▶ | $ref = $data if ( $group_mode == 2 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 17 | ▶ | $ref = ( $ref->{$_} //= {} ) for @$current_path; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ref->{$_} //= {}
MC/DC100%1/1
$ref->{$_} //= {}
$ref->{$_}
Truth table100%2/2
A
A= $ref->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 17 | $ref->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 17 | $entry_count++; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 8 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 8 | ▶ | if ( $ini_mode && $group_mode != 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ini_mode and $group_mode != 2
MC/DC100%2/2
$ini_mode and $group_mode != 2
$ini_mode
$group_mode != 2
Truth table100%3/3
A and B
A= $ini_modeB= $group_mode != 2
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | my $flatten; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | ▶ | $flatten = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.8
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | my ( $hash, $prefix ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | for my $k ( sort keys %$hash ) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 5 | my $v = $hash->{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 5 | ▶ | my $full = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$group_mode == 1 and $prefix ne ""
MC/DC0%0/2
$group_mode == 1 and $prefix ne ""
$group_mode == 1
$prefix ne ""
Truth table66%2/3
A and B
A= $group_mode == 1B= $prefix ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | ( $group_mode == 1 && $prefix ne '' ) ? "$prefix.$k" : $k; | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 5 | ▶ | if ( ref $v eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $flatten->( $v, $full ); | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 5 | ▶ | $data->{ $group_mode == 0 ? $k : $full } = $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 3 | $flatten->( \%structured, '' ); | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 8 | ▶ | if ( $verbose && $ini_mode ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$verbose and $ini_mode
MC/DC100%2/2
$verbose and $ini_mode
$verbose
$ini_mode
Truth table100%3/3
A and B
A= $verboseB= $ini_mode
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | push @messages, "- groups: " . join( ',', sort keys %groups_seen ) . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 8 | ▶ | push @messages, "- $entry_count data records read from $filename\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | if $verbose; | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 8 | return ( $entry_count, \@messages, \%groups_seen ); | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 13 | ▶ | sub writehash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
writehash: called CC=36 SCAR=36.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 13 | my ( $filename, $hash, $opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 13 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 13 | ▶ | my $delim = $opts->{delimiter} // '='; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"delimiter"} // "="
MC/DC100%1/1
$opts->{"delimiter"} // "="
$opts->{"delimiter"}
Truth table100%2/2
A
A= $opts->{"delimiter"}
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 13 | ▶ | my $comment_char = $opts->{comment_char} // '#'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"comment_char"} // "#"
MC/DC100%1/1
$opts->{"comment_char"} // "#"
$opts->{"comment_char"}
Truth table100%2/2
A
A= $opts->{"comment_char"}
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 13 | ▶ | my $prot = $opts->{prot} // 0660; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"prot"} // 432
MC/DC100%1/1
$opts->{"prot"} // 432
$opts->{"prot"}
Truth table100%2/2
A
A= $opts->{"prot"}
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 13 | ▶ | my $backup = $opts->{backup} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"backup"} // 0
MC/DC100%1/1
$opts->{"backup"} // 0
$opts->{"backup"}
Truth table100%2/2
A
A= $opts->{"backup"}
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 13 | ▶ | my $verbose = $opts->{verbose} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"verbose"} // 0
MC/DC100%1/1
$opts->{"verbose"} // 0
$opts->{"verbose"}
Truth table100%2/2
A
A= $opts->{"verbose"}
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 13 | my $ini_mode = 0; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 13 | ▶ | $ini_mode = 1 if $delim eq '='; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 13 | ▶ | $ini_mode = 1 if $delim eq ':'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 13 | ▶ | unless ( $hash && ref $hash eq 'HASH' && keys %$hash ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$hash and ref $hash eq "HASH" and keys %$hash
$hash and ref $hash eq "HASH"
MC/DC33%1/3
$hash and ref $hash eq "HASH" and keys %$hash
$hash
ref $hash eq "HASH"
keys %$hash
Truth table50%2/4
A and B and C
A= $hashB= ref $hash eq "HASH"C= keys %$hash
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | ▶ | unlink($filename) if -f $filename; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | return(0, ["datafile::hash::writehash: second argument (\$data) must be a HASH reference"] ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 10 | my $tmp = "$filename.tmp"; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 10 | my @messages = (); | |||||||||||||||||||||||||||||||||||||||||
| 172 | 10 | ▶ | if ($verbose) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 5 | push @messages, "# writehash: delimiter='$delim'\n"; | |||||||||||||||||||||||||||||||||||||||||
| 174 | push @messages, | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 5 | "# opts: " . join( ", ", map { "$_=$opts->{$_}" } sort keys %$opts ) . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 10 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 10 | ▶ | unless (open( $fh, '>:encoding(UTF-8):crlf', $tmp)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | return ( 0, ["ERROR: cannot open '$tmp' for writing: $!"] ); | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 9 | ▶ | if ( my $comment = $opts->{comment} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 3 | ▶ | my @lines = ref $comment eq 'ARRAY' ? @$comment : split /\n/, $comment; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 3 | print $fh "$comment_char $_\n" for @lines; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 3 | ▶ | if ($verbose) { push @messages, "> $comment_char $_\n" for @lines; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 9 | my $entry_count = 0; | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 9 | ▶ | unless ($ini_mode) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | for my $k ( keys %$hash ) { | |||||||||||||||||||||||||||||||||||||||||
| 193 | 6 | my $v = $hash->{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 6 | print $fh "$k$delim$v\n"; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 6 | $entry_count++; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 7 | my $first_section = 1; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 7 | my %data = %$hash; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 7 | my $has_real_nested = grep { ref $data{$_} eq 'HASH' } keys %data; | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 7 | ▶ | if ( !$has_real_nested && grep /\./, keys %data ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $has_real_nested and grep /\./u, keys %data
MC/DC0%0/2
not $has_real_nested and grep /\./u, keys %data
not $has_real_nested
grep /\./u, keys %data
Truth table66%2/3
not(A) and B
A= not $has_real_nestedB= grep /\./u, keys %data
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | my %nested; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | while ( my ( $k, $v ) = each %data ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | my @p = split /\./, $k; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | my $r = \%nested; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | $r = ( $r->{$_} //= {} ) for @p[ 0 .. $#p - 1 ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$r->{$_} //= {}
MC/DC0%0/1
$r->{$_} //= {}
$r->{$_}
Truth table0%0/2
A
A= $r->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | $r->{ $p[-1] } = $v; | |||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | %data = %nested; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | $has_real_nested = 1; | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 7 | my %global_data; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 7 | for my $k ( keys %data ) { | |||||||||||||||||||||||||||||||||||||||||
| 217 | 20 | ▶ | next if ref $data{$k} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 9 | $global_data{$k} = delete $data{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 7 | my $write_section; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 15 | ▶ | $write_section = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 15 | my ( $cur, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 15 | ▶ | my $name = @$path ? join( '.', @$path ) : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 15 | ▶ | print $fh "\n" unless ($first_section); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 15 | $first_section = 0; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 15 | ▶ | if ( $name ne '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 12 | print $fh "[$name]\n"; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 12 | ▶ | push @messages, "> [$name]\n" if $verbose; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 15 | my $maxsize = 0; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 15 | for my $k ( sort grep { !ref $cur->{$_} } keys %$cur ) { | |||||||||||||||||||||||||||||||||||||||||
| 234 | 35 | ▶ | $maxsize = length($k) if length($k) > $maxsize; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 15 | for my $k ( sort grep { !ref $cur->{$_} } keys %$cur ) { | |||||||||||||||||||||||||||||||||||||||||
| 237 | 35 | my $v = $cur->{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 35 | ▶ | my $needs_quoting = | ||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$v =~ /[#"'\r\n]/u || $v =~ /^\s+|\s+$/u || $v eq ''
$v =~ /[#"'\r\n]/u or $v =~ /^\s+|\s+$/u
MC/DC66%2/3
$v =~ /[#"'\r\n]/u || $v =~ /^\s+|\s+$/u || $v eq ''
$v =~ /[#"'\r\n]/u
$v =~ /^\s+|\s+$/u
$v eq ''
Truth table75%3/4
A or B || C
A= $v =~ /[#"'\r\n]/uB= $v =~ /^\s+|\s+$/uC= $v eq ''
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | ( $v =~ /[#"'\r\n]/ || $v =~ /^\s+|\s+$/ || $v eq '' ); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 35 | ▶ | if ( $delim eq '=' && $needs_quoting ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$delim eq "=" and $needs_quoting
MC/DC100%2/2
$delim eq "=" and $needs_quoting
$delim eq "="
$needs_quoting
Truth table100%3/3
A and B
A= $delim eq "="B= $needs_quoting
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 5 | $v =~ s/"/\\"/g; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 5 | $v = qq("$v"); | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 35 | my $line = sprintf "%-*s %s %s", $maxsize, $k, $delim, $v; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 35 | print $fh "$line\n"; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 35 | $entry_count++; | |||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 15 | for my $sub ( sort grep { ref $cur->{$_} eq 'HASH' } keys %$cur ) { | |||||||||||||||||||||||||||||||||||||||||
| 250 | 1 | $write_section->( $cur->{$sub}, [ @$path, $sub ] ); | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 7 | ▶ | if ( keys %global_data ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 3 | $write_section->( \%global_data, [] ); | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 7 | for my $top ( sort keys %data ) { | |||||||||||||||||||||||||||||||||||||||||
| 258 | 11 | $write_section->( $data{$top}, [$top] ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 9 | ▶ | print $fh "#EOF\n" if $comment_char eq '#'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 9 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 9 | ▶ | if ( $backup && -f $filename ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$backup and -f $filename
MC/DC100%2/2
$backup and -f $filename
$backup
-f $filename
Truth table100%3/3
A and B
A= $backupB= -f $filename
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | rename( $filename, $filename . '.bak' ); | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 9 | ▶ | rename( $tmp, $filename ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | or return ( 0, ["ERROR: failed to rename '$tmp' to '$filename': $!"] ); | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 9 | chmod $prot, $filename; | |||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 9 | ▶ | push @messages, "- $entry_count entries written to $filename\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | if $verbose; | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 9 | return ( $entry_count, \@messages ); | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | __END__ | ||||||||||||||||||||||||||||||||||||||||||