Tips to add cookies using Perl www::mechanize

June 14, 2008

In the Perl module www::Mechanize, it is easy to disable the cookie by using:

my $mech = WWW::Mechanize->new();
$mech->cookie_jar(undef);

When you want to initialize the new Cookie, using
$mech->cookie_jar();
will not always work.

However, you can use the following couple of lines to add the Cookie:
$cj=HTTP::Cookies->new(file => “cookie.jar”,autosave=>1,ignore_discard=>1);
$mech = WWW::Mechanize->new(cookie_jar => $cj);

The cookie is saved under the file “cookie.jar” in the current running dir.