Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Calling builtin functions from Inline::C

by diotalevi (Canon)
on Aug 04, 2003 at 12:06 UTC ( [id://280643]=perlquestion: print w/replies, xml ) Need Help??

diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

This snippet show the original C code from zopen.c calling read() and then my substitute in some Inline::C code using perl's read(). I don't know that it is "ok" to call perl builtins from Inline::C by calling CORE:: functions. So is it? And is there a better way to write this?

/* Check the magic number */ if (read(zs->zs_fh, header, sizeof(header)) != sizeof(header) || memcmp(header, z_magic, sizeof(z_magic)) != 0) { errno = EFTYPE; return (-1); } /* Check the magic number */ Inline_Stack_Reset; Inline_Stack_Push(zs->zs_fh); /* a SV* of a IO handle passed in ea +rlier */ Inline_Stack_Push(header); Inline_Stack_Push(sizeof(header)); perl_call_pv("CORE::read",G_SCALAR); read_rval = (ssize_t)POPi; Inline_Stack_Done; if (read_rval != sizeof(header) || memcmp(header, z_magic, sizeof(z_magic)) != 0) { errno = EFTYPE; return (-1); }

Replies are listed 'Best First'.
Re: Calling builtin functions from Inline::C
by derby (Abbot) on Aug 04, 2003 at 12:35 UTC
    I don't know about better, it really all depends on how you're passing around zs and if/where it's being read in other parts of your code. If I was going to do this, I would either implement the reading in straight C or use PerlIO_read (perlapio) if portability is desired. And I know it's just a snippet but the one thing I would not do is return a -1 to the calling perl function - an undef is more idiomatic (&PL_sv_undef).

    -derby

      Ok, so if I use PerlIO_read() (the matching PerlIO call) that respects perlish filehandles where really they might be tied things, objects, actual filehandles, handles to *DATA, or other odd things? Or if I want to handle all the magic correctly do I just need to have a wrapper function like sub read { CORE::read( ... ) }?

Re: Calling builtin functions from Inline::C
by mpeppler (Vicar) on Aug 04, 2003 at 13:40 UTC
    I've not used Inline::C, but as someone who has messed with XS for quite a few years I'll second derby's comments - using straight C or PerlIO_read() sounds like a better solution here.

    Michael

Re: Calling builtin functions from Inline::C
by diotalevi (Canon) on Aug 04, 2003 at 17:01 UTC

    For anyone that is curious, it doesn't actually work to perl_call_cv("CORE::read",...). What the actual or best answer is... *shrug*. I dunno.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://280643]
Approved by broquaint
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-25 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found