| 1 | #=============================================== | ||||||||||||||||||||||||||||||||||||||||||
| 2 | package Banal::Utils::Data; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | no warnings qw(uninitialized); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT_OK = qw( banal_get_data | |||||||||||||||||||||||||||||||||||||||||
| 14 | flatten_complex_data_to_list | ||||||||||||||||||||||||||||||||||||||||||
| 15 | flatten_complex_data_to_list_with_options | ||||||||||||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Banal::Utils::String qw(trim); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Banal::Utils::Array qw(array1_starts_with_array2); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | ##############################################################################" | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # PUBLIC (exportable) FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ##############################################################################" | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | #---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # Function, not a method! | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub banal_get_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
banal_get_data: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $args = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my $opts = $args->{options} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'options'} || {}
MC/DC0%0/1
$args->{'options'} || {}
$args->{'options'}
Truth table0%0/2
A
A= $args->{'options'}
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my $search_upwards_while_not_defined = $opts->{search_upwards_while_not_defined}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # This is where the MAGIC happens. For a full list of options, see the function "normalize_data_root_and_keys()". | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my ($root, @keys) = _normalize_data_root_and_keys (@_); | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # The data root should have been defined by now. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | return unless ($root); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # All this for nothing? | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return $root if (scalar(@keys) < 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # The reason for the below loop is to allow outer level 'variables' to be used when the variable is not defined at the proper (inner) level. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # Very handy for CONFIGURATION handling scenarios. | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $key = pop @keys; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | while (scalar(@keys) >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $value= _banal_basic_get_data_via_key_list(data=>$root, keys=>[@keys, $key]); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return $value if defined($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # Continue searching upwards if we are allowed to do so. Return otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return unless $search_upwards_while_not_defined; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | pop @keys; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | #----------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # FUNCTION (not a method). | ||||||||||||||||||||||||||||||||||||||||||
| 68 | #----------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub flatten_complex_data_to_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flatten_complex_data_to_list: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | return flatten_complex_data_to_list_with_options (data=>[@_], on_ArrayRef=>'flatten', on_HashRef=>'flatten', on_ScalarRef=>'flatten'); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | #----------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # FUNCTION (not a method). | ||||||||||||||||||||||||||||||||||||||||||
| 76 | #----------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub flatten_complex_data_to_list_with_options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flatten_complex_data_to_list_with_options: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $opts = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my $data = $opts->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $on_ArrayRef = $opts->{on_ArrayRef} || 'flatten'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'on_ArrayRef'} || 'flatten'
MC/DC0%0/1
$opts->{'on_ArrayRef'} || 'flatten'
$opts->{'on_ArrayRef'}
Truth table0%0/2
A
A= $opts->{'on_ArrayRef'}
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $on_HashRef = $opts->{on_HashRef} || 'flatten'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'on_HashRef'} || 'flatten'
MC/DC0%0/1
$opts->{'on_HashRef'} || 'flatten'
$opts->{'on_HashRef'}
Truth table0%0/2
A
A= $opts->{'on_HashRef'}
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $on_ScalarRef = $opts->{on_ScalarRef} || 'flatten'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'on_ScalarRef'} || 'flatten'
MC/DC0%0/1
$opts->{'on_ScalarRef'} || 'flatten'
$opts->{'on_ScalarRef'}
Truth table0%0/2
A
A= $opts->{'on_ScalarRef'}
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my @list = (); | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | foreach my $datum (@$data) { | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ((reftype($datum) eq 'ARRAY') && ($on_ArrayRef =~ /^flatten|dereference$/io)){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
reftype($datum) eq "ARRAY" and $on_ArrayRef =~ /^flatten|dereference$/io
MC/DC0%0/2
reftype($datum) eq "ARRAY" and $on_ArrayRef =~ /^flatten|dereference$/io
reftype($datum) eq "ARRAY"
$on_ArrayRef =~ /^flatten|dereference$/io
Truth table0%0/3
A and B
A= reftype($datum) eq "ARRAY"B= $on_ArrayRef =~ /^flatten|dereference$/io
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | push @list, flatten_complex_data_to_list_with_options(data=>$datum); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 89 | ▶ | }elsif ((reftype($datum) eq 'HASH') && ($on_HashRef =~ /^flatten|dereference$/io)){ | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
reftype($datum) eq "HASH" and $on_HashRef =~ /^flatten|dereference$/io
MC/DC0%0/2
reftype($datum) eq "HASH" and $on_HashRef =~ /^flatten|dereference$/io
reftype($datum) eq "HASH"
$on_HashRef =~ /^flatten|dereference$/io
Truth table0%0/3
A and B
A= reftype($datum) eq "HASH"B= $on_HashRef =~ /^flatten|dereference$/io
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | push @list, flatten_complex_data_to_list_with_options(data=>[%$datum]); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 92 | ▶ | }elsif ((reftype($datum) eq 'SCALAR') && ($on_ScalarRef =~ /^flatten|dereference$/io)){ | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
reftype($datum) eq "SCALAR" and $on_ScalarRef =~ /^flatten|dereference$/io
MC/DC0%0/2
reftype($datum) eq "SCALAR" and $on_ScalarRef =~ /^flatten|dereference$/io
reftype($datum) eq "SCALAR"
$on_ScalarRef =~ /^flatten|dereference$/io
Truth table0%0/3
A and B
A= reftype($datum) eq "SCALAR"B= $on_ScalarRef =~ /^flatten|dereference$/io
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | push @list, flatten_complex_data_to_list_with_options(data=>[$$datum]);; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | push @list, $datum; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return @list; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | #******************************************************************* | ||||||||||||||||||||||||||||||||||||||||||
| 107 | # PRIVATE (non-exported) FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 108 | #******************************************************************* | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | #---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub _is_absolute_data_key_reference { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_absolute_data_key_reference: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return ((scalar(@_) > 0) && !$_[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
scalar @_ > 0 && !$_[0]
MC/DC0%0/2
scalar @_ > 0 && !$_[0]
scalar @_ > 0
!$_[0]
Truth table0%0/3
A && not(B)
A= scalar @_ > 0B= !$_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | #---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub _normalize_data_root_and_keys { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_data_root_and_keys: not called CC=35 SCAR=70.7
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $args = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $keys = $args->{keys} || $args->{key} || $args->{path} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/8
$args->{'keys'} || $args->{'key'} || $args->{'path'} || []
$args->{"keys"} or $args->{"key"} or $args->{"path"}
$args->{"keys"} or $args->{"key"}
MC/DC0%0/3
$args->{'keys'} || $args->{'key'} || $args->{'path'} || []
$args->{"keys"}
$args->{"key"}
$args->{"path"}
Truth table0%0/4
A or B or C
A= $args->{"keys"}B= $args->{"key"}C= $args->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $data = $args->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my $context = $args->{context} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'context'} || []
MC/DC0%0/1
$args->{'context'} || []
$args->{'context'}
Truth table0%0/2
A
A= $args->{'context'}
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $opts = $args->{options} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'options'} || {}
MC/DC0%0/1
$args->{'options'} || {}
$args->{'options'}
Truth table0%0/2
A
A= $args->{'options'}
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $separator = $opts->{path_separator} || $opts->{separator} || '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$opts->{'path_separator'} || $opts->{'separator'} || '/'
$opts->{"path_separator"} or $opts->{"separator"}
MC/DC0%0/2
$opts->{'path_separator'} || $opts->{'separator'} || '/'
$opts->{"path_separator"}
$opts->{"separator"}
Truth table0%0/3
A or B
A= $opts->{"path_separator"}B= $opts->{"separator"}
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my $remove_extra_separators = defined($opts->{remove_extra_separators}) ? $opts->{remove_extra_separators} : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my $remove_leading_separator = defined($opts->{remove_leading_separator}) ? $opts->{remove_leading_separator} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $remove_trailing_separator = defined($opts->{remove_trailing_separator}) ? $opts->{remove_trailing_separator} : $remove_extra_separators; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my $remove_empty_segments = defined($opts->{remove_empty_segments}) ? $opts->{remove_empty_segments} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my $try_avoiding_repeated_segments = defined($opts->{try_avoiding_repeated_segments}) ? $opts->{try_avoiding_repeated_segments} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my $lc = $opts->{lower_case} || $opts->{lc} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$opts->{'lower_case'} || $opts->{'lc'} || 0
$opts->{"lower_case"} or $opts->{"lc"}
MC/DC0%0/2
$opts->{'lower_case'} || $opts->{'lc'} || 0
$opts->{"lower_case"}
$opts->{"lc"}
Truth table0%0/3
A or B
A= $opts->{"lower_case"}B= $opts->{"lc"}
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $trim = $opts->{trim} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'trim'} || 0
MC/DC0%0/1
$opts->{'trim'} || 0
$opts->{'trim'}
Truth table0%0/2
A
A= $opts->{'trim'}
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $mroot = undef; # Yeah, 'undef' by default. | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $relevant_keys = []; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my @accumulated_segs = (); | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $use_path_semantics; | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | no warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | $use_path_semantics = (defined($opts->{path}) && (($keys eq $opts->{path}) || ($keys == $opts->{path}))) ? 1 : $opts->{use_path_semantics}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $opts->{"path"} and $keys eq $opts->{"path"} || $keys == $opts->{"path"}
$keys eq $opts->{"path"} or $keys == $opts->{"path"}
MC/DC0%0/3
defined $opts->{"path"} and $keys eq $opts->{"path"} || $keys == $opts->{"path"}
defined $opts->{"path"}
$keys eq $opts->{"path"}
$keys == $opts->{"path"}
Truth table0%0/4
A and B or C
A= defined $opts->{"path"}B= $keys eq $opts->{"path"}C= $keys == $opts->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # Flatten all context and key segments (which are potentially a mix of path segment strings) | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | $keys = flatten_complex_data_to_list_with_options(data=>[$data, $context, $keys], on_HashRef=>'keep'); | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | # If we've got a HASH reference given as a key (or context) segment, that's our root. Otherwise, build the relevant thingy (relative to the root). | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | foreach my $key (reverse @$keys) { | |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if (reftype($key) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | $mroot = $key; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 149 | }else { | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | unshift @$relevant_keys, $key; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | # Flatten all context and key segments (which are potentially a mix of path segment strings) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | while ( scalar(@$relevant_keys)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my $key = pop @$relevant_keys; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my @segs = flatten_complex_data_to_list_with_options(data=>[$key], on_HashRef=>'keep'); | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # If it's an empty ARRAY, just ignore, and pass on to the next one. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | next unless (scalar(@segs)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | # Do we have much to do, anyway? | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if ($use_path_semantics) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $path = join($separator, @segs); | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | $path =~ s/${separator}+/${separator}/ if ($remove_extra_separators); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | $path =~ s/^${separator}// if ($remove_leading_separator); # If you ask for this, you won't be able to detect absolute paths. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $path =~ s/${separator}$// if ($remove_trailing_separator); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | @segs = split /$separator/, $path; | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # Lowercase and trim if required. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | @segs = [map {lc($_)} @segs] if ($lc); # Segments are all automatically lowercased, if asked for it. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | @segs = [map {trim($_)} @segs] if ($trim); # Segments are all automatically trimmed, if asked for. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my @prepend_segs = @segs; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if($try_avoiding_repeated_segments) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | @prepend_segs = (); | |||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | while (scalar(@segs)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if (array1_starts_with_array2([@accumulated_segs], [@segs])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | my $s = shift @segs; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | push @prepend_segs, $s; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | @accumulated_segs = (@prepend_segs, @accumulated_segs); | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | last if (_is_absolute_data_key_reference(@accumulated_segs)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | # Remove empty segments if we are asked for it. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # If you ask for this, you won't be able to detect absolute paths later on (normally, we have already done the detection for you, though) | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | @accumulated_segs = grep (/^\s*$/i, @accumulated_segs) if ($remove_empty_segments); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | # Here's a little something: We insert the root to the begining of the array. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | unshift @accumulated_segs, $mroot; | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | return wantarray ? @accumulated_segs : [@accumulated_segs]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | #---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # Function, not a method! | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # Allows to get a data element within a deep structure composed of possibly complex data types (HASH, ARRAY, ...) | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # Example: | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # _banal_basic_get_data_via_key_list (data=>$h, keys=>["employee[23]", "department", "name"]) | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # In this example, we are assuming that the initial data ($h) is a HASH that has a key called 'employee' which refers to an ARRAY of hashes, .... | ||||||||||||||||||||||||||||||||||||||||||
| 213 | #---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | sub _banal_basic_get_data_via_key_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_banal_basic_get_data_via_key_list: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | my $args = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | my $data = $args->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my $keys = $args->{keys}; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my @segments = @$keys; | |||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | foreach my $segment (@segments) { | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | next unless $segment; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my $element = $segment; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $index; | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | if($element =~ /^([^\[]*)\[(\d+)\]$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | $element = $1; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | $index = $2; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | $index = undef; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | # We're on a SCALAR. Fishy, since we have got a key segment, too. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | unless(reftype($data)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # We're on a SCALAR Reference. Fishy, since we have got a key segment, too. | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | if(reftype($data) eq "SCALAR") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | # We're on an ARRAY. | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | if(reftype($data) eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if (defined ($index) && !defined($element)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $index and not defined $element
MC/DC0%0/2
defined $index and not defined $element
defined $index
not defined $element
Truth table0%0/3
A and not(B)
A= defined $indexB= not defined $element
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | if(exists $data->[$index]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | $data = $data->[$index]; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | croak "No element with index $index!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | }elsif (!defined($element)) { | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | return $data | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | # We're on a HASH. | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | if(reftype($data) eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | # the entire segment (even if it matches the array indexing pattern!) | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if (exists $data->{$segment}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $data = $data->{$segment}; # this way, we are able to retreive weird hash values with keys that actually match our array indexing. | |||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | # Now, we are on the normal route. | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | if (! exists $data->{$element}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | if(reftype($data->{$element}) eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | if(! defined($index) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | #croak "$element is an array but you didn't specify an index to access it!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | $data = $data->{$element}; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | if(exists $data->{$element}->[$index]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | $data = $data->{$element}->[$index]; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | croak "$element doesn't have an element with index $index!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | $data = $data->{$element}; | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | __END__ | ||||||||||||||||||||||||||||||||||||||||||