Timer in perl

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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sooraj S [ Mo, 30 Mai 2011 09:49 ] [ ID #2060295 ]

Re: Timer in perl

>>>>> "SS" == Sooraj S <soorajspadmanabhan [at] gmail.com> 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 [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Di, 31 Mai 2011 05:24 ] [ ID #2060297 ]
Perl » gmane.comp.lang.perl.beginners » Timer in perl

Vorheriges Thema: CGI.pm and scrolling list box code error
Nächstes Thema: How to avoid using the slow array subscripting operator?