X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperldata.pod;h=a72616ac337695e5270ac51c67ba2756bc0e9b04;hb=84287afe68eecc4342d7e27aac5d9df9d2412490;hp=9b3798ffb1c3e70a917ae650e94df64f1d1bedaf;hpb=cb1a09d0194fed9b905df7b04a4bc031d354609d;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perldata.pod b/pod/perldata.pod index 9b3798f..a72616a 100644 --- a/pod/perldata.pod +++ b/pod/perldata.pod @@ -11,6 +11,28 @@ associative arrays of scalars, known as "hashes". Normal arrays are indexed by number, starting with 0. (Negative subscripts count from the end.) Hash arrays are indexed by string. +Values are usually referred to by name (or through a named reference). +The first character of the name tells you to what sort of data +structure it refers. The rest of the name tells you the particular +value to which it refers. Most often, it consists of a single +I, that is, a string beginning with a letter or underscore, +and containing letters, underscores, and digits. In some cases, it +may be a chain of identifiers, separated by C<::> (or by C<'>, but +that's deprecated); all but the last are interpreted as names of +packages, in order to locate the namespace in which to look +up the final identifier (see L for details). +It's possible to substutite for a simple identifier an expression +which produces a reference to the value at runtime; this is +described in more detail below, and in L. + +There are also special variables whose names don't follow these +rules, so that they don't accidentally collide with one of your +normal variables. Strings which match parenthesized parts of a +regular expression are saved under names containing only digits after +the C<$> (see L and L). In addition, several special +variables which provide windows into the inner working of Perl have names +containing punctuation characters (see L). + Scalar values are always named with '$', even when referring to a scalar that is part of an array. It works like the English word "the". Thus we have: @@ -121,8 +143,8 @@ Scalars aren't necessarily one thing or another. There's no place to declare a scalar variable to be of type "string", or of type "number", or type "filehandle", or anything else. Perl is a contextually polymorphic language whose scalars can be strings, numbers, or references (which -includes objects). While strings and numbers are considered the pretty -much same thing for nearly all purposes, but references are strongly-typed +includes objects). While strings and numbers are considered pretty +much the same thing for nearly all purposes, references are strongly-typed uncastable pointers with built-in reference-counting and destructor invocation. @@ -141,7 +163,7 @@ defined() operator to determine whether the value is defined or not. To find out whether a given string is a valid non-zero number, it's usually enough to test it against both numeric 0 and also lexical "0" (although this will cause B<-w> noises). That's because strings that aren't -numbers count as 0, just as the do in I: +numbers count as 0, just as they do in I: if ($str == 0 && $str ne "0") { warn "That doesn't look like a number"; @@ -166,7 +188,7 @@ there is (ordinarily) a 0th element.) Assigning to C<$#days> changes the length of the array. Shortening an array by this method destroys intervening values. Lengthening an array that was previously shortened I recovers the values that were in those elements. (It used to -in Perl 4, but we had to break this make to make sure destructors were +in Perl 4, but we had to break this to make sure destructors were called when expected.) You can also gain some measure of efficiency by preextending an array that is going to get big. (You can also extend an array by assigning to an element that is off the end of the array.) @@ -189,7 +211,7 @@ So in general you can just assume that scalar(@whatever) == $#whatever + 1; -Some programmer choose to use an explcit conversion so nothing's +Some programmers choose to use an explicit conversion so nothing's left to doubt: $element_count = scalar(@whatever); @@ -230,14 +252,14 @@ your trailing quote, the error will not be reported until Perl finds another line containing the quote character, which may be much further on in the script. Variable substitution inside strings is limited to scalar variables, arrays, and array slices. (In other words, -identifiers beginning with $ or @, followed by an optional bracketed +names beginning with $ or @, followed by an optional bracketed expression as a subscript.) The following code segment prints out "The price is $100." $Price = '$100'; # not interpreted print "The price is $Price.\n"; # interpreted -As in some shells, you can put curly brackets around the identifier to +As in some shells, you can put curly brackets around the name to delimit it from following alphanumerics. In fact, an identifier within such curlies is forced to be a string, as is any single identifier within a hash subscript. Our earlier example, @@ -254,7 +276,7 @@ in the subscript will be interpreted as an expression. Note that a single-quoted string must be separated from a preceding word by a space, since single quote is a valid (though deprecated) character in -an identifier (see L). +a variable name (see L). Two special literals are __LINE__ and __FILE__, which represent the current line number and filename at that point in your program. They @@ -311,11 +333,10 @@ 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 CE> and the identifier. (If you put a space it will be treated as a null identifier, which is valid, and matches the -first blank line--see the Merry Christmas example below.) The terminating -string must appear by itself (unquoted and with no surrounding -whitespace) on the terminating line. +first blank line.) The terminating string must appear by itself +(unquoted and with no surrounding whitespace) on the terminating line. - print <> operator between key/value pairs. The C<=E> operator is mostly just a more visually distinctive -synonym for a comma, but it also quotes its left-hand operand, which makes -it nice for initializing hashes: +synonym for a comma, but it also arranges for its left-hand operand to be +interpreted as a string, if it's a bareword which would be a legal identifier. +This makes it nice for initializing hashes: %map = ( red => 0x00f,