The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Term::Screen - A Simple all perl Term::Cap based screen positioning module


   require Term::Screen;

   $scr = Term::Screen->new();
   unless ($scr) { die " Something's wrong \n"; }
   $scr->puts("this is some stuff");
      # you can concatenate many calls (not getch)
   $c = $scr->getch();      # doesn't need Enter key 
   if ($scr->key_pressed()) { print "ha you hit a key!"; }


Term::Screen is a very simple screen positioning module that should work wherever Term::Cap does. It is set up for Unix using stty's but these dependences are isolated by evals in the new constructor. Thus you may create a child module implementing Screen with MS-DOS, ioctl, or other means to get raw and unblocked input. This is not a replacement for Curses -- it has no memory. This was written so that it could be easily changed to fit nasty systems, and to be available first thing.

The input functions getch, key_pressed, echo, and noecho are implemented so as to work under a fairly standard Unix system. They use 'stty' to set raw and no echo modes and turn on auto flush. All of these are 'eval'ed so that this class can be inherited for new definitions easily.

Term::Screen was designed to be "required", then used with object syntax as shown above. One quirk (which the author was used to so he didn't care) is that for function key translation, no delay is set. So for many terminals to get an esc character, you have to hit another char after it, generally another esc.


Term::Screen has a very minimal set of of fixed character terminal position and character reading commands:


Initialize the screen. Does not clear the screen, but does home the cursor.


Sets or Gets the Term::Cap object used by this object.


Returns and/or sets the number of rows on the terminal.


Returns and/or sets the number of cols on the terminal.


Moves cursor to (row,col) where (0,0) is upper left corner, - if the spot is illegal does whatever 'cm' in termcap does, since that is what it uses.


Tell screen the new number of rows & cols physically you can skip the r & c and get new checked vals from stty or termcap. Term::Screen does not handle resize signals internally, but you can do it by checking and updating screen size using this function.


Turn off any highlightling (bold, reverse)


The md value from termcap - turn on bold usually


The mr value from termcap - turn on reverse text often. these last two default to whatever is available.


The vs value from termcap - make cursor very visible


The vi value from termcap - make cursor invisible


Clear the screen and home cursor


Clear to the end of the line - cursor doesn't move


Clear to end of screen - right and down, cursor doesn't move.


Insert blank line before line cursor is on, moving lower lines down.


Delete line cursor is on, moving lower lines up.


Insert character option is available.


Insert character at current position move rest to the right.


Delete char option exists and is available.


Delete character at current position moving rest to the left.

The following are the I/O functions. They provide standard useful single character reading values. getch returns either a single char or the name of a function key when a key is pressed. The only exception is when you hit a character that is the start of a function key sequence. In this case getch keeps waiting for the next char to see if it is fn key. Generally this is the escape key, and why you need to hit esc twice. To get a stright char, just use the regular 'gets' perl function. You will need to echo it yourself if you want.


Prints $s and returns the screen object. Used to do things like $scr-at(10,0)->puts("Hi!")->at(0,0);>. You can just use print if you want.


Returns just a char in raw mode. Function keys are returned as their capability names, e.g. the up key would return "ku". See the get_fn_keys function for what a lot of the names are. This will wait for next char if in a possible fn key string, so you would need to type 'esc' 'esc' most likely to get out of getch, since 'esc' is usually the leading char for function keys. You can use perl's getc, to go 'underneath' getch if you want. See the table in Screen::get_fn_keys() for more information.

def_key('name','input string')

Lets you define your own function key sequence. 'name' is what will be returned by getch. 'input string' is what the fn key sends literally. This will override any prev definitions of the input. A whole bunch of defaults are defined for xterms rxvt's, etc. in the get_fn_keys function.


Returns true if there is a character waiting. You can pass an option time in seconds to wait.


Tells getch to echo the input to the screen. (the default.)


Tells getch NOT to echo input to the screen.


Clears input buffer and removes any incoming chars.


Lets you stuff chars into the input buffer to be read like keystrokes. This is only the getch method buffer, the underlying getc stuff is not touched.


AUTHOR by Mark Kaehny ( Currently maintained by Jonathan Stowe <>


Please see the README file in the distribution kit for the license details for this module.


Term::Cap, termcap, curses, stty, select