tags:

views:

307

answers:

1

Hello,

I'm doing something like the following:

I run a Perl script which has the following:

# First i install a signal handler for HUP which sets a global flag.
$SIG{"HUP"} = sub { print "HUP received\n"; $received_hup = 1 };
# Now i wait for HUP to be received.
my $cnt = 0;
for ($cnt = 0; $received_hup != 1 and $cnt < 900; $cnt++) {
   sleep(1); 
}
print ($received_hup == 1) ? "true" : "false";

Then I send HUP to this perl process.

I find that sometimes false is printed although every time "HUP received" is also printed; i.e. although the signal handler is invoked, the global variable is not modified.

I'm not familiar with concurrency issues in Perl, so please guide me with this.

+5  A: 

Well, first off, its not actually possible for your program to print "false", because you're missing a pair a parentheses in the print:

print (($received_hup == 1) ? "true" : "false");

Without that extra pair, its parsed as:

(print($received_hup == 1)) ? "true" : "false";

which is clearly not what you want.

That test program — modified with the correct parentheses — works just fine here, on Debian Linux w/ perl 5.10. I tried it over 100 times and each time it printed "HUP received" and "true". If that exact test program is not working for you, I think you just have a buggy version of perl. Older versions of perl, before 5.8 I believe, did have signal handling issues.

derobert
You might add there would have been a warning given if warnings had been enabled.
ysth