Program: Small termcap program

Description

This program clears your screen and scribbles all over it until you interrupt it. It shows how to use Term::Cap to clear the screen, move the cursor, and write anywhere on the screen. It also uses Section 16.6.

The program text is shown in Example 15.9.

Example 15-9. tcapdemo

#!/usr/bin/perl -w
# tcapdemo - show off direct cursor placement use POSIX; use Term::Cap; init(); # Initialize Term::Cap. zip(); # Bounce lines around the screen. finish(); # Clean up afterward. exit(); # Two convenience functions. clear_screen is obvious, and # clear_end clears to the end of the screen. sub clear_screen { $tcap->Tputs('cl', 1, *STDOUT) } sub clear_end { $tcap->Tputs('cd', 1, *STDOUT) } # Move the cursor to a particular location. sub gotoxy { my($x, $y) = @_; $tcap->Tgoto('cm', $x, $y, *STDOUT); } # Get the terminal speed through the POSIX module and use that # to initialize Term::Cap. sub init { $| = 1; $delay = (shift() || 0) * 0.005; my $termios = POSIX::Termios->new(); $termios->getattr; my $ospeed = $termios->getospeed; $tcap = Term::Cap->Tgetent ({ TERM => undef, OSPEED => $ospeed }); $tcap->Trequire(qw(cl cm cd)); } # Bounce lines around the screen until the user interrupts with # Ctrl-C. sub zip { clear_screen(); ($maxrow, $maxcol) = ($tcap->{_li} - 1, $tcap->{_co} - 1); @chars = qw(* - / | \ _ ); sub circle { push(@chars, shift @chars); } $interrupted = 0; $SIG{INT} = sub { ++$interrupted }; $col = $row = 0; ($row_sign, $col_sign) = (1,1); ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.