This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlmodinstall tweaks from Philip Newton.
[perl5.git] / pod / perlref.pod
index 08a2b42..7255162 100644 (file)
@@ -31,7 +31,7 @@ have been officially "blessed" into a class package.)
 
 Symbolic references are names of variables or other objects, just as a
 symbolic link in a Unix filesystem contains merely the name of a file.
-The C<*glob> notation is something of a of symbolic reference.  (Symbolic
+The C<*glob> notation is something of a symbolic reference.  (Symbolic
 references are sometimes called "soft references", but please don't call
 them that; references are confusing enough without useless synonyms.)
 
@@ -84,7 +84,7 @@ brackets:
 Here we've created a reference to an anonymous array of three elements
 whose final element is itself a reference to another anonymous array of three
 elements.  (The multidimensional syntax described later can be used to
-access this.  For example, after the above, C<$arrayref-E<gt>[2][1]> would have
+access this.  For example, after the above, C<< $arrayref->[2][1] >> would have
 the value "b".)
 
 Taking a reference to an enumerated list is not the same
@@ -243,7 +243,9 @@ All of these are self-explanatory except for C<*foo{IO}>.  It returns
 the IO handle, used for file handles (L<perlfunc/open>), sockets
 (L<perlfunc/socket> and L<perlfunc/socketpair>), and directory
 handles (L<perlfunc/opendir>).  For compatibility with previous
-versions of Perl, C<*foo{FILEHANDLE}> is a synonym for C<*foo{IO}>.
+versions of Perl, C<*foo{FILEHANDLE}> is a synonym for C<*foo{IO}>, though it
+is deprecated as of 5.8.0.  If deprecation warnings are in effect, it will warn
+of its use.
 
 C<*foo{THING}> returns undef if that particular THING hasn't been used yet,
 except in the case of scalars.  C<*foo{SCALAR}> returns a reference to an
@@ -357,7 +359,7 @@ syntactic sugar, the examples for method 2 may be written:
 
 The left side of the arrow can be any expression returning a reference,
 including a previous dereference.  Note that C<$array[$x]> is I<not> the
-same thing as C<$array-E<gt>[$x]> here:
+same thing as C<< $array->[$x] >> here:
 
     $array[$x]->{"foo"}->[0] = "January";
 
@@ -365,7 +367,7 @@ This is one of the cases we mentioned earlier in which references could
 spring into existence when in an lvalue context.  Before this
 statement, C<$array[$x]> may have been undefined.  If so, it's
 automatically defined with a hash reference so that we can look up
-C<{"foo"}> in it.  Likewise C<$array[$x]-E<gt>{"foo"}> will automatically get
+C<{"foo"}> in it.  Likewise C<< $array[$x]->{"foo"} >> will automatically get
 defined with an array reference so that we can look up C<[0]> in it.
 This process is called I<autovivification>.
 
@@ -528,7 +530,8 @@ makes it more than a bareword:
     $array{ +shift }
     $array{ shift @_ }
 
-The B<-w> switch will warn you if it interprets a reserved word as a string.
+The C<use warnings> pragma or the B<-w> switch will warn you if it
+interprets a reserved word as a string.
 But it will no longer warn you about using lowercase words, because the
 string is effectively quoted.
 
@@ -537,6 +540,13 @@ string is effectively quoted.
 B<WARNING>:  This section describes an experimental feature.  Details may
 change without notice in future versions.
 
+B<NOTE>: The current user-visible implementation of pseudo-hashes
+(the weird use of the first array element) is deprecated starting from
+Perl 5.8.0 and will be removed in Perl 5.10.0, and the feature will be
+implemented differently.  Not only is the current interface rather ugly,
+but the current implementation slows down normal array and hash use quite
+noticeably.  The 'fields' pragma interface will remain available.
+
 Beginning with release 5.005 of Perl, you may use an array reference
 in some contexts that would normally require a hash reference.  This
 allows you to access array elements using symbolic names, as if they
@@ -546,41 +556,58 @@ For this to work, the array must contain extra information.  The first
 element of the array has to be a hash reference that maps field names
 to array indices.  Here is an example:
 
-   $struct = [{foo => 1, bar => 2}, "FOO", "BAR"];
+    $struct = [{foo => 1, bar => 2}, "FOO", "BAR"];
 
-   $struct->{foo};  # same as $struct->[1], i.e. "FOO"
-   $struct->{bar};  # same as $struct->[2], i.e. "BAR"
+    $struct->{foo};  # same as $struct->[1], i.e. "FOO"
+    $struct->{bar};  # same as $struct->[2], i.e. "BAR"
 
-   keys %$struct;   # will return ("foo", "bar") in some order
-   values %$struct; # will return ("FOO", "BAR") in same some order
+    keys %$struct;   # will return ("foo", "bar") in some order
+    values %$struct; # will return ("FOO", "BAR") in same some order
 
-   while (my($k,$v) = each %$struct) {
+    while (my($k,$v) = each %$struct) {
        print "$k => $v\n";
-   }
+    }
+
+Perl will raise an exception if you try to access nonexistent fields.
+To avoid inconsistencies, always use the fields::phash() function
+provided by the C<fields> pragma.
 
-Perl will raise an exception if you try to delete keys from a pseudo-hash
-or try to access nonexistent fields.  For better performance, Perl can also
-do the translation from field names to array indices at compile time for
-typed object references.  See L<fields>.
+    use fields;
+    $pseudohash = fields::phash(foo => "FOO", bar => "BAR");
 
-There are two ways to check for the existance of a key in a
+For better performance, Perl can also do the translation from field
+names to array indices at compile time for typed object references.
+See L<fields>.
+
+There are two ways to check for the existence of a key in a
 pseudo-hash.  The first is to use exists().  This checks to see if the
-given field has been used yet.  It acts this way to match the behavior
+given field has ever been set.  It acts this way to match the behavior
 of a regular hash.  For instance:
 
-       $phash = [{foo =>, bar => 2, pants => 3}, 'FOO'];
-       $phash->{pants} = undef;
+    use fields;
+    $phash = fields::phash([qw(foo bar pants)], ['FOO']);
+    $phash->{pants} = undef;
 
-       exists $phash->{foo};    # true, 'foo' was set in the declaration
-       exists $phash->{bar};    # false, 'bar' has not been used.
-       exists $phash->{pants};  # true, your 'pants' have been touched
+    print exists $phash->{foo};    # true, 'foo' was set in the declaration
+    print exists $phash->{bar};    # false, 'bar' has not been used.
+    print exists $phash->{pants};  # true, your 'pants' have been touched
 
 The second is to use exists() on the hash reference sitting in the
 first array element.  This checks to see if the given key is a valid
 field in the pseudo-hash.
 
-       exists $phash->[0]{pants};      # true, 'pants' is a valid field
-       exists $phash->[0]{shoes};      # false, 'shoes' can't be used
+    print exists $phash->[0]{bar};     # true, 'bar' is a valid field
+    print exists $phash->[0]{shoes};# false, 'shoes' can't be used
+
+delete() on a pseudo-hash element only deletes the value corresponding
+to the key, not the key itself.  To delete the key, you'll have to
+explicitly delete it from the first hash element.
+
+    print delete $phash->{foo};     # prints $phash->[1], "FOO"
+    print exists $phash->{foo};     # false
+    print exists $phash->[0]{foo};  # true, key still exists
+    print delete $phash->[0]{foo};  # now key is gone
+    print $phash->{foo};            # runtime exception
 
 =head2 Function Templates