Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to RE: Answer: I need an array of filehandles. by Anonymous Monk
in thread files by vroom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2019-12-15 16:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?