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

So, I just can't seem to get my code to recognize the fact that certain files are not present on the filesystem. This code:

$cmd = 'gzcat /home/me/file.zip.gz |'; open(IN,$cmd) or die print "oops";

So, the issue is that if file.zip.gz is not present, i get a message: "gzip: /home/me/file/zip.gz: No such file or directory"

I've tried eval{ open(IN,$cmd) }; and have tried my own $SIG{PIPE} handler, but this seems to be more of "file not found" rather than a broken pipe.

Any help greatly appreciated. -Monkified

Replies are listed 'Best First'.
Re: Pipe Error Trapping
by herveus (Prior) on Aug 24, 2004 at 16:24 UTC
    Howdy!

    The error message is coming directly from gzcat. The open() call succeeded.

    If you need to verify the existence of the file, check that before you open the pipe. e.g.:

    my $file = '/home/me/file.zip.gz'; die "no such file '$file'" unless -f $file; my $cmd = "gzcat $file |"; open(IN, $cmd) or die "open failed: $!";

    yours,
    Michael
Re: Pipe Error Trapping
by dpuu (Chaplain) on Aug 24, 2004 at 16:13 UTC
    When opening a command to a pipe, the open command will only fail if the command you're trying to run (gzcat) doesn't exist. You need to check if the command found an error, after it started running.

    The solution is to check the return value of other functions -- specifically, the close function:

    open IN, "cat foo|" or die "failed to open: $!"; while (<IN>) { print } close IN or die "failed to close: $!";
    --Dave
    Opinions my own; statements of fact may be in error.
A reply falls below the community's threshold of quality. You may see it by logging in.