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


In reply to RFC: Draft Q & A: How to clear the screen in a perl program by NovMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.