分类:
2009-01-06 20:23:17
Variable names
1. Perl has three built-in data types: scalars, arrays of scalars, and associative arrays of scalars, known as "hashes". Every variable type has its own namespace, as do several non-variable identifiers.
2. Case is significant.
3. Names that start with a digit may contain only more digits.
Scalar values
4. A scalar value is interpreted as TRUE in the Boolean sense if it is not the null string or the number 0 (or its string equivalent, "0").
5. There are actually two varieties of null strings, a defined one and an undefined one. The defined version is just a string of length zero, such as "" . The undefined version is the value that indicates that there is no real value for something(known as "undef").
my $emp = ""; #it's a defined value
my $emp; #it's a undefined value
6. $# represents the subscript of the last element. Change value of $#array is a efficient to make array bigger, smaller or empty($#array=-1).
7. Evaluating an array in scalar context, it returns the length of the array.
8. Evaluating a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns a value like '2/100', which means there are 2 used buckets and 100 allocated buckets.
9. You are allowed to use underscores (underbars) in numeric literals between digits for legibility.
Exp:0b1010_0110
10. Hexadecimal, octal, or binary, representations in string literals (e.g. '0xff') are not automatically converted to their integer representation. The hex() and oct() functions make these conversions for you.
11. you can enclose the variable name in braces to disambiguate it from following alphanumerics (and underscores).
|
12. The special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, line number, and package name at that point in your program.
List values
13. In a context not requiring a list value, the value of what appears to be a list literal is simply the value of the final element.
14. The null list is represented by (). Interpolating it in a list has no effect. Thus ((),(),()) is equivalent to ().
15. A list value may also be subscripted like a normal array. You must put the list in parentheses to avoid ambiguity.
# Stat returns list value.
$time = (stat($file))[8];# SYNTAX ERROR HERE.
$time = stat($file)[8]; # OOPS, FORGOT PARENTHESES
|
17. List assignment in scalar context returns the number of elements produced by the expression on the right side of the assignment:
18. It's also the source of a useful idiom for executing a function or performing an operation in list context and then counting the number of return values, by assigning to an empty list and then using that assignment in scalar context. For example, this code:
|
Typeglobs and Filehandles
19. This used to be the preferred way to pass arrays and hashes by reference into a function, but now that we have real references, this is seldom needed.
|
20. Strange though this may seem, this is the basis for the whole module import/export system.
*Here::blue = \$There::green;
21. All functions that are capable of creating filehandles automatically create an anonymous filehandle if the handle passed to them is an uninitialized scalar variable., and will be closed automatically when the scope ends, provided there are no other references to them.
22. Note that if an initialized scalar variable is used instead the result is different: $fh='zzz'; ($fh, ...) is equivalent to ( *{'zzz'}, ...) . strict 'refs' forbids such practice.