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

hi, i'm trying to open and read a .gz file.....any ideas on how to do this? i'm trying the following but does not seem to work....
open file.gz; while (<file.gz>) { }

Replies are listed 'Best First'.
Re: Opening and reading ,gz file
by dws (Chancellor) on Aug 22, 2002 at 19:47 UTC
    i'm trying to open and read a .gz file.....any ideas on how to do this?

    Try

    my $file = "file.gz"; open(IN, "gzcat $file |") or die "gunzip $file: $!"; while ( <IN> ) { ... } close(IN);
    See perldoc -f open for more details.

    You might want to specify the full patch to gzcat if there's a possibility that someone might sneak a trojan horse into your PATH.


    Update: s/gunzip/gzcat/

      Remember to use the '-c' flag or you will just uncompress the file and get no input at all.

      open(IN, "gunzip -c $file |") or die "gunzip $file: $!";

      bluto

Re: oepning and reading ,gz file
by sauoq (Abbot) on Aug 22, 2002 at 19:50 UTC
    See Compress::Zlib if you want a module for this sort of thing.
    -sauoq
    "My two cents aren't worth a dime.";
    

      Better yet, see IO::Zlib, which lets you use a simple filehandle interface.

      use IO::Zlib; #requires Compress::Zlib, of course my $fh = IO::Zlib->new("foo.gz"); while (<$fh>) { go_nuts(); }



      If God had meant us to fly, he would *never* have given us the railroads.
          --Michael Flanders

•Re: Opening and reading .gz file
by merlyn (Sage) on Aug 22, 2002 at 20:41 UTC
Re: Opening and reading .gz file
by cosmicsoup (Beadle) on Aug 22, 2002 at 21:53 UTC
    Try this: open(GZIN, 'gzcat file.gz|') ; while (<GZIN>) { }