Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

RE: Answer: I need an array of filehandles.

by Anonymous Monk
on Apr 25, 2000 at 09:21 UTC ( #8897=note: print w/replies, xml ) Need Help??

in reply to Re: How can I create an array of filehandles?
in thread How can I create an array of filehandles?

Here's another way of doing this as well ..

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
  • Comment on RE: Answer: I need an array of filehandles.

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
    That was actually me (guice) that posted that example.. I appears that this form is dropping my handle :( BTW .. the { $handle4 } is suppose to be: { $handle[4] } .. Appears the form doesn't like [ and ] .. It made a link out of it.. oops ;)

      I glanced at the page source, and saw you used a PRE tag, maybe a CODE tag works better?
      Here, lets test:


      Hmmm, I wish I could preview this sucker before I submit...

        It worked, but it's odd I wasn't logged in... it's got a cookie saved.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://8897]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2023-09-30 16:50 GMT
Find Nodes?
    Voting Booth?

    No recent polls found