in reply to maddening system call gzip/gunzip problems

There are easier ways to do certain parts of your existing program. If you want to find all gzip'd files in a directory, use File::Find:
use File::Find; my $dir = "/home/lprjob1/filest" find( sub { push @files, $File::Find::name if -f && /.+gz/ }, $dir );

Then I would look into using some of the CPAN modules associated with gzip like Tie::Gzip. If the files you are looking to process are text files, you can tie them to a gzip'd filehandle and read them:

use Tie::Gzip; for my $log (@files) { tie *LOG, 'Tie::Gzip'; open (\*LOG, '<', "$log") or die("Cannot open $log: $!"); while (my $line = <LOG>) { # Iterate over the files here and do blah } close (LOG); untie *LOG; }

Replies are listed 'Best First'.
Re^2: maddening system call gzip/gunzip problems
by graff (Chancellor) on Dec 08, 2006 at 02:09 UTC
    If you want to find all gzip'd files in a directory, use File::Find:

    (sigh) No. Stick with readdir, like the OP wants, but use it with grep. And as for actually handling the data, my favorite is PerlIO::gzip:

    my @target_files; my ( $imode, $omode ); opendir DIR, $dir or warn "Cannot open $dir $!"; if ( $cmd eq 'gzip' ) { @target_files = grep { !/\.gz$/ and -f "$dir/$_" } readdir DIR +; $imode = "<"; $omode = ">:gzip"; } else { @target_files = grep /.\.gz$/, readdir DIR; $imode = "<:gzip"; $omode = ">"; } closedir DIR; for my $ifile ( @target_files ) { my $ofile; if ( $cmd eq 'gzip' ) { $ofile .= ".gz"; } else { $ofile =~ s/\.gz$//; } open( I, $imode, $ifile ) or die "$ifile: $!"; open( O, $omode, $ofile ) or die "$ofile: $!"; while(<I>) { print O } close I; close O; unlink $ifile; }

    (update: Having read the later replies after I posted this, I should point out that this approach (using grep with readdir) avoids the problem of modifying the directory contents before being completely done with readdir -- we get all the files into an array first, then work on the files. I did update my code snippet to put an explicit "closedir" before the "for" loop, to clarify this point.)

    UPDATE: (2010-10-18) It seems that PerlIO::gzip should be viewed as superseded by PerlIO::via:gzip. (see PerlIO::gzip or PerlIO::via::gzip).