I really don’t know how to word this question, so I’ll give an example of what I’m trying to do.
I have a script where I need to use the sleep function for a few seconds. But before I go to “sleep” I need to have a message say for example “Sleeping… “. But after it’s done sleeping, I need to print “done”. So what the terminal window will look like would be something like this: Sleeping... done, and this is my code for that:
#!/usr/bin/perl
print "Sleeping... ";
sleep(1);
print "done";
But what happens is, the script sleeps, and it then outputs Sleeping... done all at once.
Is it possible to print “Sleeping… “, then actually go to “sleep”, and finally print “done”, but have everything on one line?
You can enable auto flush on the file handle by setting
$|to a non-zero value, such as:So you’re full script would be:
You can turn it back off by setting
$|to0, which you should do if this is just a small part of a larger script. Output is buffered by default for a reason.