Hello,

I'm making a simple survey site and using the CGI::Cookie module to store the user information through the course of the survey. The information includes an array. This seems like it should be a simple problem, but for the life of me I can't figure it out.

Essentially, I'm storing one cookie, whose value is a hash containing the user ID number and a reference to the necessary array. The problem arises when the script is called again and I attempt to access the array through the cookie. The array ref only exists now as a string i.e. "ARRAY(0x87480c)". Thus, when I try to access elements in the array, I get the error:

"Can't use string ("ARRAY(0x87480c)") as an ARRAY ref while "strict refs" in use"

Does anyone know why the array itself isn't being saved? My code is based directly on the CGI::Cookie tutorial page...

This is the code that initializes the cookie

my $porder = gen_order(30); my $c = $cgi->cookie(-name=>'info',-value=>{ 'user' => $UID, 'porder' => $porder, 'count' => 1 }); print $session->header(-cookie=>$c); sub gen_order { my $limit = shift; my @nums = (1..$limit); fisher_yates_shuffle(\@nums); return \@nums; }

And here is the code that tries to retrieve the array

my %info = $cgi->cookie('info'); my $porder = $info{'porder'}; my $current = $porder->[0];

In reply to CGI cookie turns array ref into string by Anonymous Monk

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.