Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: XS routine returns an extra depth of array refs

by Fastolfe (Vicar)
on Nov 05, 2001 at 22:04 UTC ( #123367=note: print w/replies, xml ) Need Help??

in reply to XS routine returns an extra depth of array refs

Let's see your Perl code where you get the results of this function and do a Dumper on it. I expect to see:
$ref = readdir_inode($dirname); print Dumper($ref); # or @a = readdir_inode($dirname); print Dumper($a[0]); # not Dumper(\@a)
I don't see anything obviously wrong with your code, but I want to eliminate some assumptions first before I go digging. :)

Replies are listed 'Best First'.
Re: Re: XS routine returns an extra depth of array refs
by mugwumpjism (Hermit) on Nov 05, 2001 at 22:13 UTC

    You hit the nail right on the head... I had the second, but Dumper \@a instead of Dumper $a[0], and my test code was assuming I was returning a list. Hate it when that happens ;-)

    OK, so now at least I have it returning something sensible, which is always good. In which case, the question is why does this:

    AV * readdir_inode(dirname) char* dirname INIT: struct dirent *ent; DIR* dir; SV* record[2]; AV *entry, *ret_val; CODE: RETVAL = newAV(); dir = opendir(dirname); if (!dir) perror("Cannot open dir"); while ((ent=readdir(dir))) { printf("%ld %x\n", ent->d_ino, ent->d_name); record[0] = newSVpv(ent->d_name, 0); record[1] = newSViv((IV)ent->d_ino); entry = av_make(2, record); av_push(RETVAL, newRV_inc((SV*) entry)); } closedir(dir); /*RETVAL=*ret_val;*/ /*RETVAL=newRV_inc((SV*) ret_val);*/ OUTPUT: RETVAL

    Return an extra level of arrays?

    The calling code:

    my @ents = readdir_inode("."); print Dumper @ents;

    The output:

    $VAR1 = [ [ '.', 67586 ], [ '..', 255604
      When you use RETVAL, XS builds it in such a way that it's functionally the same as the first item on the return stack. Thus, you've effectively stuck an AV into $ents[0], which I'm guessing Perl DWIM's here and turns it into a reference to an array. Hence the additional level of references.

      The correct way (I believe) to return a list in this situation is to declare your function with a return type of void and simply push each element onto the return stack instead of trying to return an array, using PUSHs I think.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://123367]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2022-01-18 20:31 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (54 votes). Check out past polls.