Recently, I was working on a program that takes one input file and splits it into n other files depending on the content of each line. I figured the most efficient way to do this would be to keep all of my filehandles in a hash, so I wrote the following:
while (<>){
# do some stuff
if (defined $fhs{$conn}){
local *FH = $fhs{$conn};
print FH $_;
} else {
local *FH;
open(FH, '>', "conn-$conn") or die($!);
print FH $_;
$fhs{$conn} = \*FH{IO};
}
}
I seem to be able to build up the file handles the way I expected, but by the time I get to the if block, the filehandle is closed. Is there a way I can keep it open? or is there a better way to do this?
Thanks!