[cgiapp] Using CGI::Cookie from inside CGI::Application

Rhesa Rozendaal perl at rhesa.com
Wed May 21 17:56:27 EDT 2008


CGI User wrote:
> My question are:
> 1. Why it didn't work in the first case.
> 2. If the first case won't work, then how I can set all the cookie parameters
> e.g. domain, path, expires, secure using the second case.
> 3. How I can retrieve my cookie from the browser is the recommended first case 
> to set cookie is not working, and the second case is working but I cannot see 
> any method to retrieve my own cookie?
> 

CGI::Cookie and CGI->cookie (sorry :) have the same interface, and they should 
produce the same results:

use strict;
use warnings;

use Test::More tests => 1;

$ENV{CGI_APP_RETURN_ONLY} = 1;

my $m = My::App->new;
$m->start_mode('q_cookie');
my $q_out = $m->run;
diag $q_out;

$m = My::App->new;
$m->start_mode('cgi_cookie');
my $cgi_out = $m->run;
diag $cgi_out;

is $q_out, $cgi_out, "headers are identical";


package My::App;

use base qw/CGI::Application/;

sub setup {
     my $self = shift;
     $self->run_modes( [ qw/ cgi_cookie q_cookie / ] );
}

sub q_cookie {
     my $self = shift;
     my $q    = $self->query;
     my $cookie = $q->cookie(
             -name       => 'some_name',
             -value      => '12345',
             -domain     => '.come_domain.com',
             -path       => '/',
             -expires    => '+1h',
             -secure     => 1,
             );

     $self->header_add(-cookie => [$cookie]);

     return '';
}

sub cgi_cookie {
     my $self = shift;
     my $cookie = CGI::Cookie->new(
             -name       => 'some_name',
             -value      => '12345',
             -domain     => '.come_domain.com',
             -path       => '/',
             -expires    => '+1h',
             -secure     => 1,
             );

     $self->header_add(-cookie => [$cookie]);
     return '';
}

__END__
1..1
# Set-Cookie: some_name=12345; domain=.come_domain.com; path=/; expires=Wed, 
21-May-2008 22:51:04 GMT; secure
# Date: Wed, 21 May 2008 21:51:03 GMT
# Content-Type: text/html; charset=ISO-8859-1
#
# Set-Cookie: some_name=12345; domain=.come_domain.com; path=/; expires=Wed, 
21-May-2008 22:51:04 GMT; secure
# Date: Wed, 21 May 2008 21:51:03 GMT
# Content-Type: text/html; charset=ISO-8859-1
#
ok 1 - headers are identical



More information about the cgiapp mailing list