diff options
author | Michael G. Schwern <schwern@pobox.com> | 2002-08-06 06:05:10 -0700 |
---|---|---|
committer | hv <hv@crypt.org> | 2002-08-17 00:51:19 +0000 |
commit | 6d822dc4045278fb03135b2683bac92dba061369 (patch) | |
tree | 39e9aa0ce54a7caf711f12d43985793b79fb762d /pod/perlref.pod | |
parent | 485894a1e3cb3873ba7373c744a8b6231190fbf8 (diff) | |
download | perl-6d822dc4045278fb03135b2683bac92dba061369.tar.gz |
Remove pseudo-hashes (complete)
Message-id: <20020806200510.GC31473@ool-18b93024.dyn.optonline.net>
p4raw-id: //depot/perl@17725
Diffstat (limited to 'pod/perlref.pod')
-rw-r--r-- | pod/perlref.pod | 73 |
1 files changed, 2 insertions, 71 deletions
diff --git a/pod/perlref.pod b/pod/perlref.pod index 7255162fcd..7f9b638fad 100644 --- a/pod/perlref.pod +++ b/pod/perlref.pod @@ -537,77 +537,8 @@ string is effectively quoted. =head2 Pseudo-hashes: Using an array as a hash -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 -were fields in a structure. - -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}; # 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 - - 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. - - use fields; - $pseudohash = fields::phash(foo => "FOO", bar => "BAR"); - -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 ever been set. It acts this way to match the behavior -of a regular hash. For instance: - - use fields; - $phash = fields::phash([qw(foo bar pants)], ['FOO']); - $phash->{pants} = undef; - - 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. - - 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 +Pseudo-hashes have been removed from Perl. The 'fields' pragma +remains available. =head2 Function Templates |