Debian Codegraph @

lib/HTML/FormHandler/Params.pm

raw
1 package # hide from Pause
2 HTML::FormHandler::Params;
3 # ABSTRACT: params handling
4
5 use Moose;
6 use Carp;
7
8 has 'separator' => ( isa => 'Str', is => 'rw', default => '.' );
9
10 sub split_name {
11 my ( $self, $name, $sep ) = @_;
12
13 $sep ||= $self->separator;
14 $sep = "\Q$sep";
15
16 if ( $sep eq '[]' ) {
17 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 $name =~ m/^ ( [^\\$sep]* (?: \\(?:.|$) [^\\$sep]* )* ) /gx;
32 my $first = $1;
33 $first =~ s/\\(.)/$1/g; # remove escaping
34
35 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 return ( $first, @segments );
41 }
42
43 sub expand_hash {
44 my ( $self, $flat, $sep ) = @_;
45
46 my $deep = {};
47 $sep ||= $self->separator;
48
49 for my $name ( keys %$flat ) {
50
51 my ( $first, @segments ) = $self->split_name( $name, $sep );
52
53 my $box_ref = \$deep->{$first};
54 for (@segments) {
55 if ( /^(0|[1-9]\d*)$/ ) {
56 $$box_ref = [] unless defined $$box_ref;
57 croak "HFH: param clash for $name=$_"
58 unless ref $$box_ref eq 'ARRAY';
59 $box_ref = \( $$box_ref->[$1] );
60 }
61 else {
62 s/\\(.)/$1/g if $sep; # remove escaping
63 $$box_ref = {} unless defined $$box_ref;
64 $$box_ref = { '' => $$box_ref } if ( !ref $$box_ref );
65 croak "HFH: param clash for $name=$_"
66 unless ref $$box_ref eq 'HASH';
67 $box_ref = \( $$box_ref->{$_} );
68 }
69 }
70 if ( defined $$box_ref ) {
71 croak "HFH: param clash for $name value $flat->{$name}"
72 if ref $$box_ref ne 'HASH';
73 $box_ref = \( $$box_ref->{''} );
74 }
75 $$box_ref = $flat->{$name};
76 }
77 return $deep;
78 }
79
80 sub collapse_hash {
81 my $self = shift;
82 my $deep = shift;
83 my $flat = {};
84
85 $self->_collapse_hash( $deep, $flat, () );
86 return $flat;
87 }
88
89 sub join_name {
90 my ( $self, @array ) = @_;
91 my $sep = substr( $self->separator, 0, 1 );
92 return join $sep, @array;
93 }
94
95 sub _collapse_hash {
96 my ( $self, $deep, $flat, @segments ) = @_;
97
98 if ( !ref $deep ) {
99 my $name = $self->join_name(@segments);
100 $flat->{$name} = $deep;
101 }
102 elsif ( ref $deep eq 'HASH' ) {
103 for ( keys %$deep ) {
104 # escape \ and separator chars (once only, at this level)
105 my $name = $_;
106 if ( defined( my $sep = $self->separator ) ) {
107 $sep = "\Q$sep";
108 $name =~ s/([\\$sep])/\\$1/g;
109 }
110 $self->_collapse_hash( $deep->{$_}, $flat, @segments, $name );
111 }
112 }
113 elsif ( ref $deep eq 'ARRAY' ) {
114 for ( 0 .. $#$deep ) {
115 $self->_collapse_hash( $deep->[$_], $flat, @segments, $_ )
116 if defined $deep->[$_];
117 }
118 }
119 else {
120 croak "Unknown reference type for ", $self->join_name(@segments), ":", ref $deep;
121 }
122 }
123
124 __PACKAGE__->meta->make_immutable;
125 use namespace::autoclean;
126 1;
127
128 __END__
129
130 =pod
131
132 =encoding UTF-8
133
134 =head1 NAME
135
136 HTML::FormHandler::Params - params handling
137
138 =head1 VERSION
139
140 version 0.410002
141
142 =head1 AUTHOR
143
144 FormHandler Contributors - see HTML::FormHandler
145
146 =head1 COPYRIGHT AND LICENSE
147
148 This software is copyright (c) 2017 by Gerda Shank.
149
150 This is free software; you can redistribute it and/or modify it under
151 the same terms as the Perl 5 programming language system itself.
152
153 =cut