SYNOPSIS
use LWP::UserAgent;
use HTTP::CookieJar::LWP;
my $ua = LWP::UserAgent->new(
cookie_jar => HTTP::CookieJar::LWP->new
);
and then it says
DESCRIPTION
This module is an experimental adapter to make HTTP::CookieJar work wi
+th LWP.
So you go read the docs for HTTP::CookieJar and find that they are comprehensive. You may also need to read some of the docs for LWP::UserAgent.
Here is a simple example:
#! perl
use strict;
use warnings;
use feature qw/ say /;
use Data::Dumper;
use LWP::UserAgent;
use HTTP::CookieJar::LWP;
my $protocol = 'http://';
my $domain = 'www.foobar.com';
my $path = '/baz';
my $page = '/qux.html';
my $url = join '', $protocol, $domain, $path, $page;
my $file = 'cookies.txt';
## Only here for demo. You would have a cookie file
## made somewhere else, or if not you would pass the
## values directly to HTTP::CookieJar::LWP::add()
open( my $write_handle, '>', $file ) or die "open: $!\n";
print $write_handle "Secret_Number=42; Max-Age=300; Domain=$domain; Pa
+th=$path\n";
close $write_handle or die "close: $!\n";
my $ua = LWP::UserAgent->new(
cookie_jar => HTTP::CookieJar::LWP->new()
);
open( my $read_handle, '<', $file ) or die "open: $!\n";
foreach my $cookie ( <$read_handle> ) {
chomp $cookie;
$ua->cookie_jar->add( $url, $cookie );
}
say $ua->cookie_jar->cookie_header( $url );
say Dumper $ua->cookie_jar->cookies_for( $url );
__END__
Output:
[00:04][nick:~/monks]$ perl 1139766.pl
Secret_Number=42
$VAR1 = {
'name' => 'Secret_Number',
'domain' => 'www.foobar.com',
'expires' => 1440486562,
'value' => '42',
'path' => '/baz',
'last_access_time' => 1440486262,
'creation_time' => 1440486262
};
Now you just GET the URL with LWP::UserAgent in the normal way, and the cookie goes right along in the headers.
Hope this helps!
The way forward always starts with a minimal test.
|