From 4379aba5227f4a39ec9dd4c6d93903e388fcba38 Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Sat, 30 Mar 2019 16:47:59 -0700 Subject: [PATCH] stop using IO::Uncompress::Gunzip; it's slow --- postfix_grep | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/postfix_grep b/postfix_grep index da659bc..b10e5be 100755 --- a/postfix_grep +++ b/postfix_grep @@ -72,8 +72,6 @@ GetOptions(\%options, pod2usage() if $options{help}; pod2usage({verbose=>2}) if $options{man}; -use IO::Uncompress::Gunzip; - $DEBUG = $options{debug}; if (not exists $options{regex}) { @@ -102,12 +100,36 @@ if (not @ARGV) { push @ARGV,undef; } +sub open_compressed_file { + my ($file,$encoding) = @_; + $encoding //= ':encoding(UTF-8)'; + my $fh; + if (not defined $file) { + $fh = \*STDIN; + binmode($fh,':encoding(UTF-8)'); + return $fh; + } + my $mode = "<$encoding"; + my @opts; + if ($file =~ /\.gz$/) { + $mode = "-|$encoding"; + push @opts,'gzip','-dc'; + } + if ($file =~ /\.xz$/) { + $mode = "-|$encoding"; + push @opts,'xz','-dc'; + } + if ($file =~ /\.bz2$/) { + $mode = "-|$encoding"; + push @opts,'bzip2','-dc'; + } + open($fh,$mode,@opts,$file); + return $fh; +} + my %postfix_ids; for my $file (@ARGV) { - my $fh = IO::Uncompress::Gunzip->new(defined $file ? $file:\*STDIN, - MultiStream => 1, - Transparent => 1,) - or die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip::GunzipError"; + my $fh = open_compressed_file($file); while (<$fh>) { chomp; my $line = $_; -- 2.39.2