diff options
author | Gurusamy Sarathy <gsar@cpan.org> | 1999-10-02 03:36:41 +0000 |
---|---|---|
committer | Gurusamy Sarathy <gsar@cpan.org> | 1999-10-02 03:36:41 +0000 |
commit | 4bd46447950ba8dfb481e9515d0a88ed358d2013 (patch) | |
tree | 09130fba80b96da6223b8726c171f711e6b550c2 /av.c | |
parent | a98df9627d19bd75c4b1132fc26e037aab5a7879 (diff) | |
download | perl-4bd46447950ba8dfb481e9515d0a88ed358d2013.tar.gz |
make exists() work better on pseudo-hashes (reworked a patch suggested
by Michael G Schwern <schwern@pobox.com>)
p4raw-id: //depot/perl@4279
Diffstat (limited to 'av.c')
-rw-r--r-- | av.c | 25 |
1 files changed, 24 insertions, 1 deletions
@@ -637,11 +637,34 @@ Perl_avhv_fetch_ent(pTHX_ AV *av, SV *keysv, I32 lval, U32 hash) return av_fetch(av, avhv_index_sv(HeVAL(he)), lval); } +/* Check for the existence of an element named by a given key. + * + * This relies on the fact that uninitialized array elements + * are set to &PL_sv_undef. + */ bool Perl_avhv_exists_ent(pTHX_ AV *av, SV *keysv, U32 hash) { HV *keys = avhv_keys(av); - return hv_exists_ent(keys, keysv, hash); + HE *he; + IV ix; + + he = hv_fetch_ent(keys, keysv, FALSE, hash); + if (!he || !SvOK(HeVAL(he))) + return FALSE; + + ix = SvIV(HeVAL(he)); + + /* If the array hasn't been extended to reach the key yet then + * it hasn't been accessed and thus does not exist. We use + * AvFILL() rather than AvFILLp() to handle tied av. */ + if (ix > 0 && ix <= AvFILL(av) + && (SvRMAGICAL(av) + || (AvARRAY(av)[ix] && AvARRAY(av)[ix] != &PL_sv_undef))) + { + return TRUE; + } + return FALSE; } HE * |