I am working on a perl script right now and wondered if I could get some help. The script is below. This script uses LWP::Simple to check URLs it gets from a MySQL database. I would like for it to timeout and consider a URL not working if it can't be validated in 20 seconds or less. Does anyone know how to use perl's alarm() function to make this happen? I tried a variety of things with no success. Maybe LWP even has a built-in function for timeout that I don't know about.
use LWP::Simple;
use DBI;
$db_database = "info";
$db_uid = "root";
$db_pwd = "password";
$dbh = DBI->connect ("DBI:mysql:$db_database".$mysqlsock, $db_uid, $db_pwd) or die("could not connect to db\n");
$sth = $dbh->prepare("SELECT url FROM files");
$sth -> execute();
$numrows = $sth->rows;
for ($i = 0; $i = $numrows; ++$i) {
$url = ($sth->fetchrow_array);
if((head($url)) or (get($url))) {
$validity = "works";
} else {
$validity = "broke";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}
print "$validity - $url\n";
}Thanks! Ryan McKillen
Code:
#!/usr/bin/perl use LWP::Simple;
use DBI;
$db_database = "info";
$db_uid = "root";
$db_pwd = "password";
$dbh = DBI->connect ("DBI:mysql:$db_database".$mysqlsock, $db_uid, $db_pwd) or die("could not connect to db\n");
$sth = $dbh->prepare("SELECT url FROM files");
$sth -> execute();
$numrows = $sth->rows;
for ($i = 0; $i = $numrows; ++$i) {
$url = ($sth->fetchrow_array);
if((head($url)) or (get($url))) {
$validity = "works";
} else {
$validity = "broke";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}
print "$validity - $url\n";
}