jmb1911 has asked for the wisdom of the Perl Monks concerning the following question:

I have an array and a hash that I want to pass to another form using hidden fields. I know how to do this with numbers & strings, but I can't find an example of how to do this with arrays & hashes. Thanks.

Replies are listed 'Best First'.
Re: hidden fields
by Masem (Monsignor) on Jun 20, 2001 at 22:52 UTC
    In addition to what others have said, you do want to minimize the amount of details that you carry between forms, both from a bandwidth-friendly standpoint, as well as from a security standpoint (since hidden fields are only hidden to those oblivious to the View Source command...)

    The best solution is to create a unique session id (based on any number of unique factors including localtime, user's id, random numbers, and so forth), and store the large data into that, and only send the session id in the hidden field. The recieving script can then verify the id and get the info it needs from the file. Now, one problem here is how to store the data; arrays and hashes don't store directly, but if you use either Data::Dumper, or FreezeThaw (as examples), which can convert a complex data structure to something that can be recovered, you can write the data out to file and read it in at the next step.

    If you are absolutely keen on using everything as hidden fields, you can also probably apply these same modules to convert the array or hash to a printable string, which you can use as the value field for the hidden array. But again, this is a potental security problem as you open your CGI inner workings to the user.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: hidden fields
by daniell (Sexton) on Jun 20, 2001 at 22:39 UTC
    You seem to be talking about html forms and their hidden fields. I'd try something like joining the array into one big field and then splitting it later on. Something like join("\t", @array). Of course there may be a more automatic way for mod CGI if you're using that.
Re: hidden fields
by sierrathedog04 (Hermit) on Jun 20, 2001 at 22:41 UTC
    You cannot pass an array or a hash directly from one HTML form to another using hidden fields. A hidden field can only pass text.

    If you want to pass arrays or hashes as hidden fields you must first convert the arrays and hashes to text.

    If your arrays or hashes are large then after you have converted them to strings you will probably want to use the 'POST' rather than the 'GET' method to send the strings as hidden variables in your HTML forms. Some environments impose artificial limits on the length of the parameter strings that the 'GET' method produces.

Re: hidden fields
by voyager (Friar) on Jun 20, 2001 at 23:45 UTC
    Although you asked how to do this with hidden fields, you could use a cookie. Using CGI.pm, the cookie function can handle hashes with very little gymnastics on your part.
Re: hidden fields
by TGI (Parson) on Jun 20, 2001 at 23:09 UTC

    Try WDDX.


    TGI says moo