use strict; use warnings; use Archive::Tar; use File::Spec::Functions qw(catdir); my $input = "libpst.tar.gz"; my $tar = Archive::Tar->new($input); my @files = $tar->list_files; my $output_dir = 'path'; # change this foreach my $file (@files) { my $extracted_file = catdir($output_dir, $file); $tar->extract_file($file, $extracted_file); if ($file =~ /\.txt\z/) { # or whatever open my $fh, '<', $extracted_file or do { warn "$0: Can't open file $file: $!"; next }; while (defined(my $line = <$fh>)) { # do something with $line } close $fh; } }