1

I would like flush the output after executing the script and all the output should not appear in my terminal screen.

Say I have a following example

#!/usr/bin/perl
print "HAI \n";
sleep 5;

If I execute the above script

# ./file
HAI
#

I would like to have the above output as

# ./file
#

HAI Should be displayed for 5 seconds and it should get deleted and no output should be there in the terminal like putty.

dgw
  • 13,418
  • 11
  • 56
  • 54
Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27
  • possible duplicate of [How flush file in perl?](http://stackoverflow.com/questions/4538767/how-flush-file-in-perl) – J-16 SDiZ Feb 20 '12 at 10:09
  • @J-16SDiZ He doesn't mean "flush the buffer", he means "clear the screen". – TLP Feb 20 '12 at 10:11
  • I don't think you mean "flush" as it refers to the buffer being printed to the screen immediately. You are looking for a way to remove stuff that has already been printed. – NiklasMM Feb 20 '12 at 10:13
  • okay, sorry. I read the first answer and thought that is what he want to do. – J-16 SDiZ Feb 20 '12 at 10:21

6 Answers6

5

As already stated, simply setting $| to a true value will auto flush your results.

When you say, "HAI Should be displayed for 5 seconds and it should get deleted and no output should be there in the terminal like putty.", you are describing actually removing printed text from the screen.

To do that you need to use special output. See Term::ANSIScreen for one perl module that will help you do that.

Here is some code that does what you are looking for:

use strict;
use Term::ANSIScreen qw/:cursor :screen/;

$| = 1;

savepos;
print "HAI";
sleep 5;
loadpos;
clline();
GoldenNewby
  • 4,382
  • 8
  • 33
  • 44
3

This code performs the particular trick I think you are after:

$| = 1;       # set autoflush on STDOUT to avoid buffer problems
print "HAI "; # note no newline
sleep 5; 
print "\r      \n";'

\r will move the cursor back to the start of the line and then overwrite what was previously written.

But that's somewhat of a hack. You're probably looking for something like Curses.

TLP
  • 66,756
  • 10
  • 92
  • 149
2

I use

 system("clear"); 

Hope this helps!

Alex Schittko
  • 53
  • 2
  • 7
1

$| is the variable control the flush behavior. Check it here

Just add this line before/after your print.

$| = 1;

If you are using xterm, the character sequence \e[2J is used to clear the whole terminal window. You can

$| = 1;
print "anything";
sleep 5;
print "\e[2J";

For other terminal, you can look for the correspondence character sequence.

Ade YU
  • 2,292
  • 3
  • 18
  • 28
1

What does it mean to flush

As already two answers misundestand your question, let me point: the word 'flush' in perl (and not only) context means forcing printout of the buffered output. To understand what it is about, try sth like

for(1..60) {
print "a";
sleep(1);
}
print "\n";

Contrary to what you may expect, for 60 seconds you will get no output, then 60 letters a. This is because perl buffers stdout output and sends it only on newline, file close, or when the buffer is full. And the action of actually sending this buffer is called flush, for one reason, or another.

Setting $|=1 ($| is sometimes called AUTOFLUSH and having it set means „flush stdout buffer on every character printed”) before those actions, as suggested above, changes this behaviour, but seems not related to actual problem you have.

Clearing the screen

In general there is no way to clear the output. In particular, if you redirect script to the file like

perl myscript.pl > somefile.txt

and print something, than this something will land in somefile.txt whatever you do.

When the output is the screen or console, there are many ways, but they depend on what kind of console it is. In many cases sending \r as TLP suggested will do. In other you may use terminal control codes (which are wrapped by curses library).

But there also will be cases when it won't help, for example the one I already mentioned (when you output to the file) or when you use some non-conformant console (frequent case is running script from some text editor or IDE).

So, to summarize:

  • if you really need to backtrack the output, reconsider your design,

  • if you are writing some textual progress bar or sth similar for interactive script, use \r or consider moving to Curses or maybe Term::Screen. Or check higher-level available modules like Term::ProgressBar

Mekk
  • 1,441
  • 10
  • 12
1

I got it we can get it by calling the function clup(); . Anyhow again thanks for each and every one

use strict;
use Term::ANSIScreen qw/:cursor :screen/;

$| = 1;

savepos;
print "HAI";
print "HAI";
print "HAI";
print "HAI";
sleep 2;
loadpos;
clup();

This worked for me :)

Sriharsha Kalluru
  • 1,743
  • 3
  • 21
  • 27