📄 perldata.1
字号:
\& warn "not a decimal number" unless /^\-?(?:\ed+(?:\e.\ed*)?|\e.\ed+)$/;\& warn "not a C float"\& unless /^([+\-]?)(?=\ed|\e.\ed)\ed*(\e.\ed*)?([Ee]([+\-]?\ed+))?$/;.Ve.PPThe length of an array is a scalar value. You may find the lengthof array \f(CW@days\fR by evaluating \f(CW$#days\fR, as in \fBcsh\fR. However, thisisn't the length of the array; it's the subscript of the last element,which is a different value since there is ordinarily a 0th element.Assigning to \f(CW$#days\fR actually changes the length of the array.Shortening an array this way destroys intervening values. Lengtheningan array that was previously shortened does not recover valuesthat were in those elements. (It used to do so in Perl 4, but wehad to break this to make sure destructors were called when expected.).IX Xref "$# array, length".PPYou can also gain some minuscule measure of efficiency by pre-extendingan array that is going to get big. You can also extend an arrayby assigning to an element that is off the end of the array. Youcan truncate an array down to nothing by assigning the null list() to it. The following are equivalent:.PP.Vb 2\& @whatever = ();\& $#whatever = \-1;.Ve.PPIf you evaluate an array in scalar context, it returns the lengthof the array. (Note that this is not true of lists, which returnthe last value, like the C comma operator, nor of built-in functions,which return whatever they feel like returning.) The following isalways true:.IX Xref "array, length".PP.Vb 1\& scalar(@whatever) == $#whatever \- $[ + 1;.Ve.PPVersion 5 of Perl changed the semantics of \f(CW$[\fR: files that don't setthe value of \f(CW$[\fR no longer need to worry about whether anotherfile changed its value. (In other words, use of \f(CW$[\fR is deprecated.)So in general you can assume that.IX Xref "$[".PP.Vb 1\& scalar(@whatever) == $#whatever + 1;.Ve.PPSome programmers choose to use an explicit conversion so as to leave nothing to doubt:.PP.Vb 1\& $element_count = scalar(@whatever);.Ve.PPIf you evaluate a hash in scalar context, it returns false if thehash is empty. If there are any key/value pairs, it returns true;more precisely, the value returned is a string consisting of thenumber of used buckets and the number of allocated buckets, separatedby a slash. This is pretty much useful only to find out whetherPerl's internal hashing algorithm is performing poorly on your dataset. For example, you stick 10,000 things in a hash, but evaluating\&\f(CW%HASH\fR in scalar context reveals \f(CW"1/16"\fR, which means only one outof sixteen buckets has been touched, and presumably contains all10,000 of your items. This isn't supposed to happen. If a tied hashis evaluated in scalar context, a fatal error will result, since thisbucket usage information is currently not available for tied hashes..IX Xref "hash, scalar context hash, bucket bucket".PPYou can preallocate space for a hash by assigning to the \fIkeys()\fR function.This rounds up the allocated buckets to the next power of two:.PP.Vb 1\& keys(%users) = 1000; # allocate 1024 buckets.Ve.Sh "Scalar value constructors".IX Xref "scalar, literal scalar, constant".IX Subsection "Scalar value constructors"Numeric literals are specified in any of the following floating point orinteger formats:.PP.Vb 9\& 12345\& 12345.67\& .23E\-10 # a very small number\& 3.14_15_92 # a very important number\& 4_294_967_296 # underscore for legibility\& 0xff # hex\& 0xdead_beef # more hex \& 0377 # octal (only numbers, begins with 0)\& 0b011011 # binary.Ve.PPYou are allowed to use underscores (underbars) in numeric literalsbetween digits for legibility. You could, for example, group binarydigits by threes (as for a Unix-style mode argument such as 0b110_100_100)or by fours (to represent nibbles, as in 0b1010_0110) or in other groups..IX Xref "number, literal".PPString literals are usually delimited by either single or doublequotes. They work much like quotes in the standard Unix shells:double-quoted string literals are subject to backslash and variablesubstitution; single-quoted strings are not (except for \f(CW\*(C`\e\*(Aq\*(C'\fR and\&\f(CW\*(C`\e\e\*(C'\fR). The usual C\-style backslash rules apply for makingcharacters such as newline, tab, etc., as well as some more exoticforms. See \*(L"Quote and Quote-like Operators\*(R" in perlop for a list..IX Xref "string, literal".PPHexadecimal, octal, or binary, representations in string literals(e.g. '0xff') are not automatically converted to their integerrepresentation. The \fIhex()\fR and \fIoct()\fR functions make these conversionsfor you. See \*(L"hex\*(R" in perlfunc and \*(L"oct\*(R" in perlfunc for more details..PPYou can also embed newlines directly in your strings, i.e., they can endon a different line than they begin. This is nice, but if you forgetyour trailing quote, the error will not be reported until Perl findsanother line containing the quote character, which may be much furtheron in the script. Variable substitution inside strings is limited toscalar variables, arrays, and array or hash slices. (In other words,names beginning with $ or @, followed by an optional bracketedexpression as a subscript.) The following code segment prints out \*(L"Theprice is \f(CW$100\fR.\*(R".IX Xref "interpolation".PP.Vb 2\& $Price = \*(Aq$100\*(Aq; # not interpolated\& print "The price is $Price.\en"; # interpolated.Ve.PPThere is no double interpolation in Perl, so the \f(CW$100\fR is left as is..PPBy default floating point numbers substituted inside strings use thedot (\*(L".\*(R") as the decimal separator. If \f(CW\*(C`use locale\*(C'\fR is in effect,and \fIPOSIX::setlocale()\fR has been called, the character used for thedecimal separator is affected by the \s-1LC_NUMERIC\s0 locale.See perllocale and \s-1POSIX\s0..PPAs in some shells, you can enclose the variable name in braces todisambiguate it from following alphanumerics (and underscores).You must also dothis when interpolating a variable into a string to separate thevariable name from a following double-colon or an apostrophe, sincethese would be otherwise treated as a package separator:.IX Xref "interpolation".PP.Vb 3\& $who = "Larry";\& print PASSWD "${who}::0:0:Superuser:/:/bin/perl\en";\& print "We use ${who}speak when ${who}\*(Aqs here.\en";.Ve.PPWithout the braces, Perl would have looked for a \f(CW$whospeak\fR, a\&\f(CW$who::0\fR, and a \f(CW\*(C`$who\*(Aqs\*(C'\fR variable. The last two would be the\&\f(CW$0\fR and the \f(CW$s\fR variables in the (presumably) non-existent package\&\f(CW\*(C`who\*(C'\fR..PPIn fact, an identifier within such curlies is forced to be a string,as is any simple identifier within a hash subscript. Neither needquoting. Our earlier example, \f(CW$days{\*(AqFeb\*(Aq}\fR can be written as\&\f(CW$days{Feb}\fR and the quotes will be assumed automatically. Butanything more complicated in the subscript will be interpreted as anexpression. This means for example that \f(CW\*(C`$version{2.0}++\*(C'\fR isequivalent to \f(CW\*(C`$version{2}++\*(C'\fR, not to \f(CW\*(C`$version{\*(Aq2.0\*(Aq}++\*(C'\fR..PP\fIVersion Strings\fR.IX Xref "version string vstring v-string".IX Subsection "Version Strings".PP\&\fBNote:\fR Version Strings (v\-strings) have been deprecated. They willbe removed in some future release after Perl 5.8.1. The marginalbenefits of v\-strings were greatly outweighed by the potential forSurprise and Confusion..PPA literal of the form \f(CW\*(C`v1.20.300.4000\*(C'\fR is parsed as a string composedof characters with the specified ordinals. This form, known asv\-strings, provides an alternative, more readable way to constructstrings, rather than use the somewhat less readable interpolation form\&\f(CW"\ex{1}\ex{14}\ex{12c}\ex{fa0}"\fR. This is useful for representingUnicode strings, and for comparing version \*(L"numbers\*(R" using the stringcomparison operators, \f(CW\*(C`cmp\*(C'\fR, \f(CW\*(C`gt\*(C'\fR, \f(CW\*(C`lt\*(C'\fR etc. If there are two ormore dots in the literal, the leading \f(CW\*(C`v\*(C'\fR may be omitted..PP.Vb 3\& print v9786; # prints SMILEY, "\ex{263a}"\& print v102.111.111; # prints "foo"\& print 102.111.111; # same.Ve.PPSuch literals are accepted by both \f(CW\*(C`require\*(C'\fR and \f(CW\*(C`use\*(C'\fR fordoing a version check. Note that using the v\-strings for IPv4addresses is not portable unless you also use the\&\fIinet_aton()\fR/\fIinet_ntoa()\fR routines of the Socket package..PPNote that since Perl 5.8.1 the single-number v\-strings (like \f(CW\*(C`v65\*(C'\fR)are not v\-strings before the \f(CW\*(C`=>\*(C'\fR operator (which is usually usedto separate a hash key from a hash value), instead they are interpretedas literal strings ('v65'). They were v\-strings from Perl 5.6.0 toPerl 5.8.0, but that caused more confusion and breakage than good.Multi-number v\-strings like \f(CW\*(C`v65.66\*(C'\fR and \f(CW65.66.67\fR continue tobe v\-strings always..PP\fISpecial Literals\fR.IX Xref "special literal __END__ __DATA__ END DATA end data ^D ^Z".IX Subsection "Special Literals".PPThe special literals _\|_FILE_\|_, _\|_LINE_\|_, and _\|_PACKAGE_\|_represent the current filename, line number, and package name at thatpoint in your program. They may be used only as separate tokens; theywill not be interpolated into strings. If there is no current package(due to an empty \f(CW\*(C`package;\*(C'\fR directive), _\|_PACKAGE_\|_ is the undefinedvalue..IX Xref "__FILE__ __LINE__ __PACKAGE__ line file package".PPThe two control characters ^D and ^Z, and the tokens _\|_END_\|_ and _\|_DATA_\|_may be used to indicate the logical end of the script before the actualend of file. Any following text is ignored..PPText after _\|_DATA_\|_ but may be read via the filehandle \f(CW\*(C`PACKNAME::DATA\*(C'\fR,where \f(CW\*(C`PACKNAME\*(C'\fR is the package that was current when the _\|_DATA_\|_token was encountered. The filehandle is left open pointing to thecontents after _\|_DATA_\|_. It is the program's responsibility to\&\f(CW\*(C`close DATA\*(C'\fR when it is done reading from it. For compatibility witholder scripts written before _\|_DATA_\|_ was introduced, _\|_END_\|_ behaveslike _\|_DATA_\|_ in the top level script (but not in files loaded with\&\f(CW\*(C`require\*(C'\fR or \f(CW\*(C`do\*(C'\fR) and leaves the remaining contents of thefile accessible via \f(CW\*(C`main::DATA\*(C'\fR..PPSee SelfLoader for more description of _\|_DATA_\|_, andan example of its use. Note that you cannot read from the \s-1DATA\s0filehandle in a \s-1BEGIN\s0 block: the \s-1BEGIN\s0 block is executed as soonas it is seen (during compilation), at which point the corresponding_\|_DATA_\|_ (or _\|_END_\|_) token has not yet been seen..PP\fIBarewords\fR.IX Xref "bareword".IX Subsection "Barewords".PPA word that has no other interpretation in the grammar willbe treated as if it were a quoted string. These are known as\&\*(L"barewords\*(R". As with filehandles and labels, a bareword that consistsentirely of lowercase letters risks conflict with future reservedwords, and if you use the \f(CW\*(C`use warnings\*(C'\fR pragma or the \fB\-w\fR switch, Perl will warn you about anysuch words. Some people may wish to outlaw barewords entirely. If yousay.PP.Vb 1\& use strict \*(Aqsubs\*(Aq;.Ve.PPthen any bareword that would \s-1NOT\s0 be interpreted as a subroutine callproduces a compile-time error instead. The restriction lasts to theend of the enclosing block. An inner block may countermand thisby saying \f(CW\*(C`no strict \*(Aqsubs\*(Aq\*(C'\fR..PP\fIArray Joining Delimiter\fR.IX Xref "array, interpolation interpolation, array $""".IX Subsection "Array Joining Delimiter".PPArrays and slices are interpolated into double-quoted stringsby joining the elements with the delimiter specified in the \f(CW$"\fRvariable (\f(CW$LIST_SEPARATOR\fR if \*(L"use English;\*(R" is specified), space by default. The following are equivalent:.PP.Vb 2\& $temp = join($", @ARGV);\& system "echo $temp";\&\& system "echo @ARGV";.Ve.PPWithin search patterns (which also undergo double-quotish substitution)there is an unfortunate ambiguity: Is \f(CW\*(C`/$foo[bar]/\*(C'\fR to be interpreted as\&\f(CW\*(C`/${foo}[bar]/\*(C'\fR (where \f(CW\*(C`[bar]\*(C'\fR is a character class for the regularexpression) or as \f(CW\*(C`/${foo[bar]}/\*(C'\fR (where \f(CW\*(C`[bar]\*(C'\fR is the subscript to array\&\f(CW@foo\fR)? If \f(CW@foo\fR doesn't otherwise exist, then it's obviously acharacter class. If \f(CW@foo\fR exists, Perl takes a good guess about \f(CW\*(C`[bar]\*(C'\fR,and is almost always right. If it does guess wrong, or if you're justplain paranoid, you can force the correct interpretation with curlybraces as above..PPIf you're looking for the information on how to use here-documents,which used to be here, that's been moved to\&\*(L"Quote and Quote-like Operators\*(R" in perlop..Sh "List value constructors".IX Xref "list".IX Subsection "List value constructors"List values are denoted by separating individual values by commas(and enclosing the list in parentheses where precedence requires it):.PP.Vb 1\& (LIST).Ve.PPIn a context not requiring a list value, the value of what appearsto be a list literal is simply the value of the final element, aswith the C comma operator. For example,.PP.Vb 1\& @foo = (\*(Aqcc\*(Aq, \*(Aq\-E\*(Aq, $bar);.Ve.PPassigns the entire list value to array \f(CW@foo\fR, but.PP.Vb 1\& $foo = (\*(Aqcc\*(Aq, \*(Aq\-E\*(Aq, $bar);.Ve.PPassigns the value of variable \f(CW$bar\fR to the scalar variable \f(CW$foo\fR.Note that the value of an actual array in scalar context is thelength of the array; the following assigns the value 3 to \f(CW$foo:\fR.PP.Vb 2\& @foo = (\*(Aqcc\*(Aq, \*(Aq\-E\*(Aq, $bar);\& $foo = @foo; # $foo gets 3.Ve.PPYou may have an optional comma before the closing parenthesis of alist literal, so that you can say:.PP.Vb 5\& @foo = (\& 1,\& 2,\& 3,\& );.Ve.PPTo use a here-document to assign an array, one line per element,you might use an approach like this:.PP.Vb 7\& @sauces = <<End_Lines =~ m/(\eS.*\eS)/g;\& normal tomato\& spicy tomato\& green chile\& pesto\& white wine\& End_Lines.Ve.PPLISTs do automatic interpolation of sublists. That is, when a \s-1LIST\s0 isevaluated, each element of the list is evaluated in list context, andthe resulting list value is interpolated into \s-1LIST\s0 just as if eachindividual element were a member of \s-1LIST\s0. Thus arrays and hashes lose theiridentity in a LIST\*(--the list.PP.Vb 1\& (@foo,@bar,&SomeSub,%glarch).Ve.PPcontains all the elements of \f(CW@foo\fR followed by all the elements of \f(CW@bar\fR,followed by all the elements returned by the subroutine named SomeSub called in list context, followed by the key/value pairs of \f(CW%glarch\fR.To make a list reference that does \fI\s-1NOT\s0\fR interpolate, see perlref..PPThe null list is represented by (). Interpolating it in a listhas no effect. Thus ((),(),()) is equivalent to (). Similarly,interpolating an array with no elements is the same as if noarray had been interpolated at that point..PPThis interpolation combines with the facts that the openingand closing parentheses are optional (except when necessary forprecedence) and lists may end with an optional comma to mean thatmultiple commas within lists are legal syntax. The list \f(CW\*(C`1,,3\*(C'\fR is aconcatenation of two lists, \f(CW\*(C`1,\*(C'\fR and \f(CW3\fR, the first of which endswith that optional comma. \f(CW\*(C`1,,3\*(C'\fR is \f(CW\*(C`(1,),(3)\*(C'\fR is \f(CW\*(C`1,3\*(C'\fR (Andsimilarly for \f(CW\*(C`1,,,3\*(C'\fR is \f(CW\*(C`(1,),(,),3\*(C'\fR is \f(CW\*(C`1,3\*(C'\fR and so on.) Not thatwe'd advise you to use this obfuscation.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -