我正在努力解决的是为什么 LWP::UserAgent 不提供访问器,这样我就可以通过提供 cookie 的名称来获取 cookie_jar 中关于 cookie 的所有信息。我意识到 cookie_jar 上有 scan() 方法,但是为如此基本的东西提供回调似乎有很多开销。这就是我现在所拥有的:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dump qw (dump);
use WWW::Mechanize;
my $mech = WWW::Mechanize->new;
$mech->get( 'http://www.nytimes.com' );
my %cookies = ();
$mech->cookie_jar->scan( \&check_cookies );
dump \%cookies;
sub check_cookies {
my @args = @_;
$cookies{ $args[1] } = {
version => $args[0],
val => $args[2],
path => $args[3],
domain => $args[4],
port => $args[5],
path_spec => $args[6],
secure => $args[7],
expires => $args[8],
discard => $args[9],
hash => $args[10],
};
}
脚本的输出是这样的:
{ adxcs => {
discard => 1,
domain => ".nytimes.com",
expires => undef,
hash => {},
path => "/",
path_spec => 1,
port => undef,
secure => undef,
val => "-",
version => 0,
},
RMID => {
discard => undef,
domain => ".nytimes.com",
expires => 1374340257,
hash => {},
path => "/",
path_spec => 1,
port => undef,
secure => undef,
val => "02b4bc821c00500991212ba2",
version => 0,
},
}
因此,这使我可以轻松地通过名称访问 cookie,但我想知道是否有更简单的方法可以做到这一点,或者是否有一个我不知道的有用模块。