📄 perldata.pod
字号:
=head1 NAMEperldata - Perl data types=head1 DESCRIPTION=head2 Variable namesX<variable, name> X<variable name> X<data type> X<type>Perl has three built-in data types: scalars, arrays of scalars, andassociative arrays of scalars, known as "hashes". A scalar is a single string (of any size, limited only by the available memory),number, or a reference to something (which will be discussedin L<perlref>). Normal arrays are ordered lists of scalars indexedby number, starting with 0. Hashes are unordered collections of scalar values indexed by their associated string key.Values are usually referred to by name, or through a named reference.The first character of the name tells you to what sort of datastructure it refers. The rest of the name tells you the particularvalue to which it refers. Usually this name is a single I<identifier>,that is, a string beginning with a letter or underscore, andcontaining letters, underscores, and digits. In some cases, it maybe a chain of identifiers, separated by C<::> (or by the slightlyarchaic C<'>); all but the last are interpreted as names of packages,to locate the namespace in which to look up the final identifier(see L<perlmod/Packages> for details). It's possible to substitutefor a simple identifier, an expression that produces a referenceto the value at runtime. This is described in more detail belowand in L<perlref>.X<identifier>Perl also has its own built-in variables whose names don't followthese rules. They have strange names so they don't accidentallycollide with one of your normal variables. Strings that matchparenthesized parts of a regular expression are saved under namescontaining only digits after the C<$> (see L<perlop> and L<perlre>).In addition, several special variables that provide windows intothe inner working of Perl have names containing punctuation charactersand control characters. These are documented in L<perlvar>.X<variable, built-in>Scalar values are always named with '$', even when referring to ascalar that is part of an array or a hash. The '$' symbol workssemantically like the English word "the" in that it indicates asingle value is expected.X<scalar> $days # the simple scalar value "days" $days[28] # the 29th element of array @days $days{'Feb'} # the 'Feb' value from hash %days $#days # the last index of array @daysEntire arrays (and slices of arrays and hashes) are denoted by '@',which works much like the word "these" or "those" does in English,in that it indicates multiple values are expected.X<array> @days # ($days[0], $days[1],... $days[n]) @days[3,4,5] # same as ($days[3],$days[4],$days[5]) @days{'a','c'} # same as ($days{'a'},$days{'c'})Entire hashes are denoted by '%':X<hash> %days # (key1, val1, key2, val2 ...)In addition, subroutines are named with an initial '&', though thisis optional when unambiguous, just as the word "do" is often redundantin English. Symbol table entries can be named with an initial '*',but you don't really care about that yet (if ever :-).Every variable type has its own namespace, as do severalnon-variable identifiers. This means that you can, without fearof conflict, use the same name for a scalar variable, an array, ora hash--or, for that matter, for a filehandle, a directory handle, asubroutine name, a format name, or a label. This means that $fooand @foo are two different variables. It also means that C<$foo[1]>is a part of @foo, not a part of $foo. This may seem a bit weird,but that's okay, because it is weird.X<namespace>Because variable references always start with '$', '@', or '%', the"reserved" words aren't in fact reserved with respect to variablenames. They I<are> reserved with respect to labels and filehandles,however, which don't have an initial special character. You can'thave a filehandle named "log", for instance. Hint: you could sayC<open(LOG,'logfile')> rather than C<open(log,'logfile')>. Usinguppercase filehandles also improves readability and protects youfrom conflict with future reserved words. Case I<is> significant--"FOO","Foo", and "foo" are all different names. Names that start with aletter or underscore may also contain digits and underscores.X<identifier, case sensitivity>X<case>It is possible to replace such an alphanumeric name with an expressionthat returns a reference to the appropriate type. For a descriptionof this, see L<perlref>.Names that start with a digit may contain only more digits. Namesthat do not start with a letter, underscore, digit or a caret (i.e.a control character) are limited to one character, e.g., C<$%> orC<$$>. (Most of these one character names have a predefinedsignificance to Perl. For instance, C<$$> is the current processid.)=head2 ContextX<context> X<scalar context> X<list context>The interpretation of operations and values in Perl sometimes dependson the requirements of the context around the operation or value.There are two major contexts: list and scalar. Certain operationsreturn list values in contexts wanting a list, and scalar valuesotherwise. If this is true of an operation it will be mentioned inthe documentation for that operation. In other words, Perl overloadscertain operations based on whether the expected return value issingular or plural. Some words in English work this way, like "fish"and "sheep".In a reciprocal fashion, an operation provides either a scalar or alist context to each of its arguments. For example, if you say int( <STDIN> )the integer operation provides scalar context for the <>operator, which responds by reading one line from STDIN and passing itback to the integer operation, which will then find the integer valueof that line and return that. If, on the other hand, you say sort( <STDIN> )then the sort operation provides list context for <>, whichwill proceed to read every line available up to the end of file, andpass that list of lines back to the sort routine, which will thensort those lines and return them as a list to whatever the contextof the sort was.Assignment is a little bit special in that it uses its left argumentto determine the context for the right argument. Assignment to ascalar evaluates the right-hand side in scalar context, whileassignment to an array or hash evaluates the righthand side in listcontext. Assignment to a list (or slice, which is just a listanyway) also evaluates the righthand side in list context.When you use the C<use warnings> pragma or Perl's B<-w> command-line option, you may see warningsabout useless uses of constants or functions in "void context".Void context just means the value has been discarded, such as astatement containing only C<"fred";> or C<getpwuid(0);>. It stillcounts as scalar context for functions that care whether or notthey're being called in list context.User-defined subroutines may choose to care whether they are beingcalled in a void, scalar, or list context. Most subroutines do notneed to bother, though. That's because both scalars and lists areautomatically interpolated into lists. See L<perlfunc/wantarray>for how you would dynamically discern your function's callingcontext.=head2 Scalar valuesX<scalar> X<number> X<string> X<reference>All data in Perl is a scalar, an array of scalars, or a hash ofscalars. A scalar may contain one single value in any of threedifferent flavors: a number, a string, or a reference. In general,conversion from one form to another is transparent. Although ascalar may not directly hold multiple values, it may contain areference to an array or hash which in turn contains multiple values.Scalars aren't necessarily one thing or another. There's no placeto declare a scalar variable to be of type "string", type "number",type "reference", or anything else. Because of the automaticconversion of scalars, operations that return scalars don't needto care (and in fact, cannot care) whether their caller is lookingfor a string, a number, or a reference. Perl is a contextuallypolymorphic language whose scalars can be strings, numbers, orreferences (which includes objects). Although strings and numbersare considered pretty much the same thing for nearly all purposes,references are strongly-typed, uncastable pointers with builtinreference-counting and destructor invocation.A scalar value is interpreted as TRUE in the Boolean sense if it is notthe null string or the number 0 (or its string equivalent, "0"). TheBoolean context is just a special kind of scalar context where no conversion to a string or a number is ever performed.X<boolean> X<bool> X<true> X<false> X<truth>There are actually two varieties of null strings (sometimes referredto as "empty" strings), a defined one and an undefined one. Thedefined version is just a string of length zero, such as C<"">.The undefined version is the value that indicates that there isno real value for something, such as when there was an error, orat end of file, or when you refer to an uninitialized variable orelement of an array or hash. Although in early versions of Perl,an undefined scalar could become defined when first used in aplace expecting a defined value, this no longer happens except forrare cases of autovivification as explained in L<perlref>. You canuse the defined() operator to determine whether a scalar value isdefined (this has no meaning on arrays or hashes), and the undef()operator to produce an undefined value.X<defined> X<undefined> X<undef> X<null> X<string, null>To find out whether a given string is a valid non-zero number, it'ssometimes enough to test it against both numeric 0 and also lexical"0" (although this will cause noises if warnings are on). That's because strings that aren't numbers count as 0, just as they do in B<awk>: if ($str == 0 && $str ne "0") { warn "That doesn't look like a number"; }That method may be best because otherwise you won't treat IEEEnotations like C<NaN> or C<Infinity> properly. At other times, youmight prefer to determine whether string data can be used numericallyby calling the POSIX::strtod() function or by inspecting your stringwith a regular expression (as documented in L<perlre>). warn "has nondigits" if /\D/; warn "not a natural number" unless /^\d+$/; # rejects -3 warn "not an integer" unless /^-?\d+$/; # rejects +3 warn "not an integer" unless /^[+-]?\d+$/; warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2 warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/; warn "not a C float" unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;The length of an array is a scalar value. You may find the lengthof array @days by evaluating C<$#days>, as in B<csh>. 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 C<$#days> 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.)X<$#> X<array, length>You 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: @whatever = (); $#whatever = -1;If 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:X<array, length> scalar(@whatever) == $#whatever - $[ + 1;Version 5 of Perl changed the semantics of C<$[>: files that don't setthe value of C<$[> no longer need to worry about whether anotherfile changed its value. (In other words, use of C<$[> is deprecated.)So in general you can assume thatX<$[> scalar(@whatever) == $#whatever + 1;Some programmers choose to use an explicit conversion so as to leave nothing to doubt: $element_count = scalar(@whatever);If 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%HASH in scalar context reveals C<"1/16">, 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.X<hash, scalar context> X<hash, bucket> X<bucket>You can preallocate space for a hash by assigning to the keys() function.This rounds up the allocated buckets to the next power of two: keys(%users) = 1000; # allocate 1024 buckets=head2 Scalar value constructorsX<scalar, literal> X<scalar, constant>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -