On 4/6/06, John W. Krahn <[EMAIL PROTECTED]> wrote: snip > > use POSIX ":sys_wait_h"; > > > > my $handler = sub { > > my $pid; > > while (($pid = waitpid(-1, &WNOHANG)) > 0) { > > # do something with $pid > > } > > $SIG{CHLD} = $handler #reinstall the handler > > When perl compiles this it will use $main::handler for the assignment. snip
Yes, it will. And since $main::handler holds the anonymous subroutine that I want reinstalled that is good. What I really want though is { #setup the auto-reaper for fork'ed children my $handler = sub { my $pid; while (($pid = waitpid(-1, &WNOHANG)) > 0) { # do something with $pid } $SIG{CHLD} = $handler; #reinstall the handler } $SIG{CHLD} = $handler; } since that completely avoids polluting the namespaces, but I didn't think of it until after I had typed it and was too lazy to reindent the code (damn web mail and it's treating tabs as "next field"). You may wonder why I would bother reinstalling the handler at the end. The reason is that old SysV machines are broken wrt signal handlers and the CHLD signal. >From perlipc: You might also want to employ anonymous functions for sim- ple signal handlers: $SIG{INT} = sub { die "\nOutta here!\n" }; But that will be problematic for the more complicated han- dlers that need to reinstall themselves. Because Perl's signal mechanism is currently based on the signal(3) func- tion from the C library, you may sometimes be so misfortu- nate as to run on systems where that function is "broken", that is, it behaves in the old unreliable SysV way rather than the newer, more reasonable BSD and POSIX fashion. So you'll see defensive people writing signal handlers like this: sub REAPER { $waitedpid = wait; # loathe sysV: it makes us not only reinstate # the handler, but place it after the wait $SIG{CHLD} = \&REAPER; } $SIG{CHLD} = \&REAPER; # now do something that forks... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>