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.
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: