in reply to Re: How can I create an array of filehandles?
in thread How can I create an array of filehandles?
What i'm doing is actually a hash of file handles, but it works the same way .. I'll format this example to use an array. Each part is spread out in individual sections so you can open and close and print anywhere you choose:
foreach $x (0.. 10) { $handles$x = anon_fh() } # other code here # open handles foreach $fh (@handles) { open ($fh, "file.txt"); } # code here?? foreach $fh (@handles) { # stuff .. print { $fh } "Text wil do into selected file"; # stuff } # more code?? foreach $fh (@handles) { close ($fh); } sub anon_fh { local *FH; return *FH; }Doing print { $handle4 } "Testing" should work to. Something I've learning is that file handles stored in a variable (hash, array, or scalar) needs to be used in a block context {}. I'm not exactly sure why, but problems happen when I don't use the blocks. So I use them. Well.. Just found out that scalars don't have to be in a block, but arrays and hashes do (just did a few perl tests while I was writting this ;). Exact reason why.. I still don't know, I'm just telling you from the errors I got ;) -- philip
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: RE: Answer: I need an array of filehandles.
by guice (Scribe) on Apr 25, 2000 at 09:25 UTC | |
by Anonymous Monk on Apr 25, 2000 at 10:12 UTC | |
by Aighearach (Initiate) on Apr 25, 2000 at 10:16 UTC |