jpfarmer has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Storing open filehandles in a hash
by holli (Abbot) on Aug 16, 2006 at 14:21 UTC | |
|
Re: Storing open filehandles in a hash
by ikegami (Patriarch) on Aug 16, 2006 at 14:37 UTC | |
by kovacsbv (Novice) on Nov 20, 2008 at 14:59 UTC | |
by ikegami (Patriarch) on Nov 20, 2008 at 22:14 UTC |