Is there a way to create anonymous memory buffers and store them in a hash and then write to them and finally flush the buffers to the hard drive? Basically what I'm doing is creating a basic script that reads in a log file and parses it out in to separate files based on an identifier. There will normally be hundreds of identifiers and I found that writing out each line in the foreach(@lines) is very inefficient, since it has to constantly open and close files to write a single line. So my thought was to create a hash of buffers using the identifier as the key and then store a handle to a memory buffer and write to that then flush all the buffers. I wrote the following code to do that, but I seem to me having issues with creating and writing to the buffers. I thought I could handle them just like a file handle, but I'm getting run time errors on trying to write to the buffer:
Can't call method "write" on an undefined value at z:\workspace\pn\pn.pl line 67, <GEN0> line 1
It sounds like the interpreter can't tell what is in the hash value and it needs to be cast to something, but I'm not familiar with Perl to really tell.
Sorry for the newb question, and thanks for any help.
-Zipper
Can't call method "write" on an undefined value at z:\workspace\pn\pn.pl line 67, <GEN0> line 1
It sounds like the interpreter can't tell what is in the hash value and it needs to be cast to something, but I'm not familiar with Perl to really tell.
Code:
sub breakLogThreads(){
my $filename = $_[0];
my $inFileHandle = new FileHandle;
$starttime = time();
$oldDelim = $/;
undef $/;
$inFileHandle->open($filename) or die "Could not open file: " . $filename;
$var = <$inFileHandle>;
$/ = $oldDelim;
my @lines = split /$oldDelim/, $var;
%bufHash;
foreach (@lines){
$line = $_;
if ($line =~ m#\d{4}\/\d{2}\/\d{2} .*? (.*?) (.*?) .*#){
if (!exists $bufHash{$2}){
open(MEMORY,'>', $bufHash->{$2});
}
$bufHash->{$2}->write($_);
$bufHash->{$2}->close();
}
}
print "Took: " . (time() - $starttime) . " Seconds";
for my $key( keys %hash){
open(MYOUTFILE, ">>c:\\logs\\temp\\T" . $key . ".log") or die "I failed";
print MYOUTFILE $bufHash{$key};
close(MYOUTFILE);
}
$inFileHandle->close();
}
Sorry for the newb question, and thanks for any help.
-Zipper