OK, here is the expanded version of the original OP's oneliner, following
tye's
explanation:
cat fatals.pl
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
my $cmd = shift || '_bad_exe_ a b c';
print "I'm parent with id $$\n";
eval {
my $pid = open CMD, "-|";
if ($pid) {
print "I'm forking a child with id $pid\n";
print while <CMD>;
}
else {
#no warnings; # get rid of warnings on "Can't exec:..."
print "[$$] I'm child, about to exec [$cmd]\n";
exec $cmd;
}
};
die "[$$] ERROR: $@" if $@;
print "[$$]HOW DID I GET HERE?\n";
Run with correct command (just as comparisson):
$ perl fatals.pl ls
I'm parent with id 6238
I'm forking a child with id 6239
[6239] I'm child, about to exec [ls]
642663.pl
642676.pl
642682.pl
644761.pl
No problem there. Now, run with no args (default command):
$ perl fatals.pl
I'm parent with id 6240
[6241] ERROR: Can't exec "_bad_exe_": No such file or directory at fat
+als.pl line 18.
I'm forking a child with id 6241
[6241] I'm child, about to exec [_bad_exe_ a b c]
[6240]HOW DID I GET HERE?
So I guess, it's the child that triggers the "Can't exec ..." warning that gets "upgraded" into fatal error (due to FATAL), trapped in $@, and dies. The child dies, the parent doesn't, that's
HOW DID I GET HERE, I suppose. OTOH, if the
no warnings; is uncommented, then well, no "Can't exec..." warning, no $@ trapping, and the child lives pretending nothing happens, yet covering up by asking
HOW DID I GET HERE.
Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
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).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.