| 1 | =head1 NAME
|
|---|
| 2 |
|
|---|
| 3 | perlfaq8 - System Interaction ($Revision: 1.27 $, $Date: 2005/12/31 00:54:37 $)
|
|---|
| 4 |
|
|---|
| 5 | =head1 DESCRIPTION
|
|---|
| 6 |
|
|---|
| 7 | This section of the Perl FAQ covers questions involving operating
|
|---|
| 8 | system interaction. Topics include interprocess communication (IPC),
|
|---|
| 9 | control over the user-interface (keyboard, screen and pointing
|
|---|
| 10 | devices), and most anything else not related to data manipulation.
|
|---|
| 11 |
|
|---|
| 12 | Read the FAQs and documentation specific to the port of perl to your
|
|---|
| 13 | operating system (eg, L<perlvms>, L<perlplan9>, ...). These should
|
|---|
| 14 | contain more detailed information on the vagaries of your perl.
|
|---|
| 15 |
|
|---|
| 16 | =head2 How do I find out which operating system I'm running under?
|
|---|
| 17 |
|
|---|
| 18 | The $^O variable ($OSNAME if you use English) contains an indication of
|
|---|
| 19 | the name of the operating system (not its release number) that your perl
|
|---|
| 20 | binary was built for.
|
|---|
| 21 |
|
|---|
| 22 | =head2 How come exec() doesn't return?
|
|---|
| 23 |
|
|---|
| 24 | Because that's what it does: it replaces your currently running
|
|---|
| 25 | program with a different one. If you want to keep going (as is
|
|---|
| 26 | probably the case if you're asking this question) use system()
|
|---|
| 27 | instead.
|
|---|
| 28 |
|
|---|
| 29 | =head2 How do I do fancy stuff with the keyboard/screen/mouse?
|
|---|
| 30 |
|
|---|
| 31 | How you access/control keyboards, screens, and pointing devices
|
|---|
| 32 | ("mice") is system-dependent. Try the following modules:
|
|---|
| 33 |
|
|---|
| 34 | =over 4
|
|---|
| 35 |
|
|---|
| 36 | =item Keyboard
|
|---|
| 37 |
|
|---|
| 38 | Term::Cap Standard perl distribution
|
|---|
| 39 | Term::ReadKey CPAN
|
|---|
| 40 | Term::ReadLine::Gnu CPAN
|
|---|
| 41 | Term::ReadLine::Perl CPAN
|
|---|
| 42 | Term::Screen CPAN
|
|---|
| 43 |
|
|---|
| 44 | =item Screen
|
|---|
| 45 |
|
|---|
| 46 | Term::Cap Standard perl distribution
|
|---|
| 47 | Curses CPAN
|
|---|
| 48 | Term::ANSIColor CPAN
|
|---|
| 49 |
|
|---|
| 50 | =item Mouse
|
|---|
| 51 |
|
|---|
| 52 | Tk CPAN
|
|---|
| 53 |
|
|---|
| 54 | =back
|
|---|
| 55 |
|
|---|
| 56 | Some of these specific cases are shown as examples in other answers
|
|---|
| 57 | in this section of the perlfaq.
|
|---|
| 58 |
|
|---|
| 59 | =head2 How do I print something out in color?
|
|---|
| 60 |
|
|---|
| 61 | In general, you don't, because you don't know whether
|
|---|
| 62 | the recipient has a color-aware display device. If you
|
|---|
| 63 | know that they have an ANSI terminal that understands
|
|---|
| 64 | color, you can use the Term::ANSIColor module from CPAN:
|
|---|
| 65 |
|
|---|
| 66 | use Term::ANSIColor;
|
|---|
| 67 | print color("red"), "Stop!\n", color("reset");
|
|---|
| 68 | print color("green"), "Go!\n", color("reset");
|
|---|
| 69 |
|
|---|
| 70 | Or like this:
|
|---|
| 71 |
|
|---|
| 72 | use Term::ANSIColor qw(:constants);
|
|---|
| 73 | print RED, "Stop!\n", RESET;
|
|---|
| 74 | print GREEN, "Go!\n", RESET;
|
|---|
| 75 |
|
|---|
| 76 | =head2 How do I read just one key without waiting for a return key?
|
|---|
| 77 |
|
|---|
| 78 | Controlling input buffering is a remarkably system-dependent matter.
|
|---|
| 79 | On many systems, you can just use the B<stty> command as shown in
|
|---|
| 80 | L<perlfunc/getc>, but as you see, that's already getting you into
|
|---|
| 81 | portability snags.
|
|---|
| 82 |
|
|---|
| 83 | open(TTY, "+</dev/tty") or die "no tty: $!";
|
|---|
| 84 | system "stty cbreak </dev/tty >/dev/tty 2>&1";
|
|---|
| 85 | $key = getc(TTY); # perhaps this works
|
|---|
| 86 | # OR ELSE
|
|---|
| 87 | sysread(TTY, $key, 1); # probably this does
|
|---|
| 88 | system "stty -cbreak </dev/tty >/dev/tty 2>&1";
|
|---|
| 89 |
|
|---|
| 90 | The Term::ReadKey module from CPAN offers an easy-to-use interface that
|
|---|
| 91 | should be more efficient than shelling out to B<stty> for each key.
|
|---|
| 92 | It even includes limited support for Windows.
|
|---|
| 93 |
|
|---|
| 94 | use Term::ReadKey;
|
|---|
| 95 | ReadMode('cbreak');
|
|---|
| 96 | $key = ReadKey(0);
|
|---|
| 97 | ReadMode('normal');
|
|---|
| 98 |
|
|---|
|
|---|