Gossamer Forum
Home : General : Perl Programming :

LWP::UserAgent Question

Quote Reply
LWP::UserAgent Question
I am trying to check a short list of URLs in a MySQL DB to see if they are valid. Here is the code I am using:

Code:
my($query) = "SELECT weburl FROM table where condition = '1'";
my($sth) = $dbh->prepare($query);
$sth->execute || die("Could not execute!");
while(@row = $sth->fetchrow) {
$whereto = $row[0];
&letsgo;
}
exit;
sub letsgo {

$ua->agent("Mozilla/8.0");
$req = new HTTP::Request 'GET' => '$whereto';
# send request
$res = $ua->request($req);


if ($res->is_success) {
print "It worked\n";
} else {
print "Error: " . $res->code . " " . $res->message;
print "It failed\n";
}
}

It always fails BUT if I change
Code:
$req = new HTTP::Request 'GET' => '$whereto';

to something like

Code:
$req = new HTTP::Request 'GET' => 'http://www.gossamer-threads.com';

It works. What am I missing?
Quote Reply
Re: [BennyHill] LWP::UserAgent Question In reply to
Try $sth->fetchrow_array
Quote Reply
Re: [BennyHill] LWP::UserAgent Question In reply to
Hi,

Quote:
$req = new HTTP::Request 'GET' => '$whereto'
Change this command to:
$req = new HTTP::Request 'GET' => "$whereto"
It should work.

Cheers,
TheStone.



B.

Last edited by:

TheStone: Dec 3, 2001, 4:07 PM
Quote Reply
Re: [TheStone] LWP::UserAgent Question In reply to
Hehe missed that.
Quote Reply
Re: [TheStone] LWP::UserAgent Question In reply to
Will $sth->fetchrow work without the _array though?
Quote Reply
Re: [PaulW] LWP::UserAgent Question In reply to
Yes, it will. Also you can use:

while (my $rs = $sth->fetchrow_hashref) {
print $rs->{Field_name};
}

Cheers,
TheStone.

B.

Last edited by:

TheStone: Dec 3, 2001, 5:32 PM
Quote Reply
Re: [TheStone] LWP::UserAgent Question In reply to
Yeah, I just didn't realise you could use fetchrow. I thought you had to specify _array, _hashref etc..

Learn something new every day :)

Thanks.
Quote Reply
Re: [PaulW] LWP::UserAgent Question In reply to
You are welcomeSmile

B.
Quote Reply
Re: [BennyHill] LWP::UserAgent Question In reply to
> $req = new HTTP::Request 'GET' => '$whereto';

Ugh. Read up on Perl interpolation, i.e. the difference between using "" and ''. In your case, just change the above line to:

Code:
my $req = new HTTP::Request GET => $whereto;

Rgds

- wil
Quote Reply
Re: [Wil] LWP::UserAgent Question In reply to
>>Ugh. Read up on Perl interpolation<<

Pot...kettle...black.
Quote Reply
Re: [TheStone] LWP::UserAgent Question In reply to
Does that mean that DBI uses fetchrow_array by default and that's why you don't need to specify it?


Quote Reply
Re: [PaulW] LWP::UserAgent Question In reply to
umm. R T F M ?

Sorry, s/Larry/Paul/;

- wil

Last edited by:

Wil: Dec 4, 2001, 4:59 AM
Quote Reply
Re: [Wil] LWP::UserAgent Question In reply to
Surely you can think of something more original.

It also proves to me that you yourself haven't read them as they don't answer my question..haha Tongue

The perldocs are generally the first place I look, unlike...erm.....


Last edited by:

PaulW: Dec 4, 2001, 5:07 AM
Quote Reply
Re: [PaulW] LWP::UserAgent Question In reply to
There is no "default". What do you mean by that? This is just a snippet of available methods, which should answer your question.

Code:
$sth->fetch;
fetches the next row of data, returning an array reference with the field values

$sth->fetchrow_hashref
synonym for fetch.

$sth->fetchrow;
fetches the next row of data, returning an array with the field values.

$sth->fetchrow_array;
synonym for fetchrow.

It's just a synonym for another method. There is no default.

- wil

Last edited by:

Wil: Dec 4, 2001, 5:15 AM
Quote Reply
Re: [BennyHill] LWP::UserAgent Question In reply to
Crap. Thanks guys. I can't believe I did something so stupid. Wait, yes I can. I spent 30 minutes looking for my keys the other day. Finally found them in the freezer (don't ask). Anyway, thanks.