One minor issue is that your regex,

$file !~ m/^[\w-]+(\.[\w-]+)*$/

will, if I read the regex correctly, consider this:
cns061206
to be acceptable: it's ending with at least zero strings comprising one dot followed by at least one word character or hyphen. That is, the (/\.[\w-]+)* portion of the regex is successfully matching a null-string. This, you could fix by replacing the final asterisk in the regex by a plus sign.

Something like this may do what you want (note: NOT TESTED)

sub zip { (my $cmd, my $dir) = @_[0,1]; my $count; if($cmd eq 'unzip') { foreach my $file (glob($dir . '/*.gz')){ # get only gzipped fi +les if(system(($cmd, $file)) == 0) { $count++; } else { warn "Could not $cmd $file\n"; } } return $count; } elsif($cmd eq 'zip') { foreach my $file (glob($dir . '/*')) { next if $file =~ /\.gz$/; next if $file =~ /\.txt$/; next unless $file =~ /(\.\w+)+$/; # which will skip files +without an extension if(system(($cmd, $file)) == 0){ $count++; } else { warn "Could not $cmd $file\n"; } } return $count; } else { warn "Unknown command $cmd\n"; } }

You could also check out Tie:Gzip, PerlIO::gzip, or IO::Uncompress::AnyInflate. Note that I've not used any of these, so I'm not endorsing any one of them.


NOTE: akin to what idsfa said about readdir, the glob calls should be probably be moved out of the foreach loop control statements. That is, replace:
foreach my $file (glob($dir . '/*')) {

and
foreach my $file (glob($dir . '/*.gz'))

with
my @list = glob($dir . '/*'); foreach my $file (@list) {

and
my @list = glob($dir . '/*.gz'); foreach my $file (@list) {

respectively.

emc

At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

—Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

In reply to Re: maddening system call gzip/gunzip problems by swampyankee
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.