Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Pass a hash as parameter to a CGI?

by C_T (Scribe)
on Sep 16, 2004 at 18:05 UTC ( [id://391526]=note: print w/replies, xml ) Need Help??


in reply to Re: Pass a hash as parameter to a CGI?
in thread Pass a hash as parameter to a CGI?

Very cool! What would happen in this example code if the hash was like this?:

$var = undef; $hash{'param1'} = 1; $hash{'param2'} = $var;

CT

Charles Thomas
Madison, WI

Replies are listed 'Best First'.
Re^3: Pass a hash as parameter to a CGI?
by ikegami (Patriarch) on Sep 16, 2004 at 18:20 UTC

    The code above only covers array and hashes of strings, and undef is not a string. Below is a variation that supports undef. Anything more complicated should definitely use FreezeThaw.

    sub serialize_string_list { return join('|', map { (defined($_) ? do { local $_=$_; s/\^/^1/g; s/\|/^2/g; $_ } : '^0' ) } @_ ); } sub deserialize_string_list { return map { ($_ eq '^0' ? undef : do { local $_=$_; s/\^1/^/g; s/\^2/|/g; $_ } ) } split(/\|/, $_[0]); } $s = serialize_string_list(%test_hash); %test_hash = deserialize_string_list($s);
Re^3: Pass a hash as parameter to a CGI?
by C_T (Scribe) on Sep 16, 2004 at 18:13 UTC
    I answered my own question. It works, but throws warnings if you have them turned on.

    $test_hash_ref = { 'apple' => 'red', 'junk' => 'foo^bar', 'junk2' => undef, 'lime' => 'green' }; Use of uninitialized value in substitution (s///) at ./serialize.pl li +ne 19. Use of uninitialized value in join or string at ./serialize.pl line 19 +. apple|red|junk|foo^1bar|junk2||lime|green $test_hash_ref = { 'apple' => 'red', 'junk' => 'foo^bar', 'lime' => 'green', 'junk2' => '' };

    CT

    Charles Thomas
    Madison, WI

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://391526]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found