Jeff Walker wrote:
>
> I am trying to build a hash from an .xs file, then return a ref to that hash
> to the perl code. I figured out how pass the hash back using a reference,
> but when I get the hash back, all the keys exist, but all the values are
> undefined. Here is some stripped down code that illustrates this:
>
> SV *
> Blah()
> PREINIT:
8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<
>
> The perl program (which of course uses the module) get the ref returned by
> Blah. I then print the keys of this hash and everything is okay. Then I
> try to see the values (foreach $thing ( keys( %$hash ) ) ) but they are all
> not defined (via ! defined($hash{$thing} ))
Jeff,
Here is script using Inline.pm that does what you want.
--------------------------8<----------------------------
use Inline C => DATA;
$hash_ref = my_new_hash(100);
foreach $key (keys %$hash_ref) {
print "$key => $hash_ref->{$key}\n";
}
__END__
__C__
SV* my_new_hash(int size) {
int i;
HV* hash = newHV();
for (i = 0; i < size; i++) {
hv_store_ent(hash, newSVpvf("string #%d", i),
newSViv(i), 0);
}
return newRV_noinc((SV*) hash);
}
--------------------------8<----------------------------
You'll need Inline.pm version 0.30. Just run the script. No compiling
needed.
If you have any Inline related questions/problems, please post them to
[EMAIL PROTECTED]
Brian
--
perl -le 'use Inline C=>q{SV*JAxH(char*x){return newSVpvf
("Just Another %s Hacker",x);}};print JAxH+Perl'