scoping, sig handlers, and labels

C

ctcgag

I expect the program below to go into an infinite loop after print 9,
but instead it errors out. Why can't it find the label REDO?

I plead guilty to leaving a sub through redo, but the rest of it
I swear I'm innocent.

#perl -w
use strict;
REDO:foreach(1..1000) {
$SIG{ALRM}=sub {redo REDO};
alarm 10;
system "sleep $_"; # don't use builtin sleep, just in case.
alarm 0;
print "$_\n";
};
__END__
1
2
3
4
5
6
7
8
9
Exiting subroutine via redo at - line 3.
Exiting eval via redo at - line 3.
Label not found for "redo REDO" at - line 3.

Xho
 
B

Bill

I expect the program below to go into an infinite loop after print 9,

#perl -w
use strict;
REDO:foreach(1..1000) {
$SIG{ALRM}=sub {redo REDO};
alarm 10;
system "sleep $_"; # don't use builtin sleep, just in case.
alarm 0;
print "$_\n";
};


from perlfunc:
===
It is usually a mistake to intermix alarm and sleep calls. (sleep may be
internally implemented in your system with alarm)
 
A

Anno Siegel

I expect the program below to go into an infinite loop after print 9,
but instead it errors out. Why can't it find the label REDO?

I plead guilty to leaving a sub through redo, but the rest of it
I swear I'm innocent.

#perl -w
use strict;
REDO:foreach(1..1000) {
$SIG{ALRM}=sub {redo REDO};
alarm 10;
system "sleep $_"; # don't use builtin sleep, just in case.
alarm 0;
print "$_\n";
};
__END__
1
2
3
4
5
6
7
8
9
Exiting subroutine via redo at - line 3.
Exiting eval via redo at - line 3.
Label not found for "redo REDO" at - line 3.

I'm not sure what's happening, but in a similar vein I noticed you can't
leave a SIG handler via "goto" back to the main program. Like in your
case, the label isn't found.

Anno
 
C

ctcgag

Bill said:
from perlfunc:
===
It is usually a mistake to intermix alarm and sleep calls. (sleep may be
internally implemented in your system with alarm)

That's why I shelled out to do the sleep, not that it makes any difference.

Xho
 
C

ctcgag

Purl Gurl said:
ctcgag wrote:

(snipped)



#!perl

$SIG{ALRM} = \&Jump;

JUMP:

foreach(.1, .4, .5, 1, 4)
{
alarm 2;
sleep ($_);
alarm 0;
print "$_\n";
if ($jump eq "true")
{ $jump = "false"; goto JUMP; }
};

sub Jump
{ $jump = "true"; }

If you shell out to the system for the sleep, then this still waits for
that call to return before goto JUMP. If I simply wanted to act upon
the time spent after it was already spent, I wouldn't use alarm at all, I
would just make two calls to time() and subtract. I guess I'm stuck with
those ugly die "alarmed\n" /eval{}/ if($@ eq "alarmed\n") constructs which
so badly clutter up the code.

Xho
 
B

Bill

That's why I shelled out to do the sleep, not that it makes any difference.

Xho

I see what you mean, having run the code on linux now. And look at this:

#!/usr/bin/perl
use strict;
use warnings;

my $rdo = sub { goto REDO; };

REDO:
foreach(1...1000) {
print "$_ ... ";
$SIG{ALRM}= $rdo->();
alarm 10;
system "sleep $_"; # don't use builtin sleep, just in case.
alarm 0;
print "$_\n";
}

This one loops forever, but I don't see why it does, and why your
example cannot see the REDO scope. Maybe closures are buggy here?
 
J

Jeff 'japhy' Pinyan

#!/usr/bin/perl
use strict;
use warnings;

my $rdo = sub { goto REDO; };

REDO:
foreach(1...1000) {
print "$_ ... ";
$SIG{ALRM}= $rdo->();

THIS calls &$rdo, which does 'goto REDO'. You just called the function
here, which was your mistake.
 
M

Michele Dondi

my $rdo = sub { goto REDO; };

REDO:
foreach(1...1000) {
print "$_ ... ";
$SIG{ALRM}= $rdo->();
alarm 10;
system "sleep $_"; # don't use builtin sleep, just in case.
alarm 0;
print "$_\n";
}

This one loops forever, but I don't see why it does, and why your
example cannot see the REDO scope. Maybe closures are buggy here?

What does this have to do with closures? Do you see any closure?!?
You want

$SIG{ALRM}= $rdo

instead!


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top