edwardslab

  • Increase font size
  • Default font size
  • Decrease font size
Home Ramy Perl tips: how to read a .gz file

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

Comments (2)
  • admin
    Instead of trusting the file name, you could actually test if it is a .gz file:
    if(`gzip -t $file` == 0) { ... }

  • ramy  - Thanks
    Thanks, Admin :idea:
Only registered users can write comments!

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."