MISC – Jun 5th, 2014

Today I wrote a script to check urls availability using Perl.

(LWP::Simple::head($_) ? “OK” : “!!!***BAD***!!!”);

The $_ will be an url like ‘http://abc.com.cn’ and it will append ‘OK’ if it is available otherwise ‘BAD’.

However for some urls, LWP::Simple::head will be hanging. With the help of http://perldoc.perl.org/functions/alarm.html, by changing the code as below, it will avoid hanging after hanging for 2 seconds.

alarm( 2 );
eval
{
$str = "$_ " . (LWP::Simple::head($_) ? "OK" : "!!!***BAD***!!!");
};

### I found that LWP::Simple::head return false if the link it accesses redirects to another link.
The following command will return OK for this scenario. Note, in Perl, 0 means false and all other non-zero mean true.

$str = "$_ " . ((system("wget -q --spider -t 2 $_")) ? "!!!***BAD***!!!" : "OK");