Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

File contents not updated properly

by balakrishnan (Monk)
on Dec 09, 2008 at 05:19 UTC ( #729089=perlquestion: print w/ replies, xml ) Need Help??
balakrishnan has asked for the wisdom of the Perl Monks concerning the following question:

open(FH,">Sample");
$|=1;
while(1) {
print FH "This is for testing\n";
sleep 1;
}
This is to write some contents into the "Sample" file for every 1 second. After running this program, the file "Sample" is not updated.
what is the correction, it requires?

Thanks,
balakrishnan

Comment on File contents not updated properly
Re: File contents not updated properly
by sathiya.sw (Monk) on Dec 09, 2008 at 05:27 UTC
    You need to select the file handle as,
    select(FH);
    open(FH,">Sample"); select(FH); $|=1; while(1) { print FH "This is for testing\n"; sleep 1; }
    Sathiyamoorthy

      Hi,

      First of all, don't assume that you could open the file, check for it. Then use 3 argument open, and don't use a typeglob.

      open my $fh, '>', $filename or die "Can't open $filename: $!";

      The solution provided will leave the default handler to be *FH, which is not desired, meaning all prints, etc will use *FH; alternatives:

      # not so maintainable, but still used frequently select((select($fh), $|++)[0]); # better use IO::Handle; $fh->autoflush(1);

      Check out the link about buffering provided...

      Regards,

      fmerges at irc.freenode.net
Re: File contents not updated properly
by dharanivasan (Scribe) on Dec 09, 2008 at 05:55 UTC
    use strict; use warnings; use FileHandle; open(FH,">Sample"); $|=1; FH->autoflush(1); while(1) { print FH "This is for testing\n"; sleep 1; } close(FH);
Re: File contents not updated properly
by apl (Monsignor) on Dec 09, 2008 at 11:37 UTC
Re: File contents not updated properly
by Dhineshkumar (Initiate) on Jan 05, 2009 at 07:15 UTC
    Using IO::Handle, We can flush the data whenever we need.
    Like below :
    use IO::Handle;
    open(FH,">Sample");
    while(1) {
    print FH "This is for testing\n";
    FH->flush();
    sleep 1;
    }
Re: File contents not updated properly
by Dhineshkumar (Initiate) on Jan 05, 2009 at 07:18 UTC
    When we are execute some commands in perl, Automatically file content got flush.
    Like below :
    open(FH,">Sample");
    while(1) {
    print FH "This is for testing\n";
    sleep 1;
    `date`; -- Sample command
    }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-07-30 00:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls