I've got some XS code that needs to return a hash reference. The hash keys are integers, and the values will all be "undef". The relevant chunk of code looks something like this:
RETVAL = (HV*) sv_2mortal( (SV*) newHV ); /* call some code in an external library */ num_results = find_results( data, &results ); if( num_results ) { SV* value; for( x = 0; x < num_results; x++ ) { value = &PL_sv_undef; fprintf(stderr, "XS: returning %u => undef\n", results[x]); } hv_store_ent(RETVAL, sv_2mortal(newSViv(results[x])), value, 0); } } OUTPUT: RETVAL ... but Data::Dumper is confused. Here's the output of the fprintf()s above, and what Data::Dumper thinks of the returned hashref, when results = [3,4,5]: XS: returning 3 => undef XS: returning 4 => undef XS: returning 5 => undef $VAR1 = { '3' => undef, '4' => ${\$VAR1->{'3'}}, '5' => ${\$VAR1->{'3'}} }; Here's what Devel::Peek::Dump thinks of the same hashref: SV = RV(0x8108154) at 0x82a6ce4 REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x82aa218 SV = PVHV(0x82923e0) at 0x82aa218 REFCNT = 1 FLAGS = (SHAREKEYS) IV = 3 NV = 0 ARRAY = 0x82aab38 (0:5, 1:3) hash quality = 175.0% KEYS = 3 FILL = 3 MAX = 7 RITER = -1 EITER = 0x0 Elt "3" HASH = 0x34 SV = NULL(0x0) at 0x80f4d18 REFCNT = 2147482874 FLAGS = (READONLY) Elt "4" HASH = 0x35 SV = NULL(0x0) at 0x80f4d18 REFCNT = 2147482874 FLAGS = (READONLY) Elt "5" HASH = 0x36 SV = NULL(0x0) at 0x80f4d18 REFCNT = 2147482874 FLAGS = (READONLY) Any idea what's up with the "'4' => ${\$VAR1->{'3'}}," stuff? -- Will