Tassilo Parseval <[EMAIL PROTECTED]> writes: >> >> > >> >> > PerlIO *io; >> >> > char mode[8]; >> >> > PerlIO_intmode2str(O_RDONLY|O_NONBLOCK, mode, NULL); >> >> > io = PerlIO_fdopen(self->fd, mode); PerlIO_intmode2str(O_RDONLY|O_NONBLOCK, mode, NULL); >> >> The O_NONBLOCK _may_ be confusing it. >> And as thing is already open doesn't contribute much. > >Unfortunately, the O_NONBLOCK is essential in my case as this module >provides access to all the ioctls of a CDROM-drive as permitted by the >kernel. Otherwise I couldn't open a drive with no CD in it.
Fine - and you have opened it and got the fd. PerlIO_fdopen then takes that already open fd and associates a PerlIO * with it. PerlIO_intmde2str takes O_RDONLY and turns it into stdio-like "r". Stdio and PerlIO have no way to represent O_NONBLOCK so (having now looked) it just ignores it. > >I was just trying to reproduce the result of using the typemap. What was >returned was not undef according to Devel::Peek. The funny thing now is: >The above, which didn't work this morning, now works! I am seriously >confused. Any chance some other process had the CDROM in use "this morning"? > >It'd be nice having a bridge between Perl filehandles (that is, >references to globs) and C handles (both file-descriptors and FILE*). >Often C libraries work on filehandles but exposing those to pure Perl as >glob-references is tricky. In particular, I wish we had > > PerlIO *PerlIO_importfd(int fd, int mode); You do have: PerlIO *PerlIO_fdopen(int fd, const char *); What seems to be troubling us is getting a glob ref from one of those so what we want is SV *PerlIO_perlhandle(PerlIO *f); > >Going the other way (XSUB gets a glob-ref and needs FILE*/fd) isn't >always smooth either. First step is to get the IO * Perl_sv2io() does that. Next you get your PerlIO * - you have a choice of two the one for output and the one for input (often same but not for sockets or (some) ttys). Getting a FILE * isn't smooth, but we have an API for that. Getting a fd is just PerlIO_fileno(PerlIO *f) >For that purpose, I made two macros: > > #ifdef _WIN32 > # define PerlIO_exportFILE(f,fl) ((FILE*)(f)) > #endif >Especially the _WIN32 branch is fishy. But it was on this list that >someone said that on windows a PerlIO* would just be an alias for FILE*. It was on perl5.6 - not on perl5.8. > > #define sv_to_file(sv) (PerlIO_exportFILE(IoIFP(sv_2io(sv)), NULL)) > That is "okay" anywhere with perl5.8+ - but please read the PODs. In perl5.8+ there isn't necessarily a FILE * in normal perl IO. So the above may have to FILE *stdio = fdopen(PerlIO_fileno(pio),mode)) and how the FILE *stdio and PerlIO *pio interact is "messy".