use CGI qw/:standard/;
use CGI::Cookie;
# Create new cookies and send them
$cookie1 = new CGI::Cookie(-name=>'ID',-value=>123456);
$cookie2 = new CGI::Cookie(-name=>'preferences',
-value=>{ font => Helvetica,
size => 12 }
);
print header(-cookie=>[$cookie1,$cookie2]);
# fetch existing cookies
%cookies = fetch CGI::Cookie;
$id = $cookies{'ID'}->value;
For full information on cookies see
http://www.ics.uci.edu/pub/ietf/http/rfc2109.txt
$c = new CGI::Cookie(-name => 'foo',
-value => 'bar',
-expires => '+3M',
-domain => '.capricorn.com',
-path => '/cgi-bin/database'
-secure => 1
);
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).
-expires accepts any of the relative or absolute date formats recognized by CGI.pm, for example ``+3M'' for three months in the future. See CGI.pm's documentation for details.
-domain points to a domain name or to a fully qualified host name. If not specified, the cookie will be returned only to the Web server that created it.
-path points to a partial URL on the current server. The cookie will be returned to all URLs beginning with the specified path. If not specified, it defaults to '/', which returns the cookie to all pages at your site.
-secure if set to a true value instructs the browser to return the cookie only when a cryptographic protocol is in use.
%cookies = fetch CGI::Cookie;
fetch returns an associative array consisting of all cookies returned by the browser. The keys of the array are the cookie names. You can iterate through the cookies this way:
%cookies = fetch CGI::Cookie;
foreach (keys %cookies) {
do_something($cookies{$_});
}
In a scalar context, fetch() returns a hash reference, which
may be more efficient if you are manipulating multiple cookies. =head2
Manipulating Cookies
Cookie objects have a series of accessor methods to get and set cookie attributes. Each accessor has a similar syntax. Called without arguments, the accessor returns the current value of the attribute. Called with an argument, the accessor changes the attribute and returns its new value.
$name = $c->name;
$new_name = $c->name('fred');
$value = $c->value;
@new_value = $c->value(['a','b','c','d']);
value() is context sensitive. In an array context it will return the current value of the cookie as an array. In a scalar context it will return the first value of a multivalued cookie.
Address bug reports and comments to: lstein@genome.wi.mit.edu