📄 perlfaq8.1
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "PERLFAQ8 1".TH PERLFAQ8 1 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"perlfaq8 \- System Interaction ($Revision: 10183 $).SH "DESCRIPTION".IX Header "DESCRIPTION"This section of the Perl \s-1FAQ\s0 covers questions involving operatingsystem interaction. Topics include interprocess communication (\s-1IPC\s0),control over the user-interface (keyboard, screen and pointingdevices), and most anything else not related to data manipulation..PPRead the FAQs and documentation specific to the port of perl to youroperating system (eg, perlvms, perlplan9, ...). These shouldcontain more detailed information on the vagaries of your perl..Sh "How do I find out which operating system I'm running under?".IX Subsection "How do I find out which operating system I'm running under?"The $^O variable ($OSNAME if you use English) contains an indication ofthe name of the operating system (not its release number) that your perlbinary was built for..Sh "How come \fIexec()\fP doesn't return?".IX Subsection "How come exec() doesn't return?"Because that's what it does: it replaces your currently runningprogram with a different one. If you want to keep going (as isprobably the case if you're asking this question) use \fIsystem()\fRinstead..Sh "How do I do fancy stuff with the keyboard/screen/mouse?".IX Subsection "How do I do fancy stuff with the keyboard/screen/mouse?"How you access/control keyboards, screens, and pointing devices(\*(L"mice\*(R") is system-dependent. Try the following modules:.IP "Keyboard" 4.IX Item "Keyboard".Vb 5\& Term::Cap Standard perl distribution\& Term::ReadKey CPAN\& Term::ReadLine::Gnu CPAN\& Term::ReadLine::Perl CPAN\& Term::Screen CPAN.Ve.IP "Screen" 4.IX Item "Screen".Vb 3\& Term::Cap Standard perl distribution\& Curses CPAN\& Term::ANSIColor CPAN.Ve.IP "Mouse" 4.IX Item "Mouse".Vb 1\& Tk CPAN.Ve.PPSome of these specific cases are shown as examples in other answersin this section of the perlfaq..Sh "How do I print something out in color?".IX Subsection "How do I print something out in color?"In general, you don't, because you don't know whetherthe recipient has a color-aware display device. If youknow that they have an \s-1ANSI\s0 terminal that understandscolor, you can use the Term::ANSIColor module from \s-1CPAN:\s0.PP.Vb 3\& use Term::ANSIColor;\& print color("red"), "Stop!\en", color("reset");\& print color("green"), "Go!\en", color("reset");.Ve.PPOr like this:.PP.Vb 3\& use Term::ANSIColor qw(:constants);\& print RED, "Stop!\en", RESET;\& print GREEN, "Go!\en", RESET;.Ve.Sh "How do I read just one key without waiting for a return key?".IX Subsection "How do I read just one key without waiting for a return key?"Controlling input buffering is a remarkably system-dependent matter.On many systems, you can just use the \fBstty\fR command as shown in\&\*(L"getc\*(R" in perlfunc, but as you see, that's already getting you intoportability snags..PP.Vb 6\& open(TTY, "+</dev/tty") or die "no tty: $!";\& system "stty cbreak </dev/tty >/dev/tty 2>&1";\& $key = getc(TTY); # perhaps this works\& # OR ELSE\& sysread(TTY, $key, 1); # probably this does\& system "stty \-cbreak </dev/tty >/dev/tty 2>&1";.Ve.PPThe Term::ReadKey module from \s-1CPAN\s0 offers an easy-to-use interface thatshould be more efficient than shelling out to \fBstty\fR for each key.It even includes limited support for Windows..PP.Vb 4\& use Term::ReadKey;\& ReadMode(\*(Aqcbreak\*(Aq);\& $key = ReadKey(0);\& ReadMode(\*(Aqnormal\*(Aq);.Ve.PPHowever, using the code requires that you have a working C compilerand can use it to build and install a \s-1CPAN\s0 module. Here's a solutionusing the standard \s-1POSIX\s0 module, which is already on your systems(assuming your system supports \s-1POSIX\s0)..PP.Vb 2\& use HotKey;\& $key = readkey();.Ve.PPAnd here's the HotKey module, which hides the somewhat mystifying callsto manipulate the \s-1POSIX\s0 termios structures..PP.Vb 2\& # HotKey.pm\& package HotKey;\&\& @ISA = qw(Exporter);\& @EXPORT = qw(cbreak cooked readkey);\&\& use strict;\& use POSIX qw(:termios_h);\& my ($term, $oterm, $echo, $noecho, $fd_stdin);\&\& $fd_stdin = fileno(STDIN);\& $term = POSIX::Termios\->new();\& $term\->getattr($fd_stdin);\& $oterm = $term\->getlflag();\&\& $echo = ECHO | ECHOK | ICANON;\& $noecho = $oterm & ~$echo;\&\& sub cbreak {\& $term\->setlflag($noecho); # ok, so i don\*(Aqt want echo either\& $term\->setcc(VTIME, 1);\& $term\->setattr($fd_stdin, TCSANOW);\& }\&\& sub cooked {\& $term\->setlflag($oterm);\& $term\->setcc(VTIME, 0);\& $term\->setattr($fd_stdin, TCSANOW);\& }\&\& sub readkey {\& my $key = \*(Aq\*(Aq;\& cbreak();\& sysread(STDIN, $key, 1);\& cooked();\& return $key;\& }\&\& END { cooked() }\&\& 1;.Ve.Sh "How do I check whether input is ready on the keyboard?".IX Subsection "How do I check whether input is ready on the keyboard?"The easiest way to do this is to read a key in nonblocking mode with theTerm::ReadKey module from \s-1CPAN\s0, passing it an argument of \-1 to indicatenot to block:.PP.Vb 1\& use Term::ReadKey;\&\& ReadMode(\*(Aqcbreak\*(Aq);\&\& if (defined ($char = ReadKey(\-1)) ) {\& # input was waiting and it was $char\& } else {\& # no input was waiting\& }\&\& ReadMode(\*(Aqnormal\*(Aq); # restore normal tty settings.Ve.Sh "How do I clear the screen?".IX Subsection "How do I clear the screen?"If you only have do so infrequently, use \f(CW\*(C`system\*(C'\fR:.PP.Vb 1\& system("clear");.Ve.PPIf you have to do this a lot, save the clear stringso you can print it 100 times without calling a program100 times:.PP.Vb 2\& $clear_string = \`clear\`;\& print $clear_string;.Ve.PPIf you're planning on doing other screen manipulations, like cursorpositions, etc, you might wish to use Term::Cap module:.PP.Vb 3\& use Term::Cap;\& $terminal = Term::Cap\->Tgetent( {OSPEED => 9600} );\& $clear_string = $terminal\->Tputs(\*(Aqcl\*(Aq);.Ve.Sh "How do I get the screen size?".IX Subsection "How do I get the screen size?"If you have Term::ReadKey module installed from \s-1CPAN\s0,you can use it to fetch the width and height in charactersand in pixels:.PP.Vb 2\& use Term::ReadKey;\& ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();.Ve.PPThis is more portable than the raw \f(CW\*(C`ioctl\*(C'\fR, but not asillustrative:.PP.Vb 10\& require \*(Aqsys/ioctl.ph\*(Aq;\& die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;\& open(TTY, "+</dev/tty") or die "No tty: $!";\& unless (ioctl(TTY, &TIOCGWINSZ, $winsize=\*(Aq\*(Aq)) {\& die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\en", &TIOCGWINSZ;\& }\& ($row, $col, $xpixel, $ypixel) = unpack(\*(AqS4\*(Aq, $winsize);\& print "(row,col) = ($row,$col)";\& print " (xpixel,ypixel) = ($xpixel,$ypixel)" if $xpixel || $ypixel;\& print "\en";.Ve.Sh "How do I ask the user for a password?".IX Subsection "How do I ask the user for a password?"(This question has nothing to do with the web. See a different\&\s-1FAQ\s0 for that.).PPThere's an example of this in \*(L"crypt\*(R" in perlfunc). First, you put theterminal into \*(L"no echo\*(R" mode, then just read the password normally.You may do this with an old-style \fIioctl()\fR function, \s-1POSIX\s0 terminalcontrol (see \s-1POSIX\s0 or its documentation the Camel Book), or a callto the \fBstty\fR program, with varying degrees of portability..PPYou can also do this for most systems using the Term::ReadKey modulefrom \s-1CPAN\s0, which is easier to use and in theory more portable..PP.Vb 1\& use Term::ReadKey;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -