Can anyone tell me why this XS routine returns my RETVAL with one extra depth of array refs?

#include <sys/types.h> #include <dirent.h> #include <stdio.h> SV * readdir_inode(dirname) char* dirname INIT: struct dirent *ent; DIR* dir; SV* record[2]; AV *entry, *ret_val; CODE: ret_val = newAV(); dir = opendir(dirname); if (!dir) perror("Cannot open dir"); while ((ent=readdir(dir))) { record[0] = newSVpv(ent->d_name, 0); record[1] = newSViv((IV)ent->d_ino); entry = av_make(2, record); av_push(ret_val, newRV_inc((SV*) entry)); } closedir(dir); RETVAL=newRV_inc((SV*) ret_val); OUTPUT: RETVAL

That is, I'm getting a structure like this:

$VAR1 = [ [ [ '.', 67586 ], [ '..', 255604 ], ...

Instead of something like this:

$VAR1 = [ [ '.', 67586 ], [ '..', 255604 ], ...

Any ideas why, anyone?

Also, does anyone know of a better way of saying "or die "Can't opendir $file; $!"" (or should I bother? perhaps just return undef and let the caller check $!)


In reply to XS routine returns an extra depth of array refs by mugwumpjism

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.