someuser
someuser

Reputation: 2309

Perl loop not printing out character

This one has got me.

This works:

print "Processing Feed\n";
while ( my @p = $mainex->fetchrow_array ) {
    my $iis = "$pcount";
    print "$iis\n";
    # ... Do Other Stuff Here
    $pcount++;
}

Which gives:

Processing Feed
1
2
3
4
5
6
7
8
9
10
...

This does not work (removed the \n from line 4):

print "Processing Feed\n";
while ( my @p = $mainex->fetchrow_array ) {
    my $iis = "$pcount";
    print "$iis";
    # ... Do Other Stuff Here
    $pcount++;
}

Which simply gives:

 Processing Feed

I was trying to build a counter that would output the count of the record it was up to using something like:

while( Something ){
    print "\b\b\b\b\b\b\b\b\b\b\b";
    print "$count";
    $count++;
    # Do stuff here
}

Any ideas why when there is no \n in the second example nothing is printing to screen? I have done it many times before and cannot figure out why it is not working.

Upvotes: 2

Views: 245

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754410

Buffered I/O.

The data is flushed to the screen when there's a newline, or when the buffer is full (which may be 512 bytes or 4096 bytes or some other fairly substantial number).

Upvotes: 4

Christopher Neylan
Christopher Neylan

Reputation: 8272

The newline at the end of the print triggers a flush of stdout, which prints to the screen. If you add $|++ to the top of your perl script, it will turn on autoflushing for stdout and you will see your numbers.

Upvotes: 6

Related Questions