[tpm] WWW::Mechanize and setting cookies

Zoffix Znet zoffix at zoffix.com
Fri Aug 1 03:06:14 PDT 2008


I stand corrected, this code (yours originally) works just fine:

The reason it works is because LWP::UserAgent sets the {cookie_jar}
element to what you've passed as 'cookie_jar' argument to the
constructor: $self->{cookie_jar} = $jar;

I never looked at LWP::UserAgent source code last night, thus I missed
it. Once again, the reason it didn't work for you is because you weren't
setting the domain for the cookie.



#!/usr/bin/env perl

use strict;
use warnings;
use WWW::Mechanize;
use HTTP::Cookies;

my $mech = WWW::Mechanize->new( cookie_jar => HTTP::Cookies->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";


BEFORE:
Cookies:

AFTER:
Cookies:
foo => bar


Cheers.


On Fri, 2008-08-01 at 05:53 -0400, Zoffix Znet wrote:
> Well {cookie_jar} is not defined when you call a method on it later on.
> 
> As for "debuging", I just said that if you see "use base qw/Foo Bar/;"
> than make sure you check out Foo and Bar modules for the methods which
> you are looking for, same goes for "our @ISA = qw/Foo Bar/;"
> 
> 
> 
> On Fri, 2008-08-01 at 00:09 -0400, Madison Kelly wrote:
> > Zoffix Znet wrote:
> > > Yes, that won't work, because WWW::Mechanize doesn't actually set
> > > {cookie_jar} element in its blessed hashref. Take a look at Mech's
> > sub
> > > new {}..
> > > 
> > > Now why it doesn't error out, as you've said, with " Can't call
> > method
> > > "set_cookie" on an undefined value at LINE" with your code below I
> > don't
> > > really understand.. but anyway.. use ->cookie_jar method to obtain
> > the
> > > HTTP::Cookies object and always check the "use base" or @ISA
> > assignments
> > > when you can't find the documented method in the code ^_^
> > > 
> > > Cheers.
> > 
> > I think it didn't error out because 'cookie_jar' is a hash key in the 
> > blessed 'self', which '$agent' is a reference to.
> > 
> 
> 
> 
> 
> 
> > I have to claim ignorance now; how would I use 'use base' or check the 
> > @ISA assignments to debug? I understand that these control the order of 
> > method lookups...
> > 
> > Madi
> 
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm



More information about the toronto-pm mailing list