Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Printing inside a loop

by no21 (Sexton)
on Feb 10, 2006 at 21:46 UTC ( [id://529484]=perlquestion: print w/replies, xml ) Need Help??

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

I wrote a simple FTP script that transfers several files to a web server. As a way of indicating to the user that the script is still running and still transferring files, I want to print a period to the screen each time a file has been transferred (sort of a poor man's progress bar).

To do that, I used the following code.
for(@files){ $ftp->put($_) || die("Couldn't put file:$_\n."); print "."; }
Yet, when the code runs, instead of printing a period each time a file has been transferred, it waits to print out all of the periods all at once after all of the files have been transferred.

(Not that this should matter, but I'm using ActivePerl on a Windows box.)

Why don't the periods print out one at a time to the screen?

Replies are listed 'Best First'.
Re: Printing inside a loop
by kutsu (Priest) on Feb 10, 2006 at 21:53 UTC

    Check out perlvar and the variable $|. Simply put setting $| = 1; should flush to STDOUT immediately instead of at the end of the program.

Re: Printing inside a loop
by Fletch (Bishop) on Feb 10, 2006 at 21:54 UTC
Re: Printing inside a loop
by GrandFather (Saint) on Feb 10, 2006 at 21:56 UTC

    Turn on autoflush: $| = 1;


    DWIM is Perl's answer to Gödel
Re: Printing inside a loop
by mbeast (Beadle) on Feb 10, 2006 at 21:53 UTC
    Output is buffered.

    However you can disable that.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found