# man > CGI::Cookie(3pm)

---
type: CommandReference
command: CGI::Cookie
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference

- `$c = CGI::Cookie->new(-name=>'ID', -value=>123456)` — Create a new cookie with basic attributes.  
- `$c->bake;` — Send cookie to browser (requires CGI.pm loaded).  
- `print header(-cookie=>[$c1,$c2]);` — Send multiple cookies via CGI.pm header.  
- `%cookies = CGI::Cookie->fetch;` — Retrieve all incoming cookies as a hash.  
- `$val = $cookies{'ID'}->value;` — Get the value of a specific cookie.  
- `$c->as_string;` — Get the cookie’s Set-Cookie header line string.  
- `%cookies = CGI::Cookie->parse($raw_cookie_string);` — Parse a raw `Cookie:` header.  
- `$c->name('new_name');` — Change the cookie’s name and return it.

## Name

[CGI::Cookie](http://localhost/phpMan.php/perldoc/CGI%3A%3ACookie/markdown) — Interface to HTTP Cookies

## Synopsis

perl
use CGI qw/:standard/;
use CGI::Cookie;

# Create and send cookies
$cookie1 = CGI::Cookie->new(-name=>'ID',-value=>123456);
$cookie2 = CGI::Cookie->new(-name=>'preferences', -value=>{ font => 'Helvetica', size => 12 });
print header(-cookie=>[$cookie1,$cookie2]);

# Retrieve incoming cookies
%cookies = CGI::Cookie->fetch;
$id = $cookies{'ID'}->value;

# Parse from raw environment
%cookies = CGI::Cookie->parse($ENV{COOKIE});
## Options

### Constructor options

- `-name` — Cookie name (required, scalar).  
- `-value` — Cookie value (required; scalar, arrayref, or hashref).  
- `-expires` — Expiration as a relative time string (e.g., `'+3M'` for 3 months). If omitted, cookie lasts until browser closes.  
- `-max-age` — Expiration in seconds from now. Less widely supported than `-expires`.  
- `-domain` — Domain string (e.g., `'.example.com'`). Must contain at least two dots. Defaults to the originating host.  
- `-path` — URL path prefix for which the cookie is valid. Defaults to `/`.  
- `-secure` — Boolean; if true, cookie sent only over HTTPS.  
- `-httponly` — Boolean; if true, cookie inaccessible to JavaScript (helps prevent XSS).  
- `-samesite` — `"Strict"`, `"Lax"`, or `"None"`. Limited browser support; consult current documentation.

### Methods

- `fetch()` — Returns hash of all cookies sent by the browser (decoded URL escaping). In scalar context returns a hashref.  
- `raw_fetch()` — Like `fetch()` but performs **no** URL unescaping.  
- `parse($string)` — Parses a raw `Cookie:` header value into a hash. Returns empty list/hashref if `$string` is undefined.  
- `bake()` — Prints a `Set-Cookie` header using CGI.pm’s `header()` (loads CGI.pm if needed). Accepts an optional mod_perl request object.  
- `as_string()` — Returns the cookie as an RFC‑compliant `Set-Cookie` string.  
- `name([$new_name])` — Get/set cookie name.  
- `value([@new_values])` — Get/set cookie value. In list context returns all values; scalar returns the first.  
- `domain([$new_domain])` — Get/set domain.  
- `path([$new_path])` — Get/set path.  
- `expires([$new_expires])` — Get/set expiration time.  
- `max_age([$new_max_age])` — Get/set max-age value.

## Examples

### Creating a new cookie

perl
my $c = CGI::Cookie->new(
    -name    => 'foo',
    -value   => ['bar','baz'],
    -expires => '+3M',
    -domain  => '.capricorn.com',
    -path    => '/cgi-bin/database',
    -secure  => 1,
    -samesite=> 'Lax'
);
### Sending cookies with CGI.pm

perl
print header(-cookie=>$c);
# or multiple cookies
print header(-cookie=>[$cookie1,$cookie2]);
### Manual Set-Cookie header

perl
my $c = CGI::Cookie->new(-name=>'foo', -value=>'bar', -expires=>'+3M');
print "Set-Cookie: $c\n";
print "Content-Type: text/html\n\n";
# or using as_string()
print "Set-Cookie: ", $c->as_string, "\n";
### Retrieving cookies

perl
my %cookies = CGI::Cookie->fetch;
for (keys %cookies) {
    do_something($cookies{$_});
}
# scalar context returns a hash reference
my $cookies = CGI::Cookie->fetch;
### Parsing a raw cookie string

perl
my $raw = 'cookie1=val1; cookie2=val2';
my %cookies = CGI::Cookie->parse($raw);
## See Also

- [CGI::Carp](http://localhost/phpMan.php/perldoc/CGI%3A%3ACarp/markdown)  
- [CGI](http://localhost/phpMan.php/perldoc/CGI)  
- [RFC 6265](https://tools.ietf.org/html/rfc6265)  
- [RFC 2109](http://www.ietf.org/rfc/rfc2109.txt)  
- [RFC 2965](http://www.ietf.org/rfc/rfc2965.txt)