为什么我没有从 LWP::UserAgent->new() 获得定义的值?

发布于 2024-07-26 22:12:54 字数 446 浏览 6 评论 0原文

我在使用 cookie_jar 方法时收到此错误:

Can't call method cookie_jar on an undefined value

这是我的代码:

my $cookie_jar= new HTTP::Cookies;
my $ua=new LWP::UserAgent;
my %cookies= fetch CGI::Cookie;
my $encoded=$cookies{'SCred'};
$cookie_jar->set_cookie(1, "SCred", $encoded, "/", $SSO_DOMAIN,  "", 0, 0, 60*60, 0);
$ua->cookie_jar($cookie_jar); # I get error on this line

知道为什么收到此错误吗?

I get this error while using cookie_jar method:

Can't call method cookie_jar on an undefined value

Here is my code:

my $cookie_jar= new HTTP::Cookies;
my $ua=new LWP::UserAgent;
my %cookies= fetch CGI::Cookie;
my $encoded=$cookies{'SCred'};
$cookie_jar->set_cookie(1, "SCred", $encoded, "/", $SSO_DOMAIN,  "", 0, 0, 60*60, 0);
$ua->cookie_jar($cookie_jar); # I get error on this line

Any idea why I get this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音栖息无 2024-08-02 22:12:54

我已经尝试过你的代码(带有严格的警告和我认为必需的模块,并将自由变量转换为字符串):

kyle@indigo64 ~[home*]$ cat x.pl
use strict;
use warnings;
use HTTP::Cookies;
use LWP::UserAgent;
use CGI::Cookie;

my $ua         = new LWP::UserAgent;
my %cookies    = fetch CGI::Cookie;
my $encoded    = $cookies{'SCred'};

my $cookie_jar = new HTTP::Cookies;
$cookie_jar->set_cookie(
  1, "SCred", '$encoded',
  "/", '$SSO_DOMAIN',  "",
  0, 0, 60*60, 0
);
$ua->cookie_jar($cookie_jar);

print "ua: ",$ua,"\n";
print "ua->cookie_jar: ",$ua->cookie_jar,"\n";
mortis@indigo64 ~[home*]$ perl x.pl
ua: LWP::UserAgent=HASH(0x82f8cc8)
ua->cookie_jar: HTTP::Cookies=HASH(0x82f8b84)
kyle@indigo64 ~[home*]$ 

并且它有效。 您可能想发布一个更完整的示例,或者在 '$ua = new...' 和 '$ua->cookie_jar' 行之间是否有行,其中 $ua 被重新分配或设置为 undef? 如果您在调用 cookie_jar 之前打印“$ua”的值,您应该会看到它是 undef,它必须在第一次赋值和调用该方法之间的某个位置被重置。

I've tried your code (with strict, warnings and what I think are the required modules, with turning the free variables into strings):

kyle@indigo64 ~[home*]$ cat x.pl
use strict;
use warnings;
use HTTP::Cookies;
use LWP::UserAgent;
use CGI::Cookie;

my $ua         = new LWP::UserAgent;
my %cookies    = fetch CGI::Cookie;
my $encoded    = $cookies{'SCred'};

my $cookie_jar = new HTTP::Cookies;
$cookie_jar->set_cookie(
  1, "SCred", '$encoded',
  "/", '$SSO_DOMAIN',  "",
  0, 0, 60*60, 0
);
$ua->cookie_jar($cookie_jar);

print "ua: ",$ua,"\n";
print "ua->cookie_jar: ",$ua->cookie_jar,"\n";
mortis@indigo64 ~[home*]$ perl x.pl
ua: LWP::UserAgent=HASH(0x82f8cc8)
ua->cookie_jar: HTTP::Cookies=HASH(0x82f8b84)
kyle@indigo64 ~[home*]$ 

and it works. You might want to either post a fuller example, or are there lines between the '$ua = new...' and the '$ua->cookie_jar' lines where $ua is re-assigned or otherwise set to undef? If you print the value of '$ua' just before the call to cookie_jar you should see that it's undef, it must be being reset somewhere between the first assignment and where you are calling that method.

睡美人的小仙女 2024-08-02 22:12:54

为了排除任何奇怪的交互,请尝试以下操作:

my $cookie_jar = HTTP::Cookies->new;
my $ua = LWP::UserAgent->new;
my %cookies = CGI::Cookie->fetch;
my $encoded = $cookies{'SCred'};
$cookie_jar->set_cookie(
    1, "SCred", $encoded, "/", $SSO_DOMAIN,  "", 0, 0, 60*60, 0
);
$ua->cookie_jar($cookie_jar); # I get error on this line

现在,由于某种原因,$ua 未定义,这意味着构造函数调用:

my $ua = LWP::UserAgent->new;

失败。 我对 fastcgi 不太熟悉。 但是,LWP::UserAgent 在构造函数中出现任何故障时都会发出嘎嘎声:我不确定您如何到达有问题的行。

你检查过服务器日志吗? 完全在黑暗中拍摄:以下内容是否向错误日志添加了任何有用的信息?

my $ua = eval { LWP::UserAgent->new }
    or warn "LWP::UserAgent->new failed: $@";

Just to rule out any weird interactions, try the following:

my $cookie_jar = HTTP::Cookies->new;
my $ua = LWP::UserAgent->new;
my %cookies = CGI::Cookie->fetch;
my $encoded = $cookies{'SCred'};
$cookie_jar->set_cookie(
    1, "SCred", $encoded, "/", $SSO_DOMAIN,  "", 0, 0, 60*60, 0
);
$ua->cookie_jar($cookie_jar); # I get error on this line

Now, for some reason, $ua is undefined, which means the constructor call:

my $ua = LWP::UserAgent->new;

failed. I am not too familiar with fastcgi. However, LWP::UserAgent croaks on any failure in the constructor: I am not sure how you are reaching the line in question.

Have you checked the server logs? Total shot in the dark: Does the following add any useful information to the error log?

my $ua = eval { LWP::UserAgent->new }
    or warn "LWP::UserAgent->new failed: $@";
揪着可爱 2024-08-02 22:12:54

如果这是您收到的实际错误,则这不是 cookie_jar 的问题。 这恰好是您尝试调用的第一个方法。 检查您在创建用户代理时是否确实获得了一个对象。

删除所有 cookie 内容并尝试 agent 方法:

use strict;
my $ua = eval { LWP::UserAgent->new }
    or die "Could not make user-agent! $@";
$ua->agent("TestAgent");

如果构造函数中出现任何问题,您应该能够捕获它。 然而,如果你的脚本还没有死掉,我认为你还有其他问题。 如果 LWP::UserAgent::new 遇到问题,它就已经崩溃了。 它唯一可以返回的是它已经调用了方法的定义值。

If that is the actual error you get, it's not a problem with cookie_jar. That just happens to be the first method you try to call. Check that you actually get an object when when make the user-agent.

Remove all the cookie stuff and try the agent method:

use strict;
my $ua = eval { LWP::UserAgent->new }
    or die "Could not make user-agent! $@";
$ua->agent("TestAgent");

If anything goes wrong in the constructor, you should be able to catch it. However, if your script isn't already die-ing, I think you have something else wrong. If LWP::UserAgent::new runs into a problem, it already croaks. The only thing it can return is a defined value that it has already called methods on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文