(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).
In reply to Re^2: maddening system call gzip/gunzip problems
by graff
in thread maddening system call gzip/gunzip problems
by neilwatson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |