[mplspm]: Deference a hash reference _twice_ in *.xs??

Mark Conty Mark_Conty at cargill.com
Mon Mar 11 15:03:40 CST 2002


I'm trying to get my first *.xs module off the ground, but I've encountered
some odd behavior.  (Running HP-UX 10.20 and 11.00, Perl 5.005_02 and 5.6.0.)

First off, here's the [very simple] calling Perl code:

	#!/usr/bin/perl -w

	use showHash;

	$buffer = {
		app => "mdc",
		obj => "",
		txt => "Testing"
	};

	showHash($buffer);

	exit(0);

... and here's a simplified fragment of the C code:

    1   SV*
    2   showHash(hashref)
    3   	SV	*hashref
    4       CODE:
    5   	HV	*hash;
    6   	I32	count;
    7   {
    8   	if ( ! SvROK(hashref) ||
    9   		SvTYPE((hash=(HV*)SvRV(SvRV(hashref)))) != SVt_PVHV )
   10   	    croak("Parameter is not a reference to a hash -- type is %d.\n",
   11   		SvTYPE(hash));
   12   
   13   	count = hv_iterinit(hash);
   14   
   15   	printf ("Hash has %d key%s\n", count, (count<2)?"":"s");
   16   
   17   	if ( count ) {
   18   		SV *val;
   19   		char **key;
   20   		I32 *klen;
   21   		int i = 0;
   22   
   23   		while ( (val = hv_iternextsv(hash,key,klen) ) ) {
   24   			printf("[%d] hash{%s} = '%s'\n", ++i, *key,
   25   				SvPV(val,PL_na));
   26   		}
   27   	}
   28   	RETVAL = 0;
   29   }
   30       OUTPUT:
   31   	RETVAL

What I'm puzzled by is at line 9.  If I don't doubly-dereference hashref,
the resulting SvTYPE() value is 3, which means it's a reference.  Duhh, of
course it's a reference!  Sheesh...  Tried using only one SvRV() and casting
(HV*) inside, but that didn't make any difference.

I have a feeling that there is a simple but very important sentence buried
somewhere in perlxs/perlxstut that I have overlooked, to my detriment.

Has anyone else run into this before?  Did a Google search for xsub & hash,
but most of the questions & help seems to be in the area of creating or
updating a hash and sending key/value pairs *back* to the Perl script.
What I'm trying to do doesn't seem altogether _that_ complicated, but it's
taken me a few days just to get this to work!  *sigh*

Thanks...
-- 
Mark Conty
APS/NAGO IT Group
Server Team - MS 64
952-984-0503


--------------------------------------------------
Minneapolis Perl Mongers mailing list

To unsubscribe, send mail to majordomo at pm.org
with "unsubscribe mpls" in the body of the message.



More information about the Mpls-pm mailing list