summaryrefslogtreecommitdiff
path: root/mg.c
diff options
context:
space:
mode:
authorTassilo von Parseval <tassilo.parseval@post.rwth-aachen.de>2003-12-06 12:50:59 +0100
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>2003-12-06 19:50:07 +0000
commita3bcc51ebd4e201d85a37d8410b7a375b8d94244 (patch)
tree57797ca6128a5011622a6e805fb66f6d2a0fbaa5 /mg.c
parent5c98da1c029548d157089bc95672bf854902dd76 (diff)
downloadperl-a3bcc51ebd4e201d85a37d8410b7a375b8d94244.tar.gz
SCALAR/FIRSTKEY for tied hashes in scalar context
Message-id: <20031206105059.GA13989@ethan> p4raw-id: //depot/perl@21855
Diffstat (limited to 'mg.c')
-rw-r--r--mg.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/mg.c b/mg.c
index 3e864da30b..2529ff7793 100644
--- a/mg.c
+++ b/mg.c
@@ -1538,6 +1538,12 @@ Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg)
call_method("CLEAR", G_SCALAR|G_DISCARD);
POPSTACK;
LEAVE;
+
+ if (SvTYPE(sv) == SVt_PVHV)
+ /* must reset iterator otherwise Perl_magic_scalarpack
+ * wont report a false value on a cleared hash */
+ HvEITER((HV*)sv) = NULL;
+
return 0;
}
@@ -1572,6 +1578,41 @@ Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg)
return magic_methpack(sv,mg,"EXISTS");
}
+SV *
+Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg)
+{
+ dSP;
+ SV *retval = &PL_sv_undef;
+ SV *tied = SvTIED_obj((SV*)hv, mg);
+ HV *pkg = SvSTASH((SV*)SvRV(tied));
+
+ if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) {
+ SV *key;
+ if (HvEITER(hv))
+ /* we are in an iteration so the hash cannot be empty */
+ return &PL_sv_yes;
+ /* no xhv_eiter so now use FIRSTKEY */
+ key = sv_newmortal();
+ magic_nextpack((SV*)hv, mg, key);
+ HvEITER(hv) = NULL; /* need to reset iterator */
+ return SvOK(key) ? &PL_sv_yes : &PL_sv_no;
+ }
+
+ /* there is a SCALAR method that we can call */
+ ENTER;
+ PUSHSTACKi(PERLSI_MAGIC);
+ PUSHMARK(SP);
+ EXTEND(SP, 1);
+ PUSHs(tied);
+ PUTBACK;
+
+ if (call_method("SCALAR", G_SCALAR))
+ retval = *PL_stack_sp--;
+ POPSTACK;
+ LEAVE;
+ return retval;
+}
+
int
Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg)
{