hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hello again !

I execute a shell command using the following mode of open:
open(CMD, "$command 2>&1 |") or warn "Can't open $command for reading: + $!"; my @cmdOutput = <CMD>; close(CMD) or warn "Can't close $command: $!/$?";
The problem is that sometime (not consistent) I get the warning from the close function above, even if the procedure succeeded, anyone for the rescue?

Thanks.

Hotshot

Title edit by tye

Replies are listed 'Best First'.
Re: open()
by ferrency (Deacon) on May 06, 2002 at 14:09 UTC
    From perldoc -f close:

    If the file handle came from a piped open close will additionally return false if one of the other system calls involved fails or if the program exits with non-zero status. (If the only problem was that the program exited non-zero $! will be set to 0.)

    So, if your $command returns a non-zero exit status, close(CMD) will return 0. Unless you're really paranoid, or your specific case warrants extra care, you typically don't need to worry very much about whether close() succeeds or not. But if you do care, then you can check $!: if it's 0, then your $command returned a non-zero exit code, as stated in the perldoc above.

    Alan

Re: open()
by hotshot (Prior) on May 06, 2002 at 14:05 UTC
    more info:
    The warning I get is:

    Can't close $command: /256 at /blah/blah/blah.pl line xxx.

    Thanks.

    Hotshot

      Your command returned exit status 1 (e.g., a Perl script might have done exit 1; or similar). See the documentation of $? for more details of this variable. Consult the documentation for <samp>$command</samp> to know what went wrong.