📄 perl.ms
字号:
.\" This document is trivally reformatted from perl.man.\".\" $RCSfile: perl.man,v $$Revision: 4.0.1.6 $$Date: 92/06/08 15:07:29 $.\" .\" $Log: perl.man,v $.\" Revision 4.0.1.6 92/06/08 15:07:29 lwall.\" patch20: documented that numbers may contain underline.\" patch20: clarified that DATA may only be read from main script.\" patch20: relaxed requirement for semicolon at the end of a block.\" patch20: added ... as variant on ...\" patch20: documented need for 1; at the end of a required file.\" patch20: extended bracket-style quotes to two-arg operators: s()() and tr()().\" patch20: paragraph mode now skips extra newlines automatically.\" patch20: documented PERLLIB and PERLDB.\" patch20: documented limit on size of regexp.\" .\" Revision 4.0.1.5 91/11/11 16:42:00 lwall.\" patch19: added little-endian pack/unpack options.\" .\" Revision 4.0.1.4 91/11/05 18:11:05 lwall.\" patch11: added sort {} LIST.\" patch11: added eval {}.\" patch11: documented meaning of scalar(%foo).\" patch11: sprintf() now supports any length of s field.\" .\" Revision 4.0.1.3 91/06/10 01:26:02 lwall.\" patch10: documented some newer features in addenda.\" .\" Revision 4.0.1.2 91/06/07 11:41:23 lwall.\" patch4: added global modifier for pattern matches.\" patch4: default top-of-form format is now FILEHANDLE_TOP.\" patch4: added $^P variable to control calling of perldb routines.\" patch4: added $^F variable to specify maximum system fd, default 2.\" patch4: changed old $^P to $^X.\" .\" Revision 4.0.1.1 91/04/11 17:50:44 lwall.\" patch1: fixed some typos.\" .\" Revision 4.0 91/03/20 01:38:08 lwall.\" 4.0 baseline..\" .\" .de IR\fI\\$1\fR\\$2...de Sh.NH\fB\s+1\\$1\s-1\fR.PP...de Sp.if t .sp .5v.if n .sp...de Ip.br.ie \\n(.$>=3 .ne \\$3.el .ne 3.IP "\\$1" \\$2...\".\" Set up \*(-- to give an unbreakable dash;.\" string Tr holds user defined translation string..\" Bell System Logo is used as a dummy character..\".tr \(*W-|\(bv\*(Tr.ie n \{\.ds -- \(*W-.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 L' '.ds R' ''br\}.el\{\.ds -- \(em\|.tr \*(Tr.ds L" ``.ds R" ''.ds L' `.ds R' ''br\}.TLThe PERL Programming Language.AULarry Wall.AI<lwall@netlabs.com>.ABThe Practical Extraction and Report Language (perl)is an interpreted language optimized for scanning arbitrary text files,extracting information from those text files, and printing reports basedon that information.It is also a good language for many system management tasks.The language is intended to be practical (easy to use, efficient, complete)rather than beautiful (tiny, elegant, minimal).It combines (in the author's opinion, anyway) some of the best features of C,\fIsed\fR, \fIawk\fR, and \fIsh\fR,so people familiar with those languages should have little difficulty with it.(Language historians will also note some vestiges of \fIcsh\fR, Pascal, andeven BASIC-PLUS.)Expression syntax corresponds quite closely to C expression syntax.Unlike most Unix utilities,.I perldoes not arbitrarily limit the size of your data\*(--if you've gotthe memory,.I perlcan slurp in your whole file as a single string.Recursion is of unlimited depth.And the hash tables used by associative arrays grow as necessary to preventdegraded performance..I Perluses sophisticated pattern matching techniques to scan large amounts ofdata very quickly.Although optimized for scanning text,.I perlcan also deal with binary data, and can make dbm files look like associativearrays (where dbm is available).Setuid.I perlscripts are safer than C programsthrough a dataflow tracing mechanism which prevents many stupid security holes.If you have a problem that would ordinarily use \fIsed\fRor \fIawk\fR or \fIsh\fR, but itexceeds their capabilities or must run a little faster,and you don't want to write the silly thing in C, then.I perlmay be for you.There are also translators to turn your.I sedand.I awkscripts into.I perlscripts..AE.OH 'The PERL Programming Language''SMM:19-%'.EH 'SMM:19-%''The PERL Programming Language'.Sh "Data Types and Objects".I Perlhas three data types: scalars, arrays of scalars, andassociative arrays of scalars.Normal arrays are indexed by number, and associative arrays by string..PPThe interpretation of operations and values in perl sometimesdepends on the requirementsof the context around the operation or value.There are three major contexts: string, numeric and array.Certain operations return array valuesin contexts wanting an array, and scalar values otherwise.(If this is true of an operation it will be mentioned in the documentationfor that operation.)Operations which return scalars don't care whether the context is lookingfor a string or a number, butscalar variables and values are interpreted as strings or numbersas appropriate to the context.A scalar is interpreted as TRUE in the boolean sense if it is not the nullstring or 0.Booleans returned by operators are 1 for true and 0 or \'\' (the nullstring) for false..PPThere are actually two varieties of null string: defined and undefined.Undefined null strings are returned when there is no real value for something,such as when there was an error, or at end of file, or when you referto an uninitialized variable or element of an array.An undefined null string may become defined the first time you access it, butprior to that you can use the defined() operator to determine whether thevalue is defined or not..PPReferences to scalar variables always begin with \*(L'$\*(R', even when referringto a scalar that is part of an array.Thus:.nf.ne 3 $days \h'|2i'# a simple scalar variable $days[28] \h'|2i'# 29th element of array @days $days{\'Feb\'}\h'|2i'# one value from an associative array $#days \h'|2i'# last index of array @daysbut entire arrays or array slices are denoted by \*(L'@\*(R': @days \h'|2i'# ($days[0], $days[1],\|.\|.\|. $days[n]) @days[3,4,5]\h'|2i'# same as @days[3.\|.5] @days{'a','c'}\h'|2i'# same as ($days{'a'},$days{'c'})and entire associative arrays are denoted by \*(L'%\*(R': %days \h'|2i'# (key1, val1, key2, val2 .\|.\|.).fi.PPAny of these eight constructs may serve as an lvalue,that is, may be assigned to.(It also turns out that an assignment is itself an lvalue incertain contexts\*(--see examples under s, tr and chop.)Assignment to a scalar evaluates the righthand side in a scalar context,while assignment to an array or array slice evaluates the righthand sidein an array context..PPYou may find the length of array @days by evaluating\*(L"$#days\*(R", as in.IR csh .(Actually, it's not the length of the array, it's the subscript of the last element, since there is (ordinarily) a 0th element.)Assigning to $#days changes the length of the array.Shortening an array by this method does not actually destroy any values.Lengthening an array that was previously shortened recovers the values thatwere in those elements.You can also gain some measure of efficiency by preextending an array thatis going to get big.(You can also extend an array by assigning to an element that is off theend of the array.This differs from assigning to $#whatever in that intervening valuesare set to null rather than recovered.)You can truncate an array down to nothing by assigning the null list () toit.The following are exactly equivalent.nf @whatever = (); $#whatever = $[ \- 1;.fi.PPIf you evaluate an array in a scalar context, it returns the length ofthe array.The following is always true:.nf scalar(@whatever) == $#whatever \- $[ + 1;.fiIf you evaluate an associative array in a scalar context, it returnsa value which is true if and only if the array contains any elements.(If there are any elements, the value returned is a string consistingof the number of used buckets and the number of allocated buckets, separatedby a slash.).PPMulti-dimensional arrays are not directly supported, but see the discussionof the $; variable later for a means of emulating multiple subscripts withan associative array.You could also write a subroutine to turn multiple subscripts into a singlesubscript..PPEvery data type has its own namespace.You can, without fear of conflict, use the same name for a scalar variable,an array, an associative array, a filehandle, a subroutine name, and/ora label.Since variable and array references always start with \*(L'$\*(R', \*(L'@\*(R',or \*(L'%\*(R', the \*(L"reserved\*(R" words aren't in fact reservedwith respect to variable names.(They ARE reserved with respect to labels and filehandles, however, whichdon't have an initial special character.Hint: you could say open(LOG,\'logfile\') rather than open(log,\'logfile\').Using uppercase filehandles also improves readability and protects youfrom conflict with future reserved words.)Case IS significant\*(--\*(L"FOO\*(R", \*(L"Foo\*(R" and \*(L"foo\*(R" are alldifferent names.Names which start with a letter may also contain digits and underscores.Names which do not start with a letter are limited to one character,e.g. \*(L"$%\*(R" or \*(L"$$\*(R".(Most of the one character names have a predefined significance to.IR perl .More later.).PPNumeric literals are specified in any of the usual floating point orinteger formats:.nf.ne 6 12345 12345.67 .23E-10 0xffff # hex 0377 # octal 4_294_967_296.fiString literals are delimited by either single or double quotes.They work much like shell quotes:double-quoted string literals are subject to backslash and variablesubstitution; single-quoted strings are not (except for \e\' and \e\e).The usual backslash rules apply for making characters such as newline, tab,etc., as well as some more exotic forms:.nf \et tab \en newline \er return \ef form feed \eb backspace \ea alarm (bell) \ee escape \e033 octal char \ex1b hex char \ec[ control char \el lowercase next char \eu uppercase next char \eL lowercase till \eE \eU uppercase till \eE \eE end case modification.fiYou can also embed newlines directly in your strings, i.e. they can end ona different line than they begin.This is nice, but if you forget your trailing quote, the error will not bereported until.I perlfinds another line containing the quote character, whichmay be much further on in the script.Variable substitution inside strings is limited to scalar variables, normalarray values, and array slices.(In other words, identifiers beginning with $ or @, followed by an optionalbracketed expression as a subscript.)The following code segment prints out \*(L"The price is $100.\*(R".nf.ne 2 $Price = \'$100\';\h'|3.5i'# not interpreted print "The price is $Price.\e\|n";\h'|3.5i'# interpreted.fiNote that you can put curly brackets around the identifier to delimit itfrom following alphanumerics.Also note that a single quoted string must be separated from a precedingword by a space, since single quote is a valid character in an identifier(see Packages)..PPTwo special literals are _\|_LINE_\|_ and _\|_FILE_\|_, which represent the currentline number and filename at that point in your program.They may only be used as separate tokens; they will not be interpolatedinto strings.In addition, the token _\|_END_\|_ may be used to indicate the logical end of thescript before the actual end of file.Any following text is ignored, but may be read via the DATA filehandle.(The DATA filehandle may read data only from the main script, but not fromany required file or evaluated string.)The two control characters ^D and ^Z are synonyms for _\|_END_\|_..PPA word that doesn't have any other interpretation in the grammar will betreated as if it had single quotes around it.For this purpose, a word consists only of alphanumeric characters and underline,and must start with an alphabetic character.As with filehandles and labels, a bare word that consists entirely oflowercase letters risks conflict with future reserved words, and if youuse the.B \-wswitch, Perl will warn you about any such words..PPArray values are interpolated into double-quoted strings by joining all theelements of the array with the delimiter specified in the $" variable,space by default.(Since in versions of perl prior to 3.0 the @ character was not a metacharacterin double-quoted strings, the interpolation of @array, $array[EXPR],@array[LIST], $array{EXPR}, or @array{LIST} only happens if array isreferenced elsewhere in the program or is predefined.)The following are equivalent:.nf.ne 4 $temp = join($",@ARGV); system "echo $temp"; system "echo @ARGV";.fiWithin search patterns (which also undergo double-quotish substitution)there is a bad ambiguity: Is /$foo[bar]/ to beinterpreted as /${foo}[bar]/ (where [bar] is a character class for theregular expression) or as /${foo[bar]}/ (where [bar] is the subscript toarray @foo)?If @foo doesn't otherwise exist, then it's obviously a character class.If @foo exists, perl takes a good guess about [bar], and is almost always right.If it does guess wrong, or if you're just plain paranoid,you can force the correct interpretation with curly brackets as above..PPA line-oriented form of quoting is based on the shell here-is syntax.Following a << you specify a string to terminate the quoted material, and all linesfollowing the current line down to the terminating string are the valueof the item.The terminating string may be either an identifier (a word), or somequoted text.If quoted, the type of quotes you use determines the treatment of the text,just as in regular quoting.An unquoted identifier works like double quotes.There must be no space between the << and the identifier.(If you put a space it will be treated as a null identifier, which isvalid, and matches the first blank line\*(--see Merry Christmas example below.)The terminating string must appear by itself (unquoted and with no surroundingwhitespace) on the terminating line..nf print <<EOF; # same as aboveThe price is $Price.EOF print <<"EOF"; # same as aboveThe price is $Price.EOF print << x 10; # null identifier is delimiterMerry Christmas! print <<`EOC`; # execute commandsecho hi thereecho lo thereEOC print <<foo, <<bar; # you can stack themI said foo.fooI said bar.bar
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -