[tpm] WWW::Mechanize and setting cookies

Zoffix Znet zoffix at zoffix.com
Fri Aug 1 02:57:03 PDT 2008


You are not setting the cookie "domain". This works just fine:

#!/usr/bin/env perl

use strict;
use warnings;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new;

$mech->get('http://zoffix.com/new/cookies.pl');
print "BEFORE:\n", $mech->content, "\n";

$mech->cookie_jar->set_cookie( 0, 'foo', 'bar', '/', 'zoffix.com' );

$mech->get('http://zoffix.com/new/cookies.pl');
print "AFTER:\n", $mech->content, "\n";


Prints:
BEFORE:
Cookies:

AFTER:
Cookies:
foo => bar


If you are interested in the source code of that cookies.pl script see:
http://www.zoffix.com/new/cookies.pl?source


Cheers


On Fri, 2008-08-01 at 00:07 -0400, Madison Kelly wrote:
> Zoffix Znet wrote:
> > WWW::Mechanize uses LWP::UserAgent as base class, thus you'd call
> > $mech->cookie_jar to get the HTTP::Cookies object. Which comes to
> > $mech->cookie_jar->set_cookie( $version, $key, $val, $path, $domain,
> > $port, $path_spec, $secure, $maxage, $discard, \%rest );
> > 
> > Cheers.
> 
> Hi,
> 
>    Thanks for this! It seems to make more sense but it still doesn't 
> seem to work for me. Here is how I've updated the code I originally 
> quoted in my reply to Adam:
> 
> # Removed the 'HTTP::Cookies' reference
> my $agent = WWW::Mechanize->new(autocheck => 1);
> ...
> # Changed this to your recommendation.
> $agent->cookie_jar->set_cookie(0, $variable, $value, $path);
> 
> *sigh*
> 
> Madi



More information about the toronto-pm mailing list