in reply to Close() sometimes fails after open() pipe from command

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