perlquestion
mugwumpjism
<p>Can anyone tell me why this XS routine returns my RETVAL with one extra depth of array refs?</p>
<code>
#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
</code>
<readmore>
<p>That is, I'm getting a structure like this:</p>
<code>
$VAR1 = [
[
[
'.',
67586
],
[
'..',
255604
],
...
</code>
<p>Instead of something like this:</p>
<code>
$VAR1 = [
[
'.',
67586
],
[
'..',
255604
],
...
</code>
<p>Any ideas why, anyone?</p>
<p>Also, does anyone know of a better way of saying "<code>or die "Can't opendir $file; $!"</code>" (or should I bother? perhaps just return undef and let the caller check <code>$!</code>)</p>