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

I've got some old C code that takes a function pointer as it's argument... I simply cannot figure out how (or if) you can XS it ...
void funky(void (*f)(void)) { // some things .. and an execution of *f }
I happilly entered that into my .xs file thinkin' that would actually work. *giggle*
void funky(f) void (*f)(void)
Is there any way to do this? Will this actually work if I pass in a perl function pointer?
thingy::funky(\&this_works); # ?

Replies are listed 'Best First'.
Re: XS N' void (*f) (void)
by jettero (Monsignor) on Jul 30, 2000 at 21:02 UTC
    Answering my own question ... I rather think that you cannot. I replaced my old C callback-set-function with this--it does exactly what I wanted in the first place:
    static SV * fp = (SV*) NULL; void call_funky() { dSP; if(fp == (SV*) NULL) return; PUSHMARK(SP); perl_call_sv(fp, G_DISCARD); } MODULE = sometin PACKAGE = somethin void funky(f) SV * f CODE: if (fp == (SV*)NULL) fp = newSVsv(f); else SvSetSV(fp, f);

      A bit late for a reply, but...

      Very nice job on handling the call-back. You make it look so simple that I'm ashamed that I never got that far when I had this challenge. (: I guess it gets trickier if you have one call-back per object and need to pass the object in to the call-back function, which is what I was doing, but that still sounds doable based on what you did.

      Anyway, I was really replying to mention the FFI module that supports building call-backs at run time, usually for use with dynamic libraries. I haven't actually gotten to use it since I found out about it, but I've heard good things about it.

              - tye (but my friends call me "Tye")