Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Happy-the-monk (Abbot)
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

Replies are listed 'Best First'.
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?
[hippo]: It's always good to start the day with a laugh and Re^2: Subroutine processes only one file in the directory has genuinely made me do so. Well done, sridhar56!
[Discipulus]: :=)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2017-05-26 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?