Perl 中的远程 Web 表单发布

发布于 2024-11-30 08:45:26 字数 747 浏览 0 评论 0原文

我对 Perl 比较陌生,我试图创建一个 Perl 脚本来以 Web 表单远程登录并返回成功或失败。但它不起作用或者我错过了一些东西,而且它给了我一条错误消息:这是我写的:

#!/usr/bin/perl

use LWP::UserAgent; 
use HTTP::Response;
use HTTP::Request::Common qw(POST);


$ua = LWP::UserAgent->new; 
$ua->agent("Mozilla 8.0...");

$username = "username";
$password = "password";

my $req = (POST 'http://www.domain.com/login.php', 

["Username" => "$username", 
"Password" => "$password"]);

$request = $ua->request($req); 
$content = $request->content; 

if ($res->is_success) {
     print ("success");
     exit;
}

else {
print ("failure");
}

这个脚本根本没有运行,我得到的错误是:

Can't call method "is_success" on an undefined value at c:\remotelogin.pl line 24.

I'm relatively new to perl, I was trying to create a perl script to remote login in a web form and return either sucess or failure. but its not working or i'm missing something, plus it give me an error message: Here is what i wrote:

#!/usr/bin/perl

use LWP::UserAgent; 
use HTTP::Response;
use HTTP::Request::Common qw(POST);


$ua = LWP::UserAgent->new; 
$ua->agent("Mozilla 8.0...");

$username = "username";
$password = "password";

my $req = (POST 'http://www.domain.com/login.php', 

["Username" => "$username", 
"Password" => "$password"]);

$request = $ua->request($req); 
$content = $request->content; 

if ($res->is_success) {
     print ("success");
     exit;
}

else {
print ("failure");
}

this script is not running at all and the error i'm getting is:

Can't call method "is_success" on an undefined value at c:\remotelogin.pl line 24.

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

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

发布评论

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

评论(2

自由范儿 2024-12-07 08:45:26

它的重要性怎么强调都不为过,

use strict;
use warnings;

尤其是在学习 Perl 时。在本例中,您有一个未声明的变量 $res。也许是由于拼写错误?如果你使用了严格和警告,你会得到一个编译错误:

Global symbol "$res" requires explicit package name..

严格和警告可能会产生很多令人生畏的错误,但是一旦你学会了如何避免它们,你就会意识到它们节省了你的时间和精力,而不是相反。

It can't be stressed enough how important it is to

use strict;
use warnings;

Especially when learning perl. In this case, you have an undeclared variable $res. Due to a typo perhaps? If you have used strict and warnings, you would have gotten a compilation error:

Global symbol "$res" requires explicit package name..

Strict and warnings may give a lot of intimidating errors, but once you learn how to avoid them, you realize they save you time and effort rather than the opposite.

调妓 2024-12-07 08:45:26

$res 应替换为 $request

并且使用严格;使用警告;

$res should be replaced with $request.

And use strict; use warnings;

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