Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000289]
and snow settles gently...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2016-12-06 22:27 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (118 votes). Check out past polls.