Perl maximum execution time

Perl maximum execution time

am 11.04.2008 03:25:06 von Keenlearner

Hello, I have had been programming in PHP for a while, new to perl. I
got a perl code bug where it will go to infinite loop. So is there a
maximum execution time that I could set in perl just like in PHP ?
Thanks


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

Re: Perl maximum execution time

am 11.04.2008 10:05:07 von chas.owens

On Thu, Apr 10, 2008 at 9:25 PM, Keenlearner wrote:
> Hello, I have had been programming in PHP for a while, new to perl. I
> got a perl code bug where it will go to infinite loop. So is there a
> maximum execution time that I could set in perl just like in PHP ?
> Thanks
snip

You can set an signal to go off after X seconds with the alarm* function:

#!/usr/bin/perl

use strict;
use warnings;

my $timeout = 5*60; #timeout after five minutes

eval {
local $SIG{ALRM} = sub { die "timeout\n" };
alarm $timeout;
#stuff you want to run in under five minutes
};
die unless $@ eq "timeout\n" if $@;

* http://perldoc.perl.org/functions/alarm.html

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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

Re: Perl maximum execution time

am 11.04.2008 20:46:20 von rvtol+news

"Chas. Owens" schreef:
> On Thu, Apr 10, 2008 at 9:25 PM, Keenlearner wrote:

>> Hello, I have had been programming in PHP for a while, new to perl.
I
>> got a perl code bug where it will go to infinite loop. So is there a
>> maximum execution time that I could set in perl just like in PHP ?
>> Thanks
> snip
>
> You can set an signal to go off after X seconds with the alarm*
> function:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $timeout = 5*60; #timeout after five minutes
>
> eval {
> local $SIG{ALRM} = sub { die "timeout\n" };
> alarm $timeout;
> #stuff you want to run in under five minutes
> };
> die unless $@ eq "timeout\n" if $@;
>
> * http://perldoc.perl.org/functions/alarm.html

Playful:

$ perl -Mstrict -wle'
$SIG{ALRM} = sub { die "died"; };
alarm(3);
sleep(1);
eval {
$@ and printf q{%s: $@<%s>%s}, __LINE__, $@, "\n";
local $SIG{ALRM} = sub { die "died" };
my $old_alarm = alarm(3);
$old_alarm and print "old_alarm: $old_alarm sec\n";
sleep(2);
alarm($old_alarm);
1;
} or do {
alarm(0);
$@ and printf q{%s: $@<%s>%s}, __LINE__, $@, "\n";
# die "died";
};
sleep(5);
print "finished";
'

--
Affijn, Ruud

"Gewoon is een tijger."


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