in reply to Re^2: Using backquotes to echo results in "Bad file descriptor"
in thread Using backquotes to echo results in "Bad file descriptor"

Did you read error indicators? Essentially (as gwadej and toolic as also pointed out) you are not checking the error code you think you are. $! is set by the perl C libraries, not by external system calls, and its value is essentially meaningless unless a Perl library call (like open) just failed. On the other hand $? is set to the return value of the invoked system call, and as such is what you need to check in the case of backticks (or system).
  • Comment on Re^3: Using backquotes to echo results in "Bad file descriptor"

Replies are listed 'Best First'.
Re^4: Using backquotes to echo results in "Bad file descriptor"
by motionblurrr (Initiate) on Mar 04, 2009 at 19:03 UTC
    Yes, I realize now that I'm not doing my error checking correctly by using $! but my next question (read: obsession) is why do the perl C libraries set errno to "Bad file descriptor" whenever perl executes a shell command using backquotes that sends data to STDOUT?

    Consider this:

    perl -e '$res = `echo test`; print $!."\n";'

    versus this:

    perl -e '$res = `echo test>/dev/null`; print $!."\n";'

    In the first example, $! is "Bad file descriptor". In the latter example, $! is empty. So whatever the answer is, it has something to do with whether or not the command has output. You can replace "echo test" with any shell command that produces STDOUT info.

      Try using strace to find out.

      Nevermind, I just did, and I don't see a system call returning that error.

      Not entirely sure, but an interesting test case I found is the following:

      perl -e 'print `echo test`; print $!."\n";'

      shows $! to be empty. I am guessing the issue is that echo is attempting to output to a non-existent terminal, and this error is a result of an internal perl call to determine if the output is being piped to an existing terminal. Note that

      perl -e 'print $res = `echo test`; print $!."\n";'

      does yield $! eq "Bad file descriptor".

        So... when perl is outputing the data using print, it doesn't cause the error, but when it's sending it to a scalar, it isn't really a terminal, so it causes $! to have that error. I can go with that. Makes a lot of sense actually. Thank you!