bwilli27 has asked for the wisdom of the Perl Monks concerning the following question:
Hard to come up with a concise title, but here's the problem I am trying to solve. Given a directory of zipped log files, I need to perform the following for each file. Each zip file contains a single structured server log file with \r\n trailing each log entry.
*Decompress the file to something in memory, currently using a scalar
*Treat the scalar as if reading a file for input; read line-by-line in order to do something with each line
I am using IO::Uncompress::Unzip to decompress the zip files. Here's a snippet of my code.
use IO::Uncompress::Unzip qw(unzip $UnzipError); my $logDirectory = '/logs'; foreach my $zipFile (glob("$logDirectory/*.zip")) { my $output; print "Decompressing $zipFile to memory\n"; unzip $zipFile => \$output; #print $output; open my $fh, '<', \$output or die $!; while (<$fh>) { #do something } close $fh or die $!; }
The code within <$fh> read loop is never reached. If I print the content of $output after unzipping the file, the correct log data is displayed. I have alternatively tried various methods of splitting $output based on '\r\n' but I believe I'm reaching Perl size limitations; the unzipped data is roughly 2.1GB and Perl bombs on either 'split loop' error or some type of panic.
I am using perl 5.14 on a 64-bit linux machine with 64GB memory. The problem may seem odd, however I am trying to optimize the processing of thousands of compressed server log files. My 'old' perl script writes the decompressed log file to disk, reads that file for processing, and moves on to the next zip. Optimally I want to keep the decompressed content in memory and process the data, only writing to disk the log entries that match my search criteria.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unzip file to scalar, treat scalar as a file
by pmqs (Friar) on May 15, 2014 at 22:10 UTC | |
|
Re: Unzip file to scalar, treat scalar as a file
by Laurent_R (Canon) on May 15, 2014 at 21:39 UTC | |
|
Re: Unzip file to scalar, treat scalar as a file
by Anonymous Monk on May 15, 2014 at 20:16 UTC | |
|
Re: Unzip file to scalar, treat scalar as a file
by jmacloue (Beadle) on May 16, 2014 at 15:14 UTC |