开发者

Why doesn't my Perl pipe to zcat die if the file is not there?

开发者 https://www.devze.com 2023-01-21 16:08 出处:网络
If my gz file does not exist, why doesn\'t it DIE? $ cat test.pl open(FILE, \"zcat dummy.gz |\") or die \"DIE\";

If my gz file does not exist, why doesn't it DIE?

$ cat test.pl    
open(FILE, "zcat dummy.gz |") or die "DIE";

$ ./test.pl    
zcat: dummy.gz: No such file or directory

If I read a file normally, it works as expected:

$ cat test2.pl    
open(FI开发者_如何学JAVALE, "dummy.gz") or die "DIE";

$ ./test2.pl    
DIE at ./test.pl line 2.


Your open succeeds (as it successfully runs zcat), you won't get zcat's exit code until you close the file descriptor though.

You might want to check if the file exists and is readable before you start though, eg.

die "unable to read file" unless (-f "dummy.gz" and -r "dummy.gz")


You may want to consider using PerlIO::gzip, eg.

use PerlIO::gzip;
open(FILE, '<:gzip', 'dummy.gz') or die $!;


In your second example, the file opened by perl does not exist, so it will die.

In your first example, the command called by perl executes (with any result), so there is no reason to die.

0

精彩评论

暂无评论...
验证码 换一张
取 消