From: Michael G Schwern Date: Sun, 3 Oct 1999 02:34:01 +0000 (-0400) Subject: Re: Should keys in pseudo-hashes -always- exist? [DOC PATCH] X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e0478e5a74c23ab803d1f4136ac0e84a68ba9ecf;p=p5sagit%2Fp5-mst-13.2.git Re: Should keys in pseudo-hashes -always- exist? [DOC PATCH] To: perl5-porters@perl.org Message-ID: <19991003023401.A1520@blackrider> p4raw-id: //depot/cfgperl@4293 --- diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index a09c6e5..d07ded3 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -1412,6 +1412,9 @@ This surprising autovivification in what does not at first--or even second--glance appear to be an lvalue context may be fixed in a future release. +See L for specifics on how exists() acts when +used on a pseudo-hash. + =item exit EXPR Evaluates EXPR and exits immediately with that value. Example: diff --git a/pod/perlref.pod b/pod/perlref.pod index 5958a72..08a2b42 100644 --- a/pod/perlref.pod +++ b/pod/perlref.pod @@ -563,6 +563,25 @@ 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. +There are two ways to check for the existance 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 +of a regular hash. For instance: + + $phash = [{foo =>, 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 + +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 + =head2 Function Templates As explained above, a closure is an anonymous function with access to the