exit does not create a signal so nothing in %SIG will be called.
Inside a signal handler exit should behave similar to when called elsewhere, as it is possible for most signal handlers to choose not to exit they need to either exit or die themselves.
Under *nix try (press ctrl+c 5 times to exit)
perl -e 'my $ccc = 0; $SIG{INT} = sub { $ccc++; print "^C pressed $ccc
+ times\n"; exit 42 if $ccc >= 5 }; 1 while 1;'; echo $?
One way is to set a flag right after the fork based on what side of the fork you are on.
my $parentFlag;
my $pid = fork();
if ($pid) {
#parent process
$parentFlag= 1;
} elsif (defined $pid) {
#child process
$parentFlag= 0;
} else {
#error on fork
die "some useful message: $!\n";
}
END {
if ($parentFlag) {
#parent end code
} elsif (defined $parentFlag) {
#child end code
} else {
#end block after failed fork or exit before the fork?
}
}
Snippets of code should be wrapped in
<code> tags not<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).