sub createQueryString { # This routine expects a hash as an argument and will return a valid query string with # hash keys as the names and hash values as query string values. If multiple values # are associated with one key, pass them as an array reference. my %data = ( @_ ); my ( $key, $value ); my $query_string = ''; # The following is a hexadecimal list of the characters that should be # uri encoded. This will be passed as the second argument to the # uri_escape() function. my $cgi_chars = '\x00-\x29\x2b\x2c\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\xff'; while (($key, $value) = each %data) { $key = uri_escape( $key, $cgi_chars ); if ( ref( $value ) eq "ARRAY" ) { # <- that's a ref! # We have multiple values for this key foreach ( @$value ) { my $array_value = uri_escape( $_, $cgi_chars ); $query_string .= "$key=$array_value&"; } } else { $value = uri_escape( $value, $cgi_chars ); $query_string .= "$key=$value&"; } } chop $query_string if $query_string; # remove trailing ampersand return $query_string; }