diff options
Diffstat (limited to 'Master/tlpkg/tlperl/lib/pods/perldata.pod')
-rw-r--r-- | Master/tlpkg/tlperl/lib/pods/perldata.pod | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/Master/tlpkg/tlperl/lib/pods/perldata.pod b/Master/tlpkg/tlperl/lib/pods/perldata.pod index bc564e650d4..1b1cbf4564b 100644 --- a/Master/tlpkg/tlperl/lib/pods/perldata.pod +++ b/Master/tlpkg/tlperl/lib/pods/perldata.pod @@ -52,7 +52,7 @@ X<scalar> $#days # the last index of array @days Entire arrays (and slices of arrays and hashes) are denoted by '@', -which works much like the word "these" or "those" does in English, +which works much as the word "these" or "those" does in English, in that it indicates multiple values are expected. X<array> @@ -140,7 +140,7 @@ to determine the context for the right argument. Assignment to a scalar evaluates the right-hand side in scalar context, while assignment to an array or hash evaluates the righthand side in list context. Assignment to a list (or slice, which is just a list -anyway) also evaluates the righthand side in list context. +anyway) also evaluates the right-hand side in list context. When you use the C<use warnings> pragma or Perl's B<-w> command-line option, you may see warnings @@ -276,8 +276,8 @@ set. For example, you stick 10,000 things in a hash, but evaluating %HASH in scalar context reveals C<"1/16">, which means only one out of sixteen buckets has been touched, and presumably contains all 10,000 of your items. This isn't supposed to happen. If a tied hash -is evaluated in scalar context, a fatal error will result, since this -bucket usage information is currently not available for tied hashes. +is evaluated in scalar context, the C<SCALAR> method is called (with a +fallback to C<FIRSTKEY>). X<hash, scalar context> X<hash, bucket> X<bucket> You can preallocate space for a hash by assigning to the keys() function. @@ -391,7 +391,7 @@ inet_aton()/inet_ntoa() routines of the Socket package. Note that since Perl 5.8.1 the single-number v-strings (like C<v65>) are not v-strings before the C<< => >> operator (which is usually used -to separate a hash key from a hash value), instead they are interpreted +to separate a hash key from a hash value); instead they are interpreted as literal strings ('v65'). They were v-strings from Perl 5.6.0 to Perl 5.8.0, but that caused more confusion and breakage than good. Multi-number v-strings like C<v65.66> and C<65.66.67> continue to @@ -406,7 +406,8 @@ represent the current filename, line number, and package name at that point in your program. They may be used only as separate tokens; they will not be interpolated into strings. If there is no current package (due to an empty C<package;> directive), __PACKAGE__ is the undefined -value. +value. (But the empty C<package;> is no longer supported, as of version +5.10.) X<__FILE__> X<__LINE__> X<__PACKAGE__> X<line> X<file> X<package> The two control characters ^D and ^Z, and the tokens __END__ and __DATA__ @@ -451,7 +452,7 @@ produces a compile-time error instead. The restriction lasts to the end of the enclosing block. An inner block may countermand this by saying C<no strict 'subs'>. -=head3 Array Joining Delimiter +=head3 Array Interpolation X<array, interpolation> X<interpolation, array> X<$"> Arrays and slices are interpolated into double-quoted strings @@ -667,7 +668,8 @@ of how to arrange for an output ordering. =head2 Subscripts -An array is subscripted by specifying a dollar sign (C<$>), then the +An array can be accessed one scalar at a +time by specifying a dollar sign (C<$>), then the name of the array (without the leading C<@>), then the subscript inside square brackets. For example: @@ -691,15 +693,12 @@ are used. For example: print "Darwin's First Name is ", $scientists{"Darwin"}, "\n"; -=head2 Slices -X<slice> X<array, slice> X<hash, slice> +You can also subscript a list to get a single element from it: -A common way to access an array or a hash is one scalar element at a -time. You can also subscript a list to get a single element from it. + $dir = (getpwnam("daemon"))[7]; - $whoami = $ENV{"USER"}; # one element from the hash - $parent = $ISA[0]; # one element from the array - $dir = (getpwnam("daemon"))[7]; # likewise, but with list +=head2 Slices +X<slice> X<array, slice> X<hash, slice> A slice accesses several elements of a list, an array, or a hash simultaneously using a list of subscripts. It's more convenient @@ -761,6 +760,13 @@ is the number of elements on the right-hand side of the assignment. The null list contains no elements, so when the password file is exhausted, the result is 0, not 2. +Slices in scalar context return the last item of the slice. + + @a = qw/first second third/; + %h = (first => 'A', second => 'B'); + $t = @a[0, 1]; # $t is now 'second' + $u = @h{'first', 'second'}; # $u is now 'B' + If you're confused about why you use an '@' there on a hash slice instead of a '%', think of it like this. The type of bracket (square or curly) governs whether it's an array or a hash being looked at. @@ -856,8 +862,8 @@ C<use strict 'refs'> forbids such practice. Another way to create anonymous filehandles is with the Symbol module or with the IO::Handle module and its ilk. These modules have the advantage of not hiding different types of the same name -during the local(). See the bottom of L<perlfunc/open()> for an -example. +during the local(). See the bottom of L<perlfunc/"open FILEHANDLE"> +for an example. =head1 SEE ALSO |