Tom Allison am Dienstag, 22. November 2005 12.24:
> I figured out I can do this:
>
> eval {
>       local $SIG{ALRM} = sub { warn("alarm\n") };
>       alarm 1;
>       for(my $j = 0; $j < 1_000_000, $j++) {
>               my $x = log(rand()*1000+1);
>       }
>       alarm 0;
> };
> if ( $@ ) {
>       carp "[EMAIL PROTECTED]";
> }
>
> Which is very similar to the man pages.
> But there are a few inconsistencies I'm trying to sort out.

The syntax is incorrect, see the comma in the for-().

Your sighandler uses warn instead of die (die puts the output into $@, warn to 
STDERR)

> First, I can't seem to get the statement from the manpages
> die $@,"\n" unless $@ eq "alarm\n";
> to work.

Thats (i guess) because of the warn above.

> Second, I tried putting this into a for(){...} loop to attempt this 5 
> times.  I can't seem to get it to do that.  It goes through one loop and
> then it just keeps doing the warn("alarm\n") without doing anything else.

Try:

use strict;
use warnings;
eval {
        local $SIG{ALRM} = sub { die 'Alarm' }; # <<<<<<
        alarm 1;
        for(my $j = 0; $j < 1_000_000; $j++) { # <<<<<<<
                my $x = log(rand()*1000+1);
die;
warn "in for $x" unless $j / 100;
        }
        alarm 0;
};
if ( $@ ) {
        warn "DIED ", ([EMAIL PROTECTED]/^Alarm/) ? 'alarm' : 'no alarm'; # <<<
}


> Conceptually, I'm trying to do something and if it take too long, start
> over again, retrying 5 times.

I think this should be ok with corrected syntax
(maybe I overlooked something)

hth,

joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to