Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Perl prints only one line when directed to a file but print to a screen works

by Happy-the-monk (Monsignor)
on Jun 30, 2013 at 17:17 UTC ( #1041623=note: print w/ replies, xml ) Need Help??


in reply to Perl prints only one line when directed to a file but print to a screen works

foreach my $listDevices ($device_list->result()) { ... open OVERWRITE, ">overwrite.txt" or die $! ... close OVERWRITE; ... }

Do you see what is happening?

You have even called the filehandle "OVERWRITE".

The file gets overwritten every time the loop and if directive come by the call to open

Solution: place both the open and close outside of the foreach loop.

open goes first, then the loop, and when that's done, close the filehandle.

Cheers, Sören

(hooked on the Perl Programming language)


Comment on Re: Perl prints only one line when directed to a file but print to a screen works
Download Code
Re^2: Perl prints only one line when directed to a file but print to a screen works
by vlad3848 (Acolyte) on Jun 30, 2013 at 17:52 UTC
    Thank you Sören!

Log In?
Username:
Password:

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

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

    Is guessing a good strategy for surviving in the IT business?





    Results (48 votes), past polls