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).


In reply to Re^2: maddening system call gzip/gunzip problems by graff
in thread maddening system call gzip/gunzip problems by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.