On Sun, Jun 19, 2005 at 09:58:12AM -0700, Will Lowe wrote:
> 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'}}
>         };

> Any idea what's up with the "'4' => ${\$VAR1->{'3'}}," stuff?

It's trying to create pure perl syntax to say "set the value of key '4'
to be the same scalar as the value of key '3'"


Only this isn't ever going to work because there's no way in pure perl that
you can create such a structure - what it writes doesn't work because
$VAR1 doesn't have a value at the point of assignment, and even if it wrote
the fixup after the initial creation of $VAR1 as:

    $VAR1->{'4'} = ${\$VAR1->{'3'}};

because the hash assignment does a copy, not a binding.

Nicholas Clark

Reply via email to