X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperldata.pod;h=a72616ac337695e5270ac51c67ba2756bc0e9b04;hb=84287afe68eecc4342d7e27aac5d9df9d2412490;hp=648f0922e1264489e5b661812f550e505daa0cde;hpb=4633a7c4bad06b471d9310620b7fe8ddd158cccd;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perldata.pod b/pod/perldata.pod index 648f092..a72616a 100644 --- a/pod/perldata.pod +++ b/pod/perldata.pod @@ -1,6 +1,6 @@ =head1 NAME -perldata - Perl data structures +perldata - Perl data types =head1 DESCRIPTION @@ -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: @@ -60,8 +82,8 @@ of this, see L. Names that start with a digit may only contain more digits. Names which do not start with a letter, underscore, or digit are limited to -one character, e.g. "$%" or "$$". (Most of these one character names -have a predefined significance to Perl. For instance, $$ is the +one character, e.g. C<$%> or C<$$>. (Most of these one character names +have a predefined significance to Perl. For instance, C<$$> is the current process id.) =head2 Context @@ -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. @@ -138,15 +160,27 @@ array. An undefined null scalar may become defined the first time you use it as if it were defined, but prior to that you can use the 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 usally +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"; } +That's usually preferable because otherwise you won't treat IEEE notations +like C or C properly. At other times you might prefer to +use a regular expression to check whether data is numeric. See L +for details on regular expressions. + + warn "has nondigits" if /\D/; + warn "not a whole number" unless /^\d+$/; + warn "not an integer" unless /^[+-]?\d+$/ + warn "not a decimal number" unless /^[+-]?\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 length of array @days by evaluating C<$#days>, as in B. (Actually, it's not the length of the array, it's the subscript of the last element, since @@ -154,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.) @@ -177,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); @@ -218,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, @@ -242,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 @@ -252,7 +286,8 @@ logical end of the script 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 from any required file or evaluated string.) The two control characters ^D and -^Z are synonyms for __END__ (or __DATA__ in a module). +^Z are synonyms for __END__ (or __DATA__ in a module; see L for +details on __DATA__). A word that has no other interpretation in the grammar will be treated as if it were a quoted string. These are known as @@ -298,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, @@ -471,3 +506,39 @@ or for using call-by-named-parameter to complicated functions: linebreak => 'true', labels => \%labels ); + +Note that just because a hash is initialized in that order doesn't +mean that it comes out in that order. See L for examples +of how to arrange for an output ordering. + +=head2 Typeglobs and FileHandles + +Perl uses an internal type called a I to hold an entire +symbol table entry. The type prefix of a typeglob is a C<*>, because +it represents all types. 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. + +One place where you still use typeglobs (or references thereto) +is for passing or storing filehandles. If you want to save away +a filehandle, do it this way: + + $fh = *STDOUT; + +or perhaps as a real reference, like this: + + $fh = \*STDOUT; + +This is also the way to create a local filehandle. For example: + + sub newopen { + my $path = shift; + local *FH; # not my! + open (FH, $path) || return undef; + return \*FH; + } + $fh = newopen('/etc/passwd'); + +See L, L, and L for more +discussion on typeglobs. See L for other ways of +generating filehandles.