Perl tips: how to read a .gz file

Well, and since I’m in a blogging mode, here is one more thing I didn’t know about Perl (I can easily count the things I know!):

Question: Can Perl read a .gz file?

Answer: Of course. Ask the right question now.

Question: How can Perl read a .gz file?

Answer: Still not good enough a question…

Question: What is at least one way for Perl to read a .gz file?

Answer: Try:

if ($file =~ /.gz$/) {
open(IN, “gunzip -c $file |”) || die “can’t open pipe to $file”;
else {
open(IN, $file) || die “can’t open $file”;

while () {

Answer Credit: Rob Edwards