If you're on a Linux system and want to "roll your own", you might consider creating a subroutine to enter "raw" terminal mode, as in the following example. The subroutine
one_key creates a closure which, each time it's called, gets the next character, but without blocking. When you're done, you can call the closure a final time with a zero argument to reset "cooked" mode (normal terminal).
#!/usr/bin/perl -w
#
# Example of getting a single, non-blocking character in Linux.
#
# 060306 liverpole
#
# Strict
use strict;
use warnings;
# Create a subroutine (closure) for capturing a key
sub one_key {
chomp(my $stty = `stty -g`); # Save 'cooked' mode t
+ty
`stty -icrnl -icanon -echo min 0 time 0`; # Begin raw mode
sub { # Create a closure
if (!$_[0]) { # If argument is zero
+...
system("stty $stty"); # restore 'cooked' m
+ode
} else { # Otherwise get and re
+turn
`dd bs=1 count=1 <&0 2>/dev/null`; # a single keystroke
}
};
}
# Main program
my $p_one_key = one_key();
while (1) {
print ' ' x int(rand(20)), "*\n";
select(undef, undef, undef, 0.1);
last if $p_one_key->(1);
}
$p_one_key->(0);
@ARGV=split//,"/:L";
map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.