Pointers to functions in C ? ... then wrapped in an SV ?
C function pointers are directly analogous to perl subroutine references. The big differences are...
- They're typed, so function signatures must match.
- The syntax for declaring them is bloody awful. In fact, it's so awful that many people use typedefs whenever possible.
- You can't verify that a non-NULL function pointer actually points to anything valid, unlike in Perl where you can verify a sub ref with ref($sub).
Here's a demo for passing a function pointer around via SV.
use strict;
use warnings;
use Inline C => <<'END_C';
typedef void
(*hello_func_t)();
void
hola_mundo()
{
printf("Hola, mundo!\n");
}
SV*
get_hola_mundo_func_ptr()
{
return newSViv( PTR2IV(hola_mundo) );
}
void
do_hello(SV *sv_with_func_ptr)
{
IV temp = SvIV(sv_with_func_ptr);
hello_func_t hello = INT2PTR(hello_func_t, temp);
hello();
}
END_C
my $func_ptr = get_hola_mundo_func_ptr();
do_hello($func_ptr);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.