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

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];

Replies are listed 'Best First'.
Re: CGI cookie turns array ref into string
by zwon (Abbot) on Apr 24, 2010 at 20:40 UTC

    You can serialize array before storing it as cookie. If array contains only numbers you can do it like this:

    my $porder = join ",", @{gen_order(30)};
    and later restore array using split:
    my @porder = split /,/, $info{'porder'};
    But generally I would prefer CGI::Session for storing data locally.
Re: CGI cookie turns array ref into string
by Corion (Patriarch) on Apr 24, 2010 at 20:41 UTC

    Maybe you don't want to store the data in the cookie, but want to use CGI::Session to store arbitrary data and associate it with a user through a cookie?

Re: CGI cookie turns array ref into string
by dsheroh (Monsignor) on Apr 25, 2010 at 12:46 UTC
    To more explicitly state what other monks have already alluded to:

    Cookies only store strings. Period. You can't put a reference into a cookie, only a string, so the reference gets stringified into ARRAY(0xDEADBEEF) when you try to put it into the cookie.

    If you want to store non-string information in a cookie, you must convert it to a string and then convert it back from that string into a usable form when the user agent sends the cookie back to you.

Re: CGI cookie turns array ref into string
by Anonymous Monk on Apr 24, 2010 at 20:29 UTC
    And here is the code that tries to retrieve the array

    There is no array, cookies are key/value pairs.

      The issue isn't that cookies are key/value pairs. Hashes are also key/value pairs, but the value in that pair can be a reference (or any other arbitrary scalar).

      The issue is that cookies only accept strings as their values.