X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlref.pod;h=f738399c9a97391ece56cd3379c56d8a566855b7;hb=ce32ed4b46e58900e04971fb3189c48cd1fadbd1;hp=5958a7233c8a2b637da7dc32df2c27408afca1d3;hpb=7c2ea1c74a297a6e4f5e99f73e5cadca30825f36;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlref.pod b/pod/perlref.pod index 5958a72..f738399 100644 --- a/pod/perlref.pod +++ b/pod/perlref.pod @@ -558,11 +558,40 @@ 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. +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: + + $phash = [{foo =>1, bar => 2, pants => 3}, '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 + =head2 Function Templates As explained above, a closure is an anonymous function with access to the