开发者

Remote web form post in perl

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";
$passwor开发者_高级运维d = "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.


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.


$res should be replaced with $request.

And use strict; use warnings;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜