source: trunk/essentials/dev-lang/perl/pod/perlfaq8.pod@ 3368

Last change on this file since 3368 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 42.2 KB
Line 
1=head1 NAME
2
3perlfaq8 - System Interaction ($Revision: 1.27 $, $Date: 2005/12/31 00:54:37 $)
4
5=head1 DESCRIPTION
6
7This section of the Perl FAQ covers questions involving operating
8system interaction. Topics include interprocess communication (IPC),
9control over the user-interface (keyboard, screen and pointing
10devices), and most anything else not related to data manipulation.
11
12Read the FAQs and documentation specific to the port of perl to your
13operating system (eg, L<perlvms>, L<perlplan9>, ...). These should
14contain 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
18The $^O variable ($OSNAME if you use English) contains an indication of
19the name of the operating system (not its release number) that your perl
20binary was built for.
21
22=head2 How come exec() doesn't return?
23
24Because that's what it does: it replaces your currently running
25program with a different one. If you want to keep going (as is
26probably the case if you're asking this question) use system()
27instead.
28
29=head2 How do I do fancy stuff with the keyboard/screen/mouse?
30
31How 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
56Some of these specific cases are shown as examples in other answers
57in this section of the perlfaq.
58
59=head2 How do I print something out in color?
60
61In general, you don't, because you don't know whether
62the recipient has a color-aware display device. If you
63know that they have an ANSI terminal that understands
64color, 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
70Or 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
78Controlling input buffering is a remarkably system-dependent matter.
79On many systems, you can just use the B<stty> command as shown in
80L<perlfunc/getc>, but as you see, that's already getting you into
81portability 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
90The Term::ReadKey module from CPAN offers an easy-to-use interface that
91should be more efficient than shelling out to B<stty> for each key.
92It even includes limited support for Windows.
93
94 use Term::ReadKey;
95 ReadMode('cbreak');
96 $key = ReadKey(0);
97 ReadMode('normal');
98