westrock2000 has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I am self teaching perl and I have come to a road block, because I do not understand if what I want to do is fundamentaly possible in perl. I would like to have a running clock on a screen while it waits for user input. But I want the clock to be at a certain place in the screen, not just where the user would enter data. So far my understanding of programming is "line by line", so I do not understand how to tell perl to do Routine A while also doing Routine B. I came up with this script that kind of gives idea of what I want, obviously its not perfect because it runs in infinite loop, but you can get an idea of my thinking so far.
#!/usr/bin/perl #The \033[ stuff only works in Xterm print ("\033[2J"); #<- Clears screen print ("\033[6;0H"); #<- Repositions cursor at 6 down 0 from the left print ("Press C to continue\n"); print ("Press Q to quite\n"); do{ print ("\033[s"); #<-- Saves Cursor Position print ("\033[3;6H",`date`); #<-- Puts date and time above "Press C + to continue" print ("\033[u"); #<-- Restores Cursor Position }until (exists $input[0]); if ( $input eq Q ){ exit; }
Here is what I'm looking for as far as layout
May 21 22:00 Press C to continue Press Q to quite
Other then not knowing how to get $input entered, this sort of does what I want, except that you can tell that its drawing the screen over and over because the cursor moves back and forth.

Is it within perl's capabilities to draw the clock at the top of the screen, but also wait for user input below the choices?

Replies are listed 'Best First'.
Re: Can I have a running clock within a script in Xterm?
by Utilitarian (Vicar) on May 22, 2010 at 07:03 UTC
    Hi westrock,

    This list of Tutorials on threads will bring you through how to have two things happen at once.

    PS: Term::ANSIScreen will allow you position the cursor on the screen more legibly, also you should have a sleep(1); in the timer loop as otherwise it will hog too much of the processor

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Can I have a running clock within a script in Xterm?
by flipper (Beadle) on May 22, 2010 at 16:49 UTC
    I'd be inclined to use IO::Select to get notification that STDIN has some data for you. It's pretty simple to use, you can wait in a loop for a timeout of 1s for input, and update the clock if you don't get it.

    As the other reply says, there's got to be a more legible way of moving the cursor around :-)