diff options
Diffstat (limited to 'pod/perlref.pod')
-rw-r--r-- | pod/perlref.pod | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/pod/perlref.pod b/pod/perlref.pod index 12bc581a6d..f738399c9a 100644 --- a/pod/perlref.pod +++ b/pod/perlref.pod @@ -558,29 +558,39 @@ to array indices. Here is an example: print "$k => $v\n"; } -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 +Perl will raise an exception if you 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>. -There are two ways to check for the existance of a key in a +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 =>1, bar => 2, pants => 3}, '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]{bar}; # true, 'bar' 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 |