| 1 | package # hide from Pause | ||||||||||||||||||||
| 2 | HTML::FormHandler::Params; | ||||||||||||||||||||
| 3 | # ABSTRACT: params handling | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 146 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 146 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 146 | has 'separator' => ( isa => 'Str', is => 'rw', default => '.' ); | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 602 | ▶ | sub split_name { | ||||||||||||||||||
|
Subroutine
split_name: called CC=3 SCAR=11.9
Pod
undocumented
| |||||||||||||||||||||
| 11 | 602 | my ( $self, $name, $sep ) = @_; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 602 | ▶ | $sep ||= $self->separator; | ||||||||||||||||||
|
Condition33%1/3
$sep ||= $self->separator
MC/DC0%0/2
$sep ||= $self->separator
$sep
$self->separator
Truth table33%1/3
A ||= B
A= $sepB= $self->separator
| |||||||||||||||||||||
| 14 | 602 | $sep = "\Q$sep"; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 602 | ▶ | if ( $sep eq '[]' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | 0 | return grep { defined } ( | |||||||||||||||||||
| 18 | $name =~ / | ||||||||||||||||||||
| 19 | ^ (\w+) # root param | ||||||||||||||||||||
| 20 | | \[ (\w+) \] # nested | ||||||||||||||||||||
| 21 | /gx | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # These next two regexes are the escaping aware equivalent | ||||||||||||||||||||
| 26 | # to the following: | ||||||||||||||||||||
| 27 | # my ($first, @segments) = split(/\./, $name, -1); | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # m// splits on unescaped '.' chars. Can't fail b/c \G on next | ||||||||||||||||||||
| 30 | # non ./ * -> escaped anything -> non ./ * | ||||||||||||||||||||
| 31 | 602 | $name =~ m/^ ( [^\\$sep]* (?: \\(?:.|$) [^\\$sep]* )* ) /gx; | |||||||||||||||||||
| 32 | 602 | my $first = $1; | |||||||||||||||||||
| 33 | 602 | $first =~ s/\\(.)/$1/g; # remove escaping | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 602 | my (@segments) = $name =~ | |||||||||||||||||||
| 36 | # . -> ( non ./ * -> escaped anything -> non ./ * ) | ||||||||||||||||||||
| 37 | m/\G (?:[$sep]) ( [^\\$sep]* (?: \\(?:.|$) [^\\$sep]* )* ) /gx; | ||||||||||||||||||||
| 38 | # Escapes removed later, can be used to avoid using as array index | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 602 | return ( $first, @segments ); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 369 | ▶ | sub expand_hash { | ||||||||||||||||||
|
Subroutine
expand_hash: called CC=13 SCAR=26.2
Pod
undocumented
| |||||||||||||||||||||
| 44 | 369 | my ( $self, $flat, $sep ) = @_; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 369 | my $deep = {}; | |||||||||||||||||||
| 47 | 369 | ▶ | $sep ||= $self->separator; | ||||||||||||||||||
|
Condition33%1/3
$sep ||= $self->separator
MC/DC0%0/2
$sep ||= $self->separator
$sep
$self->separator
Truth table33%1/3
A ||= B
A= $sepB= $self->separator
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 369 | for my $name ( keys %$flat ) { | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 602 | my ( $first, @segments ) = $self->split_name( $name, $sep ); | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 602 | my $box_ref = \$deep->{$first}; | |||||||||||||||||||
| 54 | 602 | for (@segments) { | |||||||||||||||||||
| 55 | 337 | ▶ | if ( /^(0|[1-9]\d*)$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 118 | ▶ | $$box_ref = [] unless defined $$box_ref; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 118 | ▶ | croak "HFH: param clash for $name=$_" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | unless ref $$box_ref eq 'ARRAY'; | ||||||||||||||||||||
| 59 | 118 | $box_ref = \( $$box_ref->[$1] ); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||
| 62 | 219 | ▶ | s/\\(.)/$1/g if $sep; # remove escaping | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 219 | ▶ | $$box_ref = {} unless defined $$box_ref; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 219 | ▶ | $$box_ref = { '' => $$box_ref } if ( !ref $$box_ref ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 219 | ▶ | croak "HFH: param clash for $name=$_" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | unless ref $$box_ref eq 'HASH'; | ||||||||||||||||||||
| 67 | 219 | $box_ref = \( $$box_ref->{$_} ); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 602 | ▶ | if ( defined $$box_ref ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 2 | ▶ | croak "HFH: param clash for $name value $flat->{$name}" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | if ref $$box_ref ne 'HASH'; | ||||||||||||||||||||
| 73 | 2 | $box_ref = \( $$box_ref->{''} ); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 602 | $$box_ref = $flat->{$name}; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | 369 | return $deep; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | ▶ | sub collapse_hash { | ||||||||||||||||||
|
Subroutine
collapse_hash: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||||||||||
| 82 | 0 | my $deep = shift; | |||||||||||||||||||
| 83 | 0 | my $flat = {}; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | $self->_collapse_hash( $deep, $flat, () ); | |||||||||||||||||||
| 86 | 0 | return $flat; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ▶ | sub join_name { | ||||||||||||||||||
|
Subroutine
join_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 90 | 0 | my ( $self, @array ) = @_; | |||||||||||||||||||
| 91 | 0 | my $sep = substr( $self->separator, 0, 1 ); | |||||||||||||||||||
| 92 | 0 | return join $sep, @array; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | ▶ | sub _collapse_hash { | ||||||||||||||||||
|
Subroutine
_collapse_hash: not called CC=8 SCAR=42.8
| |||||||||||||||||||||
| 96 | 0 | my ( $self, $deep, $flat, @segments ) = @_; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | ▶ | if ( !ref $deep ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 99 | 0 | my $name = $self->join_name(@segments); | |||||||||||||||||||
| 100 | 0 | $flat->{$name} = $deep; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | elsif ( ref $deep eq 'HASH' ) { | ||||||||||||||||||||
| 103 | 0 | for ( keys %$deep ) { | |||||||||||||||||||
| 104 | # escape \ and separator chars (once only, at this level) | ||||||||||||||||||||
| 105 | 0 | my $name = $_; | |||||||||||||||||||
| 106 | 0 | ▶ | if ( defined( my $sep = $self->separator ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 0 | $sep = "\Q$sep"; | |||||||||||||||||||
| 108 | 0 | $name =~ s/([\\$sep])/\\$1/g; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | 0 | $self->_collapse_hash( $deep->{$_}, $flat, @segments, $name ); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | elsif ( ref $deep eq 'ARRAY' ) { | ||||||||||||||||||||
| 114 | 0 | for ( 0 .. $#$deep ) { | |||||||||||||||||||
| 115 | 0 | ▶ | $self->_collapse_hash( $deep->[$_], $flat, @segments, $_ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | if defined $deep->[$_]; | ||||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||
| 120 | 0 | croak "Unknown reference type for ", $self->join_name(@segments), ":", ref $deep; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 146 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 125 | 146 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 126 | 146 | 1; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||