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!
I’m pretty sure it’s just a timing issue. The
dieversion is slightly faster, so it has a better chance of outputting the error message before the shell can print the next prompt. When I try running your examples, thecroakversion usually gets printed after the prompt, but occasionally it comes before the prompt. Thedieversion pretty consistently comes before the prompt.