Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Opening and closing files in loop

by BrowserUk (Pope)
on Oct 22, 2012 at 08:30 UTC ( #1000289=note: print w/ replies, xml ) Need Help??


in reply to Opening and closing files in loop

Is there a restriction in opening and closing files inside a loop?

You aren't opening the files within the loop.

select only makes a particular, already open, filehandle the default for print.

But you are closeing those handles on the first pass, and they never get re-opened.

Two possibilities:

  1. Don't close the files, just select between then:
    for ($i=1;$i<=$max;$i++){ $pwd = &randomPwd($nmbchars); select (DB); print qq("$dept$w$i","7 days","$pwd",,,,,,\n); select (newDB); print qq("$dept$w$i"\t>\t"$pwd"\n); }
  2. Instead of selecting between them, supply the required file handle to the print statements:
    for ($i=1;$i<=$max;$i++){ $pwd = &randomPwd($nmbchars); print DB qq("$dept$w$i","7 days","$pwd",,,,,,\n); print newDB qq("$dept$w$i"\t>\t"$pwd"\n); }

Personally, I prefer the latter option.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong


Comment on Re: Opening and closing files in loop
Select or Download Code
Re^2: Opening and closing files in loop
by lt007 (Initiate) on Oct 22, 2012 at 08:42 UTC
    Thank you very much!!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-11-26 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (162 votes), past polls