📄 perlsyn.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 "PERLSYN 1".TH PERLSYN 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"perlsyn \- Perl syntax.IX Xref "syntax".SH "DESCRIPTION".IX Header "DESCRIPTION"A Perl program consists of a sequence of declarations and statementswhich run from the top to the bottom. Loops, subroutines and othercontrol structures allow you to jump around within the code..PPPerl is a \fBfree-form\fR language, you can format and indent it howeveryou like. Whitespace mostly serves to separate tokens, unlikelanguages like Python where it is an important part of the syntax..PPMany of Perl's syntactic elements are \fBoptional\fR. Rather thanrequiring you to put parentheses around every function call anddeclare every variable, you can often leave such explicit elements offand Perl will figure out what you meant. This is known as \fBDo What IMean\fR, abbreviated \fB\s-1DWIM\s0\fR. It allows programmers to be \fBlazy\fR and tocode in a style with which they are comfortable..PPPerl \fBborrows syntax\fR and concepts from many languages: awk, sed, C,Bourne Shell, Smalltalk, Lisp and even English. Otherlanguages have borrowed syntax from Perl, particularly its regularexpression extensions. So if you have programmed in another languageyou will see familiar pieces in Perl. They often work the same, butsee perltrap for information about how they differ..Sh "Declarations".IX Xref "declaration undef undefined uninitialized".IX Subsection "Declarations"The only things you need to declare in Perl are report formats andsubroutines (and sometimes not even subroutines). A variable holdsthe undefined value (\f(CW\*(C`undef\*(C'\fR) until it has been assigned a definedvalue, which is anything other than \f(CW\*(C`undef\*(C'\fR. When used as a number,\&\f(CW\*(C`undef\*(C'\fR is treated as \f(CW0\fR; when used as a string, it is treated asthe empty string, \f(CW""\fR; and when used as a reference that isn't beingassigned to, it is treated as an error. If you enable warnings,you'll be notified of an uninitialized value whenever you treat\&\f(CW\*(C`undef\*(C'\fR as a string or a number. Well, usually. Boolean contexts,such as:.PP.Vb 2\& my $a;\& if ($a) {}.Ve.PPare exempt from warnings (because they care about truth rather thandefinedness). Operators such as \f(CW\*(C`++\*(C'\fR, \f(CW\*(C`\-\-\*(C'\fR, \f(CW\*(C`+=\*(C'\fR,\&\f(CW\*(C`\-=\*(C'\fR, and \f(CW\*(C`.=\*(C'\fR, that operate on undefined left values such as:.PP.Vb 2\& my $a;\& $a++;.Ve.PPare also always exempt from such warnings..PPA declaration can be put anywhere a statement can, but has no effect onthe execution of the primary sequence of statements\*(--declarations alltake effect at compile time. Typically all the declarations are put atthe beginning or the end of the script. However, if you're usinglexically-scoped private variables created with \f(CW\*(C`my()\*(C'\fR, you'llhave to make sureyour format or subroutine definition is within the same block scopeas the my if you expect to be able to access those private variables..PPDeclaring a subroutine allows a subroutine name to be used as if it were alist operator from that point forward in the program. You can declare asubroutine without defining it by saying \f(CW\*(C`sub name\*(C'\fR, thus:.IX Xref "subroutine, declaration".PP.Vb 2\& sub myname;\& $me = myname $0 or die "can\*(Aqt get myname";.Ve.PPNote that \fImyname()\fR functions as a list operator, not as a unary operator;so be careful to use \f(CW\*(C`or\*(C'\fR instead of \f(CW\*(C`||\*(C'\fR in this case. However, ifyou were to declare the subroutine as \f(CW\*(C`sub myname ($)\*(C'\fR, then\&\f(CW\*(C`myname\*(C'\fR would function as a unary operator, so either \f(CW\*(C`or\*(C'\fR or\&\f(CW\*(C`||\*(C'\fR would work..PPSubroutines declarations can also be loaded up with the \f(CW\*(C`require\*(C'\fR statementor both loaded and imported into your namespace with a \f(CW\*(C`use\*(C'\fR statement.See perlmod for details on this..PPA statement sequence may contain declarations of lexically-scopedvariables, but apart from declaring a variable name, the declaration actslike an ordinary statement, and is elaborated within the sequence ofstatements as if it were an ordinary statement. That means it actuallyhas both compile-time and run-time effects..Sh "Comments".IX Xref "comment #".IX Subsection "Comments"Text from a \f(CW"#"\fR character until the end of the line is a comment,and is ignored. Exceptions include \f(CW"#"\fR inside a string or regularexpression..Sh "Simple Statements".IX Xref "statement semicolon expression ;".IX Subsection "Simple Statements"The only kind of simple statement is an expression evaluated for itsside effects. Every simple statement must be terminated with asemicolon, unless it is the final statement in a block, in which casethe semicolon is optional. (A semicolon is still encouraged if theblock takes up more than one line, because you may eventually addanother line.) Note that there are some operators like \f(CW\*(C`eval {}\*(C'\fR and\&\f(CW\*(C`do {}\*(C'\fR that look like compound statements, but aren't (they're justTERMs in an expression), and thus need an explicit termination if usedas the last item in a statement..Sh "Truth and Falsehood".IX Xref "truth falsehood true false ! not negation 0".IX Subsection "Truth and Falsehood"The number 0, the strings \f(CW\*(Aq0\*(Aq\fR and \f(CW\*(Aq\*(Aq\fR, the empty list \f(CW\*(C`()\*(C'\fR, and\&\f(CW\*(C`undef\*(C'\fR are all false in a boolean context. All other values are true.Negation of a true value by \f(CW\*(C`!\*(C'\fR or \f(CW\*(C`not\*(C'\fR returns a special false value.When evaluated as a string it is treated as \f(CW\*(Aq\*(Aq\fR, but as a number, itis treated as 0..Sh "Statement Modifiers".IX Xref "statement modifier modifier if unless while until foreach for".IX Subsection "Statement Modifiers"Any simple statement may optionally be followed by a \fI\s-1SINGLE\s0\fR modifier,just before the terminating semicolon (or block ending). The possiblemodifiers are:.PP.Vb 5\& if EXPR\& unless EXPR\& while EXPR\& until EXPR\& foreach LIST.Ve.PPThe \f(CW\*(C`EXPR\*(C'\fR following the modifier is referred to as the \*(L"condition\*(R".Its truth or falsehood determines how the modifier will behave..PP\&\f(CW\*(C`if\*(C'\fR executes the statement once \fIif\fR and only if the condition istrue. \f(CW\*(C`unless\*(C'\fR is the opposite, it executes the statement \fIunless\fRthe condition is true (i.e., if the condition is false)..PP.Vb 2\& print "Basset hounds got long ears" if length $ear >= 10;\& go_outside() and play() unless $is_raining;.Ve.PPThe \f(CW\*(C`foreach\*(C'\fR modifier is an iterator: it executes the statement oncefor each item in the \s-1LIST\s0 (with \f(CW$_\fR aliased to each item in turn)..PP.Vb 1\& print "Hello $_!\en" foreach qw(world Dolly nurse);.Ve.PP\&\f(CW\*(C`while\*(C'\fR repeats the statement \fIwhile\fR the condition is true.\&\f(CW\*(C`until\*(C'\fR does the opposite, it repeats the statement \fIuntil\fR thecondition is true (or while the condition is false):.PP.Vb 3\& # Both of these count from 0 to 10.\& print $i++ while $i <= 10;\& print $j++ until $j > 10;.Ve.PPThe \f(CW\*(C`while\*(C'\fR and \f(CW\*(C`until\*(C'\fR modifiers have the usual "\f(CW\*(C`while\*(C'\fR loop"semantics (conditional evaluated first), except when applied to a\&\f(CW\*(C`do\*(C'\fR\-BLOCK (or to the deprecated \f(CW\*(C`do\*(C'\fR\-SUBROUTINE statement), inwhich case the block executes once before the conditional isevaluated. This is so that you can write loops like:.PP.Vb 4\& do {\& $line = <STDIN>;\& ...\& } until $line eq ".\en";.Ve.PPSee \*(L"do\*(R" in perlfunc. Note also that the loop control statements describedlater will \fI\s-1NOT\s0\fR work in this construct, because modifiers don't takeloop labels. Sorry. You can always put another block inside of it(for \f(CW\*(C`next\*(C'\fR) or around it (for \f(CW\*(C`last\*(C'\fR) to do that sort of thing.For \f(CW\*(C`next\*(C'\fR, just double the braces:.IX Xref "next last redo".PP.Vb 4\& do {{\& next if $x == $y;\& # do something here\& }} until $x++ > $z;.Ve.PPFor \f(CW\*(C`last\*(C'\fR, you have to be more elaborate:.IX Xref "last".PP.Vb 6\& LOOP: { \& do {\& last if $x = $y**2;\& # do something here\& } while $x++ <= $z;\& }.Ve.PP\&\fB\s-1NOTE:\s0\fR The behaviour of a \f(CW\*(C`my\*(C'\fR statement modified with a statementmodifier conditional or loop construct (e.g. \f(CW\*(C`my $x if ...\*(C'\fR) is\&\fBundefined\fR. The value of the \f(CW\*(C`my\*(C'\fR variable may be \f(CW\*(C`undef\*(C'\fR, anypreviously assigned value, or possibly anything else. Don't rely onit. Future versions of perl might do something different from theversion of perl you try it out on. Here be dragons..IX Xref "my".Sh "Compound Statements".IX Xref "statement, compound block bracket, curly curly bracket brace { } if unless while until foreach for continue".IX Subsection "Compound Statements"In Perl, a sequence of statements that defines a scope is called a block.Sometimes a block is delimited by the file containing it (in the caseof a required file, or the program as a whole), and sometimes a blockis delimited by the extent of a string (in the case of an eval)..PPBut generally, a block is delimited by curly brackets, also known as braces.We will call this syntactic construct a \s-1BLOCK\s0..PPThe following compound statements may be used to control flow:.PP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -