Throwing an exception via croak
in a forked child process seems to print the error as a background process would. That is, it clobbers the shell prompt.
If I die
instead of croak
, the the error message pops up as a foreground process. I've trying to find out why that is in the Carp
documentation without any luck.
Here's what I mean. The croak
version:
$ perl Wrapper.pm
$ error: ... does not exist at Wrapper.pm line 624
The die
version:
$ perl Wrapper.pm
error: ... does not exist at Wrapper.pm line 515.
I tried trapping the fork
and printing $@
to STDERR and exiting, but that didn't have an effect. Any ideas? I'd like to be able to use croak
in this particular case.
Although my code is quite a bit more convoluted, here is how you can reproduce this behavior:
$ perl -MCarp -e 'unless (fork) {croak "child"}'
$ child at -e line 1
<- cursor blinking here. Pressing enter gives me a new prompt:
$
$ perl -e 'unless (fork) {die "child"}'
child at -e line 1.
$
Solved: cjm got it:
$ perl -e '$SIG{__DIE__} = sub {sleep 1}; unless (fork) {die "child"}'
$ child at -e line 1.
Thanks for the help!