📄 perl.man
字号:
The shell executes the second line as a normal shell command, and thusstarts up the.I perlinterpreter.On some systems $0 doesn't always contain the full pathname,so the.B \-Stells.I perlto search for the script if necessary.After.I perllocates the script, it parses the lines and ignores them becausethe variable $running_under_some_shell is never true.A better construct than $* would be ${1+"$@"}, which handles embedded spacesand such in the filenames, but doesn't work if the script is being interpretedby csh.In order to start up sh rather than csh, some systems may have to replace the#! line with a line containing justa colon, which will be politely ignored by perl.Other systems can't control that, and need a totally devious construct thatwill work under any of csh, sh or perl, such as the following:.nf.ne 3 eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' & eval 'exec /usr/bin/perl -S $0 $argv:q' if 0;.fi.TP 5.B \-ucauses.I perlto dump core after compiling your script.You can then take this core dump and turn it into an executable fileby using the undump program (not supplied).This speeds startup at the expense of some disk space (which you canminimize by stripping the executable).(Still, a "hello world" executable comes out to about 200K on my machine.)If you are going to run your executable as a set-id program then youshould probably compile it using taintperl rather than normal perl.If you want to execute a portion of your script before dumping, use thedump operator instead.Note: availability of undump is platform specific and may not be availablefor a specific port of perl..TP 5.B \-Uallows.I perlto do unsafe operations.Currently the only \*(L"unsafe\*(R" operations are the unlinking of directories whilerunning as superuser, and running setuid programs with fatal taint checksturned into warnings..TP 5.B \-vprints the version and patchlevel of your.I perlexecutable..TP 5.B \-wprints warnings about identifiers that are mentioned only once, and scalarvariables that are used before being set.Also warns about redefined subroutines, and references to undefinedfilehandles or filehandles opened readonly that you are attempting towrite on.Also warns you if you use == on values that don't look like numbers, and ifyour subroutines recurse more than 100 deep..TP 5.BI \-x directorytells.I perlthat the script is embedded in a message.Leading garbage will be discarded until the first line that startswith #! and contains the string "perl".Any meaningful switches on that line will be applied (but only onegroup of switches, as with normal #! processing).If a directory name is specified, Perl will switch to that directorybefore running the script.The.B \-xswitch only controls the the disposal of leading garbage.The script must be terminated with _\|_END_\|_ if there is trailing garbageto be ignored (the script can process any or all of the trailing garbagevia the DATA filehandle if desired)..Sh "Data Types and Objects".PP.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.fiArray literals are denoted by separating individual values by commas, andenclosing the list in parentheses:.nf (LIST).fiIn a context not requiring an array value, the value of the array literalis the value of the final element, as in the C comma operator.For example,.nf.ne 4 @foo = (\'cc\', \'\-E\', $bar);assigns the entire array value to array foo, but $foo = (\'cc\', \'\-E\', $bar);.fiassigns the value of variable bar to variable foo.Note that the value of an actual array in a scalar context is the lengthof the array; the following assigns to $foo the value 3:.nf.ne 2 @foo = (\'cc\', \'\-E\', $bar); $foo = @foo; # $foo gets 3.fiYou may have an optional comma before the closing parenthesis of anarray literal, so that you can say:.nf @foo = ( 1, 2, 3, );.fiWhen a LIST is evaluated, each element of the list is evaluated inan array context, and the resulting array value is interpolated into LISTjust as if each individual element were a member of LIST. Thus arrayslose their identity in a LIST\*(--the list (@foo,@bar,&SomeSub)contains all the elements of @foo followed by all the elements of @bar,followed by all the elements returned by the subroutine named SomeSub..PPA list value may also be subscripted like a normal array.Examples:.nf $time = (stat($file))[8]; # stat returns array value $digit = ('a','b','c','d','e','f')[$digit-10]; return (pop(@foo),pop(@foo))[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -