in reply to Bad file descriptor when using backticks?

Checking $! for errors is never a good idea unless you're sure the last syscall produced an error. $! may have any kind of value if the last syscall did not produce an error or is not documented to set $!.

As far as I can see the only way to check for this case is to see if the `` operator returned undef:

my $hostname = `hostname`; unless (defined $hostname) { print "found error of: $!\n"; }
update: as Fletch mentioned, `` does not set $! but $? so this code doesn't really work.

In addendum: be aware that not all functions that actually set $! are documented as doing so in the man pages. The only source that I know that seems to mention all of them is the Camel book.

Replies are listed 'Best First'.
Re^2: Bad file descriptor when using backticks? (yes, $!)
by tye (Sage) on Feb 14, 2008 at 18:22 UTC

    Your answer was better before the update. qx() can set $! if there was a failure in the parent process. But qx() does always set $? and you need to look at $? in order to figure out if $! was also set. Despite not finding this in the documentation at the moment, I believe that $? being -1 means that $! should be checked / reported.

    - tye