develooper Front page | perl.perl5.porters | Postings from March 2001

Re: [ID 19990808.001] [PATCH] FETCH triggered on exists()

Thread Previous | Thread Next
March 9, 2001 00:30
Re: [ID 19990808.001] [PATCH] FETCH triggered on exists()
Message ID:

What you're saying makes sense.  

So, I'll start following it down the other path:

A little bit of GDB walking and here's what I see:

FETCH is being called inside the
hv.c:934	    return SvTRUE(sv);

SvTRUE is a scary macro which calls sv_2bool
sv_2bool on a magical variable calls mg_get()

and mg_get calls the get entry in the vtable for the magic, which for
a hash is  Perl_magic_getpack, which does the FETCH method
invocation.  *kaboom*

I think the problem is that the sv passed to
     magic_existspack(sv, mg_find(sv, 'p'));
is Magical, because it'a copy of the magical HV.

I think the patch below may (actually) solve this problem (instead of
documenting it firmly in place).  It creates a new SV to hold the
return value from magic_existspack, instead of using the magical

All tests in the test suite pass for me, (except for op/qu.t, which is 
very unrelated.)

--- hv.c.orig   Fri Mar  9 03:16:20 2001
+++ hv.c        Fri Mar  9 03:20:34 2001
@@ -927,11 +927,12 @@
     if (SvRMAGICAL(hv)) {
        if (mg_find((SV*)hv,'P')) {
+           SV* svret = sv_newmortal();
            sv = sv_newmortal();
            keysv = sv_2mortal(newSVsv(keysv));
            mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
-           magic_existspack(sv, mg_find(sv, 'p'));
-           return SvTRUE(sv);
+           magic_existspack(svret, mg_find(sv, 'p'));
+           return SvTRUE(svret);
        else if (mg_find((SV*)hv,'E')) {


>>>>> "MD" == Mark-Jason Dominus <> writes:

>> The patch below solves this issue.

MD> I respectfully disagree.  I think this is a bug, not a feature.

MD> At least, it is not normal for Perl to treat 'undef' in this
MD> special way; every other place where Perl expects a boolean value,
MD> undefined is taken to mean false.  And I cannot remember that last
MD> time this came up anybody advanced any reason why the existing
MD> behavior was desirable.  If the author of the EXISTS method wanted
MD> to call FETCH and return that value, they would be free to do so.

MD> I think the correct approach is to fix it, or at least to leave it
MD> as an undocumented oddity that might be fixed in the future.
MD> Documenting it requires that it remain forever.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About