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 *
package main;
-print "1..12\n";
+print "1..15\n";
$sch = {
'abc' => 1,
print "not " unless $a->[1] eq 'b';
print "ok 12\n";
+# check if exists() is behaving properly
+$avhv = [{foo=>1,bar=>2,pants=>3}];
+print "not " if exists $avhv->{bar};
+print "ok 13\n";
+
+$avhv->{pants} = undef;
+print "not " unless exists $avhv->{pants};
+print "ok 14\n";
+print "not " if exists $avhv->{bar};
+print "ok 15\n";