📄 perlfaq7.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 "PERLFAQ7 1".TH PERLFAQ7 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"perlfaq7 \- General Perl Language Issues ($Revision: 10100 $).SH "DESCRIPTION".IX Header "DESCRIPTION"This section deals with general Perl language issues that don'tclearly fit into any of the other sections..Sh "Can I get a BNF/yacc/RE for the Perl language?".IX Subsection "Can I get a BNF/yacc/RE for the Perl language?"There is no \s-1BNF\s0, but you can paw your way through the yacc grammar inperly.y in the source distribution if you're particularly brave. Thegrammar relies on very smart tokenizing code, so be prepared toventure into toke.c as well..PPIn the words of Chaim Frenkel: \*(L"Perl's grammar can not be reduced to \s-1BNF\s0.The work of parsing perl is distributed between yacc, the lexer, smokeand mirrors.\*(R".Sh "What are all these $@%&* punctuation signs, and how do I know when to use them?".IX Subsection "What are all these $@%&* punctuation signs, and how do I know when to use them?"They are type specifiers, as detailed in perldata:.PP.Vb 6\& $ for scalar values (number, string or reference)\& @ for arrays\& % for hashes (associative arrays)\& & for subroutines (aka functions, procedures, methods)\& * for all types of that symbol name. In version 4 you used them like\& pointers, but in modern perls you can just use references..Ve.PPThere are couple of other symbols that you're likely to encounter that aren'treally type specifiers:.PP.Vb 2\& <> are used for inputting a record from a filehandle.\& \e takes a reference to something..Ve.PPNote that <\s-1FILE\s0> is \fIneither\fR the type specifier for filesnor the name of the handle. It is the \f(CW\*(C`<>\*(C'\fR operator appliedto the handle \s-1FILE\s0. It reads one line (well, record\*(--see\&\*(L"$/\*(R" in perlvar) from the handle \s-1FILE\s0 in scalar context, or \fIall\fR linesin list context. When performing open, close, or any other operationbesides \f(CW\*(C`<>\*(C'\fR on files, or even when talking about the handle, do\&\fInot\fR use the brackets. These are correct: \f(CW\*(C`eof(FH)\*(C'\fR, \f(CW\*(C`seek(FH, 0,2)\*(C'\fR and \*(L"copying from \s-1STDIN\s0 to \s-1FILE\s0\*(R"..Sh "Do I always/never have to quote my strings or use semicolons and commas?".IX Subsection "Do I always/never have to quote my strings or use semicolons and commas?"Normally, a bareword doesn't need to be quoted, but in most casesprobably should be (and must be under \f(CW\*(C`use strict\*(C'\fR). But a hash keyconsisting of a simple word (that isn't the name of a definedsubroutine) and the left-hand operand to the \f(CW\*(C`=>\*(C'\fR operator bothcount as though they were quoted:.PP.Vb 4\& This is like this\& \-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& $foo{line} $foo{\*(Aqline\*(Aq}\& bar => stuff \*(Aqbar\*(Aq => stuff.Ve.PPThe final semicolon in a block is optional, as is the final comma in alist. Good style (see perlstyle) says to put them in except forone-liners:.PP.Vb 2\& if ($whoops) { exit 1 }\& @nums = (1, 2, 3);\& \& if ($whoops) {\& exit 1;\& }\&\& @lines = (\& "There Beren came from mountains cold",\& "And lost he wandered under leaves",\& );.Ve.Sh "How do I skip some return values?".IX Subsection "How do I skip some return values?"One way is to treat the return values as a list and index into it:.PP.Vb 1\& $dir = (getpwnam($user))[7];.Ve.PPAnother way is to use undef as an element on the left-hand-side:.PP.Vb 1\& ($dev, $ino, undef, undef, $uid, $gid) = stat($file);.Ve.PPYou can also use a list slice to select only the elements thatyou need:.PP.Vb 1\& ($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];.Ve.Sh "How do I temporarily block warnings?".IX Subsection "How do I temporarily block warnings?"If you are running Perl 5.6.0 or better, the \f(CW\*(C`use warnings\*(C'\fR pragmaallows fine control of what warning are produced.See perllexwarn for more details..PP.Vb 4\& {\& no warnings; # temporarily turn off warnings\& $a = $b + $c; # I know these might be undef\& }.Ve.PPAdditionally, you can enable and disable categories of warnings.You turn off the categories you want to ignore and you can stillget other categories of warnings. See perllexwarn for thecomplete details, including the category names and hierarchy..PP.Vb 4\& {\& no warnings \*(Aquninitialized\*(Aq;\& $a = $b + $c;\& }.Ve.PPIf you have an older version of Perl, the \f(CW$^W\fR variable (documentedin perlvar) controls runtime warnings for a block:.PP.Vb 4\& {\& local $^W = 0; # temporarily turn off warnings\& $a = $b + $c; # I know these might be undef\& }.Ve.PPNote that like all the punctuation variables, you cannot currentlyuse \fImy()\fR on \f(CW$^W\fR, only \fIlocal()\fR..Sh "What's an extension?".IX Subsection "What's an extension?"An extension is a way of calling compiled C code from Perl. Readingperlxstut is a good place to learn more about extensions..Sh "Why do Perl operators have different precedence than C operators?".IX Subsection "Why do Perl operators have different precedence than C operators?"Actually, they don't. All C operators that Perl copies have the sameprecedence in Perl as they do in C. The problem is with operators that Cdoesn't have, especially functions that give a list context to everythingon their right, eg. print, chmod, exec, and so on. Such functions arecalled \*(L"list operators\*(R" and appear as such in the precedence table inperlop..PPA common mistake is to write:.PP.Vb 1\& unlink $file || die "snafu";.Ve.PPThis gets interpreted as:.PP.Vb 1\& unlink ($file || die "snafu");.Ve.PPTo avoid this problem, either put in extra parentheses or use thesuper low precedence \f(CW\*(C`or\*(C'\fR operator:.PP.Vb 2\& (unlink $file) || die "snafu";\& unlink $file or die "snafu";.Ve.PPThe \*(L"English\*(R" operators (\f(CW\*(C`and\*(C'\fR, \f(CW\*(C`or\*(C'\fR, \f(CW\*(C`xor\*(C'\fR, and \f(CW\*(C`not\*(C'\fR)deliberately have precedence lower than that of list operators for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -