CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Why is it not printing instantly

 



oscarjiao
Novice

Feb 18, 2010, 1:23 PM

Post #1 of 5 (585 views)
Why is it not printing instantly Can't Post

This script handles a large number of file, and prints out some result for each of them.


Code
#!/usr/bin/perl 

if ($#ARGV != 5)
{
die "Usage x0_amber traj_name frame1 frame2 r0 r1 dr\n";
}

$traj=$ARGV[0];
$frame1=$ARGV[1];
$frame2=$ARGV[2];
$r0=$ARGV[3];
$r1=$ARGV[4];
$dr=$ARGV[5];
print "frame $frame1-$frame2\n";

$no_frm=$frame2-$frame1+1;
for($r=$r0;$r<=$r1;$r=$r+$dr)
{
foreach $i($frame1..$frame2)
{
open(PDB,"$traj.$i");
@line_pdb=<PDB>;
close PDB;
for ($j=0;$j<=$#line_pdb;$j++)
{
#calculation skipping
print .....
}
}
$x0=$n_count/$no_frm;
print "$r $x0\n";
}


If I ask it to write to the screen, it prints out right away. But if I redirect the output to a file:
x0_amber.pl tra.pdb 1001 10000 1 5 0.1 > x0_out &
it doesn't print out until the program reaches the end and prints out everything at once.

When I do "grep WAT * > wat.log", the wat.log gets updated immediately. Why can't my script print out to a file instantaneously?


FishMonger
Veteran / Moderator

Feb 18, 2010, 1:30 PM

Post #2 of 5 (584 views)
Re: [oscarjiao] Why is it not printing instantly [In reply to] Can't Post

Disable buffering by adding this near the top.


Code
$|++;



oscarjiao
Novice

Feb 18, 2010, 2:01 PM

Post #3 of 5 (581 views)
Re: [FishMonger] Why is it not printing instantly [In reply to] Can't Post


In Reply To
Disable buffering by adding this near the top.


Code
$|++;



Thanks.

If I did not disable buffering and the program has already run for a while, is there a way to write the buffer to the file in the middle?


(This post was edited by oscarjiao on Feb 18, 2010, 2:11 PM)


FishMonger
Veteran / Moderator

Feb 18, 2010, 3:21 PM

Post #4 of 5 (576 views)
Re: [oscarjiao] Why is it not printing instantly [In reply to] Can't Post

See: `perldoc -q flush`
http://perldoc.perl.org/perlfaq5.html#How-do-I-flush/unbuffer-an-output-filehandle?--Why-must-I-do-this?


7stud
Enthusiast

Feb 19, 2010, 6:22 AM

Post #5 of 5 (562 views)
Re: [oscarjiao] Why is it not printing instantly [In reply to] Can't Post

You mean change the code of a program while it is running?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives