A perl reference is just a fancy pointer. Even if you could recreate the pointer when the .cgi is called again, that to which it was pointing is no longer in memory. What you can do is serialize the hash. There are modules to do that, or here's a simple way to do a hash of strings or an array of strings:

$s = join('|', map { local $_=$_; s/\^/^1/g; s/\|/^2/g; $_ } %test_hash );

And to deserialize:

%test_hash = map { local $_=$_; s/\^1/^/g; s/\^2/|/g; $_ } split(/\|/, $s);

I didn't use a slashed escape mechanism since it's makes deserialization hard. (i.e. "Should I split on this pipe, or is that an escaped pipe?") Trivia: IP over Serial Line (SLIP) and maybe Point to Point Protocol (PPP) use a similar escaping mechanism to escape packet delimiters since the delimiters cannot appear inside a packet.


Update: I had typos originally. Fixed. here's some test code:

use Data::Dumper (); %test_hash = ( 'apple' => 'red', 'lime' => 'green', 'junk' => 'foo^bar', 'junk2' => 'bla|bla', ); print(Data::Dumper->Dump([ \%test_hash ],[qw( $test_hash_ref )])); print("\n"); $s = join('|', map { local $_=$_; s/\^/^1/g; s/\|/^2/g; $_ } %test_hash ); print($s, "\n"); print("\n"); %test_hash = map { local $_=$_; s/\^1/^/g; s/\^2/|/g; $_ } split(/\|/, $s); print(Data::Dumper->Dump([ \%test_hash ],[qw( $test_hash_ref )])); __END__ output ====== $test_hash_ref = { 'apple' => 'red', 'junk' => 'foo^bar', 'junk2' => 'bla|bla', 'lime' => 'green' }; apple|red|junk|foo^1bar|junk2|bla^2bla|lime|green $test_hash_ref = { 'apple' => 'red', 'junk' => 'foo^bar', 'lime' => 'green', 'junk2' => 'bla|bla' };

In reply to Re: Pass a hash as parameter to a CGI? by ikegami
in thread Pass a hash as parameter to a CGI? by C_T

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.