CGI-MultiValuedHash

 view release on metacpan or  search on metacpan

lib/CGI/MultiValuedHash.pm  view on Meta::CPAN

When the to_url_encoded_string() and from_url_encoded_string() methods and their 
derivatives are used with the fewest number of arguments, they default to an 
encoding format used in query strings, such as $ENV{QUERY_STRING}.  Normal query 
strings look like this:

	name=name&type=textfield&visible_title=What%27s+your+name%3f

Here's another example with a multi-valued field (it is actually a single line, 
but appears on two here for clarity:

	name=color&type=popup_menu&values=red&values=green&values=blue&
	values=chartreuse&visible_title=What%27s+your+favorite+colour%3f

Some query strings are the result of ISINDEX queries, and they look different:

	tell&me&about&stuff

Cookie strings such as $ENV{HTTP_COOKIE} are different yet and look like:

	name=color; type=popup_menu; values=red&green&blue&chartreuse

In the argument lists for the above methods, DELIM refers to the "&" in normal 
query strings and the "; " in cookies, whereas VALSEP is meaningless with normal 
query strings and is the "&" in "isindex" queries and cookie strings.

=head1 THE DEFAULT FILE FORMAT

When the to_file() and from_file() methods and their derivatives are used with 
the fewest number of arguments, they default to an encoding format that is quite 
easy for humans to read.  This common format is capable of storing an ordered 

lib/CGI/MultiValuedHash.pm  view on Meta::CPAN

	default=minie
	name=words
	type=checkbox_group
	values=eenie
	values=meenie
	values=minie
	values=moe
	visible_title=What%27s+the+combination%3f
	=
	name=color
	type=popup_menu
	values=red
	values=green
	values=blue
	values=chartreuse
	visible_title=What%27s+your+favorite+colour%3f
	=
	type=submit

This file format is identical to that used by CGI.pm when saving its state, so 
such files could be used and manipulated by either that class or this one as you 

t/CGI-MultiValuedHash.t  view on Meta::CPAN

			type => 'textfield',
			name => 'name',
		}, {
			visible_title => "What's the combination?",
			type => 'checkbox_group',
			name => 'words',
			'values' => ['eenie', 'meenie', 'minie', 'moe'],
			default => ['eenie', 'minie'],
		}, {
			visible_title => "What's your favorite colour?",
			type => 'popup_menu',
			name => 'color',
			'values' => ['red', 'green', 'blue', 'chartreuse'],
		}, {
			type => 'submit', 
		},	
	);

	my @src_list_query = split( "\n", <<__endquote );
name=name&type=textfield&visible_title=What%27s+your+name%3F
default=eenie&default=minie&name=words&type=checkbox_group&values=eenie&values=meenie&values=minie&values=moe&visible_title=What%27s+the+combination%3F
name=color&type=popup_menu&values=red&values=green&values=blue&values=chartreuse&visible_title=What%27s+your+favorite+colour%3F
type=submit
__endquote

	my @src_list_cookie = split( "\n", <<__endquote );
name=name; type=textfield; visible_title=What%27s+your+name%3F
default=eenie&minie; name=words; type=checkbox_group; values=eenie&meenie&minie&moe; visible_title=What%27s+the+combination%3F
name=color; type=popup_menu; values=red&green&blue&chartreuse; visible_title=What%27s+your+favorite+colour%3F
type=submit
__endquote
	
	my @src_list_file = split( "\n=\n", substr( <<__endquote, 2 ) );
=
name=name
type=textfield
visible_title=What%27s+your+name%3F
=
default=eenie
default=minie
name=words
type=checkbox_group
values=eenie
values=meenie
values=minie
values=moe
visible_title=What%27s+the+combination%3F
=
name=color
type=popup_menu
values=red
values=green
values=blue
values=chartreuse
visible_title=What%27s+your+favorite+colour%3F
=
type=submit
__endquote

	my (@d1hash,@d2hash,@d1query,@d2query,@d1cookie,@d2cookie,@d1file,@d2file);

t/CGI-MultiValuedHash.t  view on Meta::CPAN

	# try batch decoding all records at once

	@d2hash = CGI::MultiValuedHash->batch_new( 0, \@src_list_hash );
	@d2query = CGI::MultiValuedHash->batch_new( 0, \@src_list_query );
	@d2cookie = CGI::MultiValuedHash->batch_new( 0, \@src_list_cookie, "; ", "&" );
	@d2file = CGI::MultiValuedHash->batch_new( 0, \@src_list_file, "\n" );
	
	my @expected = (
		"{ 'name' => [ 'name', ], 'type' => [ 'textfield', ], 'visible_title' => [ 'What's your name?', ], }, ",
		"{ 'default' => [ 'eenie', 'minie', ], 'name' => [ 'words', ], 'type' => [ 'checkbox_group', ], 'values' => [ 'eenie', 'meenie', 'minie', 'moe', ], 'visible_title' => [ 'What's the combination?', ], }, ",
		"{ 'name' => [ 'color', ], 'type' => [ 'popup_menu', ], 'values' => [ 'red', 'green', 'blue', 'chartreuse', ], 'visible_title' => [ 'What's your favorite colour?', ], }, ",
		"{ 'type' => [ 'submit', ], }, ",
	);

	# compare decodes to what we expect

	foreach my $i (0..$#expected) {
		$should = $expected[$i];
	
		$did = serialize( scalar( $d1hash[$i]->fetch_all() ) );
		result( $did eq $should, "decode hash $i single returns '$did'" );



( run in 1.037 second using v1.01-cache-2.11-cpan-364913b4093 )