| 1 | 497 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::Role::NestedHashUtils; | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: role for nested hashes | ||||||||||||||||||||||||||||
| 5 | 497 | $HTML::FormFu::Role::NestedHashUtils::VERSION = '2.09'; | |||||||||||||||||||||||||||
| 6 | 497 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 497 | ▶ | use HTML::FormFu::Util qw( split_name ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 497 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3580 | ▶ | sub get_nested_hash_value { | ||||||||||||||||||||||||||
|
Subroutine
get_nested_hash_value: called CC=9 SCAR=24.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 3580 | my ( $self, $param, $name ) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3580 | my ( $root, @names ) = split_name($name); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3580 | ▶ | if ( !@names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 2925 | ▶ | return exists $param->{$root} ? $param->{$root} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 655 | my $ref = \$param->{$root}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 655 | for (@names) { | |||||||||||||||||||||||||||
| 23 | 681 | ▶ | if (/^(0|[1-9][0-9]*)\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | croak "nested param clash for ARRAY $root" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | if ref $$ref ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | return if $1 > $#{$$ref}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | $ref = \( $$ref->[$1] ); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||
| 32 | 681 | ▶ | return if ref $$ref ne 'HASH' || !exists $$ref->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $$ref ne "HASH" or not exists $$ref->{$_}
MC/DC100%2/2
ref $$ref ne "HASH" or not exists $$ref->{$_}
ref $$ref ne "HASH"
not exists $$ref->{$_}
Truth table100%3/3
A or not(B)
A= ref $$ref ne "HASH"B= not exists $$ref->{$_}
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 632 | $ref = \( $$ref->{$_} ); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 606 | return $$ref; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1726 | ▶ | sub set_nested_hash_value { | ||||||||||||||||||||||||||
|
Subroutine
set_nested_hash_value: called CC=8 SCAR=23.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 1726 | my ( $self, $param, $name, $value ) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1726 | my ( $root, @names ) = split_name($name); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1726 | ▶ | if ( !@names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1222 | return $param->{$root} = $value; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 504 | my $ref = \$param->{$root}; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 504 | for (@names) { | |||||||||||||||||||||||||||
| 53 | 541 | ▶ | if (/^(0|[1-9][0-9]*)\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $$ref = [] if !defined $$ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | croak "nested param clash for ARRAY $name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | if ref $$ref ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | $ref = \( $$ref->[$1] ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | 541 | ▶ | $$ref = {} if !defined $$ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 541 | ▶ | croak "nested param clash for HASH $name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | if ref $$ref ne 'HASH'; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 541 | $ref = \( $$ref->{$_} ); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 504 | $$ref = $value; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub delete_nested_hash_key { | ||||||||||||||||||||||||||
|
Subroutine
delete_nested_hash_key: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 0 | my ( $self, $param, $name ) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my ( $root, @names ) = split_name($name); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ( !@names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | delete $param->{$root}; | |||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | my $ref = \$param->{$root}; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | for my $i ( 0 .. $#names ) { | |||||||||||||||||||||||||||
| 87 | 0 | my $name = $names[$i]; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ( $name =~ /^(0|[1-9][0-9]*)\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return if !defined $$ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | croak "nested param clash for ARRAY $name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | if ref $$ref ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | $ref = \( $$ref->[$1] ); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( $i == $#names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | croak "can't delete hash key for an array"; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return if !defined $$ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | croak "nested param clash for HASH $name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | if ref $$ref ne 'HASH'; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ( $i == $#names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | delete $$ref->{$name}; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||
| 111 | 0 | $ref = \( $$ref->{$name} ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | return; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 4010 | ▶ | sub nested_hash_key_exists { | ||||||||||||||||||||||||||
|
Subroutine
nested_hash_key_exists: called CC=12 SCAR=28.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 4010 | my ( $self, $param, $name ) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 4010 | my ( $root, @names ) = split_name($name); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 4010 | ▶ | if ( !@names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 3029 | ▶ | return ( defined($root) && exists( $param->{$root} ) ); | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $root && exists $param->{$root}
MC/DC50%1/2
defined $root && exists $param->{$root}
defined $root
exists $param->{$root}
Truth table66%2/3
A && B
A= defined $rootB= exists $param->{$root}
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 981 | my $ref = \$param->{$root}; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 981 | for my $i ( 0 .. $#names ) { | |||||||||||||||||||||||||||
| 131 | 1009 | my $part = $names[$i]; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1009 | ▶ | if ( $part =~ /^(0|[1-9][0-9]*)\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | croak "nested param clash for ARRAY $root" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | if ref $$ref ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ( $i == $#names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | return $1 > $$ref->[$1] ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | $ref = \( $$ref->[$1] ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||
| 144 | 1009 | ▶ | if ( $i == $#names ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 981 | ▶ | return if !ref $$ref || ref($$ref) ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not ref $$ref or ref $$ref ne "HASH"
MC/DC100%2/2
not ref $$ref or ref $$ref ne "HASH"
not ref $$ref
ref $$ref ne "HASH"
Truth table100%3/3
not(A) or B
A= not ref $$refB= ref $$ref ne "HASH"
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 574 | ▶ | return exists $$ref->{$part} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 28 | $ref = \( $$ref->{$part} ); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | return; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 497 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||