Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

pipe

perl

zcat

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(FILE, "dummy.gz") or die "DIE";

$ ./test2.pl    
DIE at ./test.pl line 2.
like image 984
dogbane Avatar asked Dec 22 '22 22:12

dogbane


1 Answers

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")
like image 108
Hasturkun Avatar answered Jan 10 '23 06:01

Hasturkun