"Chas. Owens" schreef:
> On Thu, Apr 10, 2008 at 9:25 PM, Keenlearner <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to