1Cookie::Baker(3pm) User Contributed Perl Documentation Cookie::Baker(3pm)
2
3
4
6 Cookie::Baker - Cookie string generator / parser
7
9 use Cookie::Baker;
10
11 $headers->push_header('Set-Cookie', bake_cookie($key,$val));
12
13 my $cookies_hashref = crush_cookie($headers->header('Cookie'));
14
16 Cookie::Baker provides simple cookie string generator and parser.
17
19 This module tries to use Cookie::Baker::XS's crush_cookie by default.
20 If this fails, it will use Cookie::Baker's pure Perl crush_cookie.
21
22 There is no XS implementation of bake_cookie yet.
23
25 bake_cookie
26 my $cookie = bake_cookie('foo','val');
27 my $cookie = bake_cookie('foo', {
28 value => 'val',
29 path => "test",
30 domain => '.example.com',
31 expires => '+24h'
32 } );
33
34 Generates a cookie string for an HTTP response header. The first
35 argument is the cookie's name and the second argument is a plain
36 string or hash reference that can contain keys such as "value",
37 "domain", "expires", "path", "httponly", "secure", "max-age",
38 "samesite".
39
40 value
41 Cookie's value.
42
43 domain
44 Cookie's domain.
45
46 expires
47 Cookie's expires date time. Several formats are supported:
48
49 expires => time + 24 * 60 * 60 # epoch time
50 expires => 'Wed, 03-Nov-2010 20:54:16 GMT'
51 expires => '+30s' # 30 seconds from now
52 expires => '+10m' # ten minutes from now
53 expires => '+1h' # one hour from now
54 expires => '-1d' # yesterday (i.e. "ASAP!")
55 expires => '+3M' # in three months
56 expires => '+10y' # in ten years time (60*60*24*365*10 seconds)
57 expires => 'now' #immediately
58
59 max-age
60 If defined, sets the max-age for the cookie.
61
62 path
63 Cookie's path.
64
65 httponly
66 If true, sets HttpOnly flag. false by default.
67
68 secure
69 If true, sets secure flag. false by default.
70
71 samesite
72 If defined as 'lax' or 'strict' or 'none' (case-insensitive),
73 sets the SameSite restriction for the cookie as described in
74 the draft proposal <https://tools.ietf.org/html/draft-west-
75 first-party-cookies-07>, which is already implemented in Chrome
76 (v51), Safari (v12), Edge (v16), Opera (v38) and Firefox
77 (v60).
78
79 crush_cookie
80 Parses cookie string and returns a hashref.
81
82 my $cookies_hashref = crush_cookie($headers->header('Cookie'));
83 my $cookie_value = $cookies_hashref->{cookie_name}
84
86 CPAN already has many cookie related modules. But there is no simple
87 cookie string generator and parser module.
88
89 CGI, CGI::Simple, Plack, Dancer::Cookie
90
92 Copyright (C) Masahiro Nagano.
93
94 This library is free software; you can redistribute it and/or modify it
95 under the same terms as Perl itself.
96
98 Masahiro Nagano <kazeburo@gmail.com>
99
100
101
102perl v5.34.0 2021-07-22 Cookie::Baker(3pm)