Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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 How can I create an array of filehandles? by Punto

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2023-01-30 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?