Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 Answer: How can I create an array of filehandles?
in thread files

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:

      PRE:
      $array$i
      
      CODE:
      $array[$i]

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2019-12-15 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?