为什么我没有从 LWP::UserAgent->new() 获得定义的值?
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经尝试过你的代码(带有严格的警告和我认为必需的模块,并将自由变量转换为字符串):
并且它有效。 您可能想发布一个更完整的示例,或者在 '$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):
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.
为了排除任何奇怪的交互,请尝试以下操作:
现在,由于某种原因,$ua 未定义,这意味着构造函数调用:
失败。 我对
fastcgi
不太熟悉。 但是,LWP::UserAgent
在构造函数中出现任何故障时都会发出嘎嘎声:我不确定您如何到达有问题的行。你检查过服务器日志吗? 完全在黑暗中拍摄:以下内容是否向错误日志添加了任何有用的信息?
Just to rule out any weird interactions, try the following:
Now, for some reason,
$ua
is undefined, which means the constructor call: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?
如果这是您收到的实际错误,则这不是
cookie_jar
的问题。 这恰好是您尝试调用的第一个方法。 检查您在创建用户代理时是否确实获得了一个对象。删除所有 cookie 内容并尝试
agent
方法:如果构造函数中出现任何问题,您应该能够捕获它。 然而,如果你的脚本还没有死掉,我认为你还有其他问题。 如果 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: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.