| 1 |
NAME |
| 2 |
CGI::Expand - convert flat hash to nested data using TT2's dot |
| 3 |
convention |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
use CGI::Expand (); |
| 7 |
use CGI; # or Apache::Request, etc. |
| 8 |
|
| 9 |
$args = CGI::Expand->expand_cgi( CGI->new('a.0=3&a.2=4&b.c.0=x') ); |
| 10 |
|
| 11 |
Or, as an imported function for convenience: |
| 12 |
|
| 13 |
use CGI::Expand; |
| 14 |
use CGI; # or Apache::Request, etc. |
| 15 |
|
| 16 |
$args = expand_cgi( CGI->new('a.0=3&a.2=4&b.c.0=x') ); |
| 17 |
# $args = { a => [3,undef,4], b => { c => ['x'] }, } |
| 18 |
|
| 19 |
# Or to catch exceptions: |
| 20 |
eval { |
| 21 |
$args = expand_cgi( CGI->new('a.0=3&a.2=4&b.c.0=x') ); |
| 22 |
} or log_and_exit( $@ ); |
| 23 |
|
| 24 |
#----- |
| 25 |
use CGI::Expand qw(expand_hash); |
| 26 |
|
| 27 |
$args = expand_hash({'a.0'=>77}); # $args = { a => [ 77 ] } |
| 28 |
|
| 29 |
DESCRIPTION |
| 30 |
Converts a CGI query into structured data using a dotted name convention |
| 31 |
similar to TT2. |
| 32 |
|
| 33 |
"expand_cgi" works with CGI.pm, Apache::Request or anything with an |
| 34 |
appropriate "param" method. Or you can use "expand_hash" directly. |
| 35 |
|
| 36 |
If you prefer to use a different flattening convention then CGI::Expand |
| 37 |
can be subclassed. |
| 38 |
|
| 39 |
MOTIVATION |
| 40 |
The Common Gateway Interface restricts parameters to name=value pairs, |
| 41 |
but often we'd like to use more structured data. This module uses a name |
| 42 |
encoding convention to rebuild a hash of hashes, arrays and values. |
| 43 |
Arrays can either be indexed explicitly or from CGI's multi-valued |
| 44 |
parameter handling. |
| 45 |
|
| 46 |
The generic nature of this process means that the core components of |
| 47 |
your system can remain CGI ignorant and operate on structured data. |
| 48 |
Better for modularity, better for testing. |
| 49 |
|
| 50 |
DOT CONVENTION |
| 51 |
The key-value pair "a.b.1=hi" expands to the perl structure: |
| 52 |
|
| 53 |
{ a => { b => [ undef, "hi" ] } |
| 54 |
|
| 55 |
The key ("a.b.1") specifies the location at which the value ("hi") is |
| 56 |
stored. The key is split on '.' characters, the first segment ("a") is a |
| 57 |
key in the top level hash, subsequent segments may be keys in sub-hashes |
| 58 |
or indices in sub-arrays. Integer segments are treated as array indices, |
| 59 |
others as hash keys. |
| 60 |
|
| 61 |
Array size is limited to 100 by default. The limit can be altered by |
| 62 |
subclassing or using the deprecated $Max_Array package variable. See |
| 63 |
below. |
| 64 |
|
| 65 |
The backslash '\' escapes the next character in cgi parameter names |
| 66 |
allowing '.' , '\' and digits in hash keys. The escaping '\' is removed. |
| 67 |
Values are not altered. |
| 68 |
|
| 69 |
Key-Value Examples |
| 70 |
# HoHoL |
| 71 |
a.b.1=hi ---> { a => { b => [ undef, "hi" ] } |
| 72 |
|
| 73 |
# HoLoH |
| 74 |
a.1.b=hi ---> { a => [ undef, { b => "hi" } ] } |
| 75 |
|
| 76 |
# top level always a hash |
| 77 |
9.0=hi ---> { "9" => [ "hi" ] } |
| 78 |
|
| 79 |
# can backslash escape to treat digits hash as keys |
| 80 |
a.\0=hi ---> { "a" => { 0 => "hi"} } |
| 81 |
|
| 82 |
# or to put . and \ literals in keys |
| 83 |
a\\b\.c=hi --- { 'a\\b\.c' => "hi" } |
| 84 |
|
| 85 |
METHODS / FUNCTIONS |
| 86 |
The routines listed below are all methods, but can be imported to be |
| 87 |
called as functions. In other words, you can call |
| 88 |
"CGI::Expand->expand_hash(...)" or you can import "expand_hash" and then |
| 89 |
call "expand_hash(...)" without using method invocation syntax. |
| 90 |
|
| 91 |
"expand_cgi" is exported by default. "expand_hash" and "collapse_hash" |
| 92 |
are exported upon request. |
| 93 |
|
| 94 |
expand_cgi |
| 95 |
my $deep_hash = expand_cgi ( $CGI_object_or_similar ); |
| 96 |
|
| 97 |
Takes a CGI object and returns a hashref for the expanded data |
| 98 |
structure (or dies, see "EXCEPTIONS"). |
| 99 |
|
| 100 |
Wrapper around expand_hash that uses the "param" method of the CGI |
| 101 |
object to collect the names and values. |
| 102 |
|
| 103 |
Handles multivalued parameters as array refs (although they can't be |
| 104 |
mixed with indexed arrays and will have an undefined ordering). |
| 105 |
|
| 106 |
$query = 'a.0=3&a.2=4&b.c.0=x&c.0=2&c.1=3&d=&e=1&e=2'; |
| 107 |
|
| 108 |
$args = expand_cgi( CGI->new($query) ); |
| 109 |
|
| 110 |
# result: |
| 111 |
# $args = { |
| 112 |
# a => [3,undef,4], |
| 113 |
# b => { c => ['x'] }, |
| 114 |
# c => ['2','3'], |
| 115 |
# d => '', |
| 116 |
# e => ['1','2'], # order depends on CGI/etc |
| 117 |
# }; |
| 118 |
|
| 119 |
expand_hash |
| 120 |
my $deep_hash = expand_hash( $flat_hash ); |
| 121 |
|
| 122 |
Expands the keys of the parameter hash according to the dot |
| 123 |
convention (or dies, see "EXCEPTIONS"). |
| 124 |
|
| 125 |
$args = expand_hash({ 'a.b.1' => [1,2] }); |
| 126 |
# $args = { a => { b => [undef, [1,2] ] } } |
| 127 |
|
| 128 |
collapse_hash |
| 129 |
my $flat_hash = collapse_hash( $deep_hash ); |
| 130 |
|
| 131 |
The inverse of expand_hash. Converts the $deep_hash data structure |
| 132 |
back into a flat hash. |
| 133 |
|
| 134 |
$flat = collapse_hash({ a => { b => [undef, [1,2] ] } }); |
| 135 |
# $flat = { 'a.b.1.0' => 1, 'a.b.1.1' => 2 } |
| 136 |
|
| 137 |
EXCEPTIONS |
| 138 |
WARNING: The *users* of your site can cause these exceptions so you must |
| 139 |
decide how they are handled (possibly by letting the process die). |
| 140 |
|
| 141 |
"CGI param array limit exceeded..." |
| 142 |
If an array index exceeds the array limit (default: 100) then an |
| 143 |
exception is thrown. |
| 144 |
|
| 145 |
"CGI param clash for..." |
| 146 |
A cgi query like "a=1&a.b=1" would require the value of $args->{a} |
| 147 |
to be both 1 and { b => 1 }. Such type inconsistencies are reported |
| 148 |
as exceptions. (See test.pl for for examples) |
| 149 |
|
| 150 |
SUBCLASSING |
| 151 |
Subclassing in now the preferred way to change the behaviour and |
| 152 |
defaults. (Previously package variables were used, see test.pl). |
| 153 |
|
| 154 |
The methods which may be overriden by subclasses are separator, |
| 155 |
max_array, split_name and join_name. |
| 156 |
|
| 157 |
max_array |
| 158 |
$subclass->max_Array; |
| 159 |
|
| 160 |
The limit for the array size, defaults to 100. The value 0 can be |
| 161 |
used to disable the use of arrays, everthing is a hash key. |
| 162 |
|
| 163 |
separator |
| 164 |
$subclass->separator; |
| 165 |
|
| 166 |
Returns the separator charaters used to split the keys of the flat |
| 167 |
hash. The default is '.' but multiple characters are allowed. The |
| 168 |
default join will use the first character. |
| 169 |
|
| 170 |
If there is no separator then '\' escaping does not occur. This is |
| 171 |
for use with split_name and join_name below. |
| 172 |
|
| 173 |
split_name |
| 174 |
my @segments = $subclass->split_name($name); |
| 175 |
|
| 176 |
The split_name method must break $name in to key segments for the |
| 177 |
nested data structure. The default version just splits on the |
| 178 |
separator characters with a bit of fiddling to handle escaping. |
| 179 |
|
| 180 |
join_name |
| 181 |
my $name = $subclass->join_name(@segments); |
| 182 |
|
| 183 |
The inverse of split_name, joins the segments back to the key for |
| 184 |
the flat hash. The default version uses the first character of the |
| 185 |
string returned by the separator method. |
| 186 |
|
| 187 |
DEPRECATIONS |
| 188 |
$CGI::Expand::Separator and $CGI::Expand::Max_Array are deprecated. They |
| 189 |
still work for now but emit a warning (supressed with |
| 190 |
$CGI::Expand::BackCompat = 1) |
| 191 |
|
| 192 |
Using the functions by their fully qualified names ceased to work at |
| 193 |
around version 1.04. They're now class methods so just replace the last |
| 194 |
:: with ->. |
| 195 |
|
| 196 |
LIMITATIONS |
| 197 |
The top level is always a hash. Consequently, any digit only names will |
| 198 |
be keys in this hash rather than array indices. |
| 199 |
|
| 200 |
Image inputs with name.x, name.y coordinates are ignored as they will |
| 201 |
class with the value for name. |
| 202 |
|
| 203 |
TODO |
| 204 |
Thing about ways to keep $cgi and the expanded version in sync |
| 205 |
|
| 206 |
Glob style parameters (with SCALAR, ARRAY and HASH slots) would resolve |
| 207 |
the type clashes, probably no fun to use. Look at using |
| 208 |
Template::Plugin::StringTree to avoid path clashes |
| 209 |
|
| 210 |
SEE ALSO |
| 211 |
* HTTP::Rollup - Replaces CGI.pm completely, no list ordering. |
| 212 |
|
| 213 |
* CGI::State - Tied to CGI.pm, unclear error checking |
| 214 |
|
| 215 |
* Template::Plugin::StringTree |
| 216 |
|
| 217 |
* Hash::Flatten - Pick your delimiters |
| 218 |
|
| 219 |
* http://template-toolkit.org/pipermail/templates/2002-January/002368. |
| 220 |
html |
| 221 |
|
| 222 |
* There's a tiny and beautiful reduce solution somewhere on perlmonks. |
| 223 |
|
| 224 |
AUTHOR |
| 225 |
Brad Bowman <[email protected]> |
| 226 |
|
| 227 |
Pod corrections: Ricardo Signes |
| 228 |
|
| 229 |
COPYRIGHT |
| 230 |
Copyright (C) 2004-2013, Brad Bowman. |
| 231 |
|
| 232 |
LICENSE |
| 233 |
CGI::Expand is free software; you can redistribute it and/or modify it |
| 234 |
under the terms of either: |
| 235 |
|
| 236 |
a) the GNU General Public License as published by the Free Software |
| 237 |
Foundation; either version 1, or (at your option) any later version, or |
| 238 |
|
| 239 |
b) the "Artistic License" which comes with Perl. |
| 240 |
|
| 241 |
For more details, see the full text of the licenses at |
| 242 |
<http://www.perlfoundation.org/artistic_license_1_0>, and |
| 243 |
<http://www.gnu.org/licenses/gpl-1.0.html>. |
| 244 |
|