Operating System - HP-UX
1753550 Members
5302 Online
108795 Solutions
New Discussion юеВ

Re: Perl script help needed

 
Piotr Kirklewski
Super Advisor

Perl script help needed

Hi there

Could you guys help me to figure out how can I set a timeout value smaller that 1 sec ? I tried 0.3 but it didn't work. The program actually does stuff longer then if the value is set to 1.

#!/usr/bin/perl -w
use strict;
use warnings;

use Socket;
my $timeout = 1;

open (MYFILE, '/home/x/robot/ips.txt');

while () {
chomp;
$a="$_";

socket SOCKET, PF_INET, SOCK_STREAM, getprotobyname('tcp') or die;
my $ip = inet_aton( $a );
my $address = sockaddr_in( 80, $ip );

eval {
local $SIG{ALRM} = sub { die "timeout" };
alarm($timeout);
connect (SOCKET, $address) || error();
alarm(0);
};
if ($@) {
close SOCKET || die "close: $!";
#print "$a is NOT listening on tcp port 80.\n";
}
else {
close SOCKET || die "close: $!";
open (OUTFILE, '/home/x/robot/ip80ok.txt');
print "$a\n";
print OUTFILE "$a\n";
close (OUTFILE);
}

}
close(MYFILE);

Jesus is the King
4 REPLIES 4
Matti_Kurkela
Honored Contributor

Re: Perl script help needed

The number of seconds value for the "alarm" function must be an integer.

The description of "alarm" in perlfunc(1) man page says:

-----
[...]
For delays of finer granularity than one second, the Time::HiRes module (from CPAN, and starting from Perl 5.8 part of the standard distribution) provides ualarm(). You may also use Perl's four-argument version of select() leaving the first three arguments undefined, or you might be able to use the "syscall" interface to access setitimer(2) if your system supports it. See perlfaq8 for details.
[...]
-----

Information about Time::HiRes can of course be found at the cpan.org website:

http://search.cpan.org/~zefram/Time-HiRes-1.9722/HiRes.pm

MK
MK
James R. Ferguson
Acclaimed Contributor

Re: Perl script help needed

HI:

You don't need the '-w' switch on the shebang line *and* the 'warnings' pragma. Use the pragma.

The '-w' switch applies globally whereas the pragma allows much finer control over what you want to be warned about and where you might want to temporarily turn off warnings.

As Matti noted, use the 'Time::HiRes' module for working with times less than one-second. This module is part of the core Perl distribution so you can immediately deploy it.

Regards!

...JRF...
Piotr Kirklewski
Super Advisor

Re: Perl script help needed

OK
I have no idea how to do this.
Some example based on the code above please ?
Regards
Peter
Jesus is the King
James R. Ferguson
Acclaimed Contributor

Re: Perl script help needed

Hi (again):

I have no idea how to do this.
Some example based on the code above please ?

Homework, eh?

Start by adding the 'Time::HiRes' module:

use Time::HiRes qw(ualarm);

Now, change 'alarm()' to 'ualarm()'

Remember, the 'ualarm' runs in *micro*seconds.

Regards!

...JRF...