What you're trying to do is maintain state in an inherently stateless protocl (HTTP). To get around this, typically the user is given a a sessionID and this is tied to state information that is stored in a database. That's a more robust solution. For something quick 'n dirty, you could use Storable to serialize your data structure, store it in a cookie, and later retrieve it and deserialize it. Here's a small code sample to show you how this is done (minus using a cookie):

use strict; use warnings; use Storable qw/ freeze thaw /; use Data::Dumper; my %foo = ( one => [ qw/ a b c / ], two => [ qw/ 1 2 3 / ] ); my $serialized = freeze \%foo; my %clone = %{ thaw $serialized }; print Dumper \%clone;

For the snippet above, you would store $serialized in the cookie. I've never actually tried this, but it should work. However, remember that this is not a good long-term solution. Amongst other things, cookies accept a max of 4K of data and will truncate anything over that. If you rely on this technique too much, you'll get bit. Further, this increases bandwidth (slightly) and setting too many cookies (by overusing this technique) will also cause problems.

Good luck!

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Storing an array in a cookie... by Ovid
in thread Storing an array in a cookie... by Nacho37

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.