use PerlIO::gzip; open my $in_fh => "<:gzip", 'input.gz' or die "ack: $!"; open my $out_fh => ">:gzip", 'output.gz' or die "ack: $!"; while(<$in_fh>) { do_stuff($_) if /matches some condition/; print {$out_fh} $_; }