Returning 'undef' from your function on error and setting
$! seems the best way of doing this. If you really want to throw an error directly from your XS, use the "croak" function, which behaves like
die in Perl. (There's also a 'warn'.) These act like
printf.
croak("Can't opendir %s!", dirname);