expatmark has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem with printout in a small script I have written. It's something I have never seen before and I cannot see why it is happening.

The relevant code is as follows. (Everything before it is just input gathering)

for $station (@stations){ print "\n $station"; print OUTFILE "\n$station"; undef %found; splice @found; for $stage (@stages){ chdir "/opt/stc/db/$station/$stage/prod/doc"; @files = <MO*>; for $file (@files){ open HTMFILE, "< $file"; for $line (<HTMFILE>){ if ($line =~ /${monobj}/ && $line =~ /HREF/ && $line ! +~ /TOP/){ $found = $stage,$file"; $found{$found} = (split /-/, $line)[0]; } } close HTMFILE; } } @found = sort (keys %found); for $found (@found){ ($stage) = (split /,/, $found)[0]; print "\t- $stage - $found{$found}\n"; print OUTFILE ",$stage,$found{$found}\n"; } print "\n" if ($found[0] eq ""); print OUTFILE "\n" if ($found[0] eq ""); }
Any print statement within the 'for $station ...' loop is withheld until the start of the 'for $found ...' sub loop. So the line print "\n    $station"; right at the top does not print until the start of this last sub loop.

It's not a major issue. The printout looks exactly how I want it but it is bugging me. Since the @files can contain between 2000 and 3500 entries, it would be nice to have the value of $station printed out where I put it as this would give a better progress indication.

All suggestions gratefully received and tried.

Replies are listed 'Best First'.
Re: Not printing when expected.
by no_slogan (Deacon) on May 26, 2014 at 13:45 UTC
Re: Not printing when expected.
by Laurent_R (Canon) on May 26, 2014 at 21:46 UTC
    no_slogan said it all, or almost, but I wish to add that when you print to standard output (STDOUT), you'll get your output immediately provided that you have a newline character at the end, which is what is lacking in your program. If you had:
    print "\n $station \n";
    instead of what you have:
    print "\n $station";
    you would see the result immediately because printing to the terminal is line-buffered (meaning that, by default, it is printed as soon as a newline character is printed out). Printing to a file is a different story, it will be delayed by default, but this is very well explained in Mark-Jason Dominus' article referred to by no_slogan.