Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

RFC: Draft Q & A: How to clear the screen in a perl program

by NovMonk (Chaplain)
on Jul 18, 2006 at 18:38 UTC ( [id://562093]=perlmeditation: print w/replies, xml ) Need Help??

Esteemed Monks,

I just stumbled upon something I have searched for a couple of times without success. It involves how to clear a screen within an interactive Perl program.

It might seem an obvious thing, but believe me, for a novice, who has shaky Unix knowledge in the first place, this is a useful ability: to be able to wipe the screen in simple interactive programs. It might even be educational-- there are all kinds of other system commands they might think to learn and employ.

All that said, this is my draft of a Q & A, with comments welcome, and with suggestions for where it might go even more welcome. Maybe we could make a novice/misc Q & A section? And what about making a table somewhere in the Q & A or Tutorials section, of common systems commands and what they do?

How do I clear the screen?

Sometimes, in a program, you want all previous inputs to disappear, for aesthetic or other reasons. When you want this to happen, add this line to your program at the appropriate spot:

system "clear";

An example follows: part of a program to interactively quiz a child on his times tables. Note text in <> is pseudocode, except for  "<STDIN>":

while (my $exit = 0) { <generate question variables randomly and calculate solution> print "$mult1 * $mult2 = ?\n"; chomp ($guess = <STDIN>); <test whether $guess = $solution; if so> print "Good job! Want to play again? (Y/N)\n"; <test answer here; if Y, then> system "clear"; } <program loops back and pick another pair of numbers and caculates +their product>
Note that I was going to post this as a question in SoPW, until I stumbled across another system command, and wondered what would happen if I substituted "clear" where that other command was. But I remember a time when I didn't know "clear" was a Unix command. Hence this little Q & A. Thanks in advance for your comments.

NovMonk

Replies are listed 'Best First'.
Re: RFC: Draft Q & A: How to clear the screen in a perl program
by Fletch (Bishop) on Jul 18, 2006 at 19:06 UTC

    Actually you can usually just cache the output from qx/clear/ (or qx/tput clear/) and use that. Or use Term::Cap and avoid calling anything external at all.

Re: RFC: Draft Q & A: How to clear the screen in a perl program
by gellyfish (Monsignor) on Jul 18, 2006 at 19:07 UTC

    You want to look at the clrscr() method from Term::Screen

    /J\

Re: RFC: Draft Q & A: How to clear the screen in a perl program
by ysth (Canon) on Jul 18, 2006 at 23:32 UTC
    Many obfus do system(($^O eq 'MSWin32')?'cls':'clear'). Others erroneously have $^O=~/win/i or similar instead, which breaks on cygwin (and darwin?).
Re: RFC: Draft Q & A: How to clear the screen in a perl program
by NovMonk (Chaplain) on Jul 20, 2006 at 12:09 UTC
    Thanks for the replies. As I expected, I learned a lot more about what I didn't know.

    The last comment by AnonyMonk, while harsh, was particularly enlightening. I would respond, though, that some people just starting out, and who have found this oasis of knowledge in our monastery, will do what I did-- super search here on "clear screen" and variations. I know about man, but I believed you had to know what the sometimes cryptic command was Called before you could wrest anything intelligible out of it.

    If it's the consensus of the community that this is not needed in our Q&A section, that's cool. Someone can still seach as I did and now find this little discussion. So my aim has been achived. But my thought was, when I wrote this up, that something this simple and basic, should be available somewhere in our tutorials or Q&A. I can't be the only one coming to Perl with limited Unix background who wanted to know how to do this.

    Thanks again to all who commented.

    NovMonk

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: RFC: Draft Q & A: How to clear the screen in a perl program
by Anonymous Monk on Jul 19, 2006 at 19:02 UTC
    Don't bother trying to make this an RFC.

    If you want to know how to do something in UNIX, you need to know exactly one command before you start: "man".

    This lets you look up how things work in the UNIX manual pages. If you look up the "man" commmand, you'll see that if you use the -k option, you can do a keyword search on any topic you want.

    Let's find out what commands might be involved in clearing the screen, shall we?

    %man -k clear | grep screen clear(1) - clear terminal screen

    Right: there's exactly one command, called "clear", which clears the screen.

    See? If people read the UNIX FAQ, they won't need to read your little FAQ. If they're not the type to read a FAQ, writing one won't help.

    If they do read the UNIX FAQ, they'll also know how to look up the man page for perl. The man page for perl points out that there is a man page for the Perl FAQ.

    The Perl FAQ explictly lists the answer to the question: "How do I clear the screen".

    We don't need another FAQ: we need people to read the FAQs that already exist.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-16 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found