Timer in perl

Timer in perl

am 30.05.2011 09:49:56 von Sooraj S

Hi,

Could any one tell me what is wrong with my code? It does not time out
after 5 seconds which is what i need. It remains in the infinit loop.

#! /usr/bin/perl

use strict;
use warnings;

sub comeOut;
$SIG{ALRM} = \&comeOut;

while (1) { };

eval
{
alarm 5; #timeout is 60
}

sub comeOut
{
print "\nThis is what i wanted\n";
exit;
}


Pls help me...


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Timer in perl

am 31.05.2011 05:24:43 von Uri Guttman

>>>>> "SS" == Sooraj S writes:

SS> Hi,
SS> Could any one tell me what is wrong with my code? It does not time out
SS> after 5 seconds which is what i need. It remains in the infinit loop.

SS> while (1) { };

your bug is here. you never exit the loop.

SS> eval
SS> {
SS> alarm 5; #timeout is 60
SS> }

so this eval is never executed.

you need to but the alarm before the loop starts and wrap the loop in
the eval block. then the eval can trap the exit and you can timeout the
loop.

see the examples in perldoc perlipc for more on how to use alarm.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/