http://qs321.pair.com?node_id=199845

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

It is late, I am tired of searching for this on the web! Please help a weary monk.
#!/usr/bin/perl use strict; use CGI; my $cgi = new CGI; print $cgi->header( -cookie => new CGI::Cookie( -name => 'ChocolateChip', -value=> $cgi->param('name'), -expires => 'Thu, 26-Sep-2002 00:00:00 GMT', -domain => 'students.cs.byu.edu', -path => '/' + ) )
I would like to know how to set several values in the -value=> part of the cookie (this values come from a form). I would also like to know how to read those values once the cookie is set and have the ChocolateChip cookie to read from (look at the code).
#!/usr/bin/perl use strict; use CGI; my $cgi = new CGI; my %cookie = $cgi->cookie(-name => "ChocolateChip"); if(defined %cookie) { # read from the cookie ChocolateChip to have my document display user # preferences that were saved int the -value=> part of the cookie }
-thanks

Replies are listed 'Best First'.
Re: Setting several values and Reading them from a cookie
by blokhead (Monsignor) on Sep 22, 2002 at 04:48 UTC
    You can pass an array ref (or even a hash ref) to the -value parameter of CGI::Cookie::new. From the CGI::Cookie documentation:
    Create cookies from scratch with the new method. The -name and -value parameters are required. The name must be a scalar value. The value can be a scalar, an array reference, or a hash reference. (At some point in the future cookies will support one of the Perl object serialization protocols for full generality).
    So you can set multiple values by doing
    my $cookie = new CGI::Cookie( -name => 'ChocolateChip', -value => [ 'Sweet', 'Semi-Sweet', 'Yumm +y' ] );
    If there are multiple values for a cookie, the $cookie->value method will return a list of them (only if it's called in list context, of course).

    blokhead

•Re: Setting several values and Reading them from a cookie
by merlyn (Sage) on Sep 22, 2002 at 15:13 UTC
    You really shouldn't be sending anything in a cookie except a serial number uniquely identifying the browser, and then provide a login mechanism to temporarily associate this particular browser for a limited time with a server-side storage of the data. Of course, I wrote a column on that.

    -- Randal L. Schwartz, Perl hacker

Re: Setting several values and Reading them from a cookie
by BrowserUk (Patriarch) on Sep 22, 2002 at 04:43 UTC

    Update: Post withdrawn. See below for better info. Shouldn't try and code when I just woke up. Must get around to re-reading the docs for some of the first modules I ever used. Seems there are bits I didn't understand back then. No surprise.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!