Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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


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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-26 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found