bh has asked for the wisdom of the Perl Monks concerning the following question:

I am unable to retrieve the value of a key in a hash h1, if that value is another hash reference h2, and then recognize keys in h2. What am I doing wrong and how can I fix it? I have a structure stored in Perl as a multi-dimensional hash like the following:
my $h1; my $h2; $h2->{'bar'} = 1; $h1->{'foo'} = $h2;
I can test the existence of 'foo' in $h1 with XS as follows:
void mhash1(key1, h1) char* key1 HV* h1 CODE: I32 klen1 = strlen(key1); if ( hv_exists(h1, key1, klen1) ) { }
I cannot test for 'bar' because when I get the value of 'foo' (which is another hash reference) it appears not to be the right kind of structure in the following code:
void mhash2(key1, h1, key2) char* key1 char* key2 HV* h1 CODE: I32 klen1 = strlen(key1); I32 klen2 = strlen(key2); if ( hv_exists(h1, key1, klen1) ) { SV** h2r = hv_fetch(h1, key1, klen1, 0); HV* h2 = (HV*)*h2r; if ( hv_exists(h2, key2, klen2) ) { printf("%s\n", "found it!"); } }
The following Perl code results in a segmentation fault:
#!/usr/bin/perl use strict; use warnings; use MyTest; my $h1; my $h2; $h2->{'bar'} = 1; $h1->{'foo'} = $h2; MyTest::mhash2('foo', $h1, 'bar');
Thank you!

Replies are listed 'Best First'.
Re: Accessing a multi-dimensional hash within an XS routine
by Anonymous Monk on Apr 03, 2016 at 19:08 UTC
    because
    HV* h2 = (HV*)*h2r;
    *h2r is not a hash (HV *), its a reference. You need to use SvRV to dereference it, for example:
    HV *h2 = (HV *) SvRV(*h2r);
    Also, it's a good idea to check whether key1 was found (h2r != NULL) and if its a reference (SvROK(*h2r)) to a hash (SvTYPE(SvRV(*h2r)) == SVtPVHV).
      Perfect! Thanks.
      Tip: MUTABLE_HV(...) is safer than (HV*)...