PERLCALL(1perl)
PERLCALL(1perl) β straight out of the Perl Programmers Reference Guide, bestie π
β¨ NAME β¨
perlcall β Perl calling conventions from C. iykyk π«Ά
π¦ DESCRIPTION π¦
okay so the whole point of this doc? teaching you how to call Perl subroutines directly from C, which is literally just "how to write callbacks" but make it sound fancy β¨
so yeah β besides walking through the C interface Perl gives you for writing callbacks, this doc also runs a whole bunch of examples so you can actually see the interface doing its thing in practice. and on top of that there's some callback-coding technique talk sprinkled in there too. so much going on, we love a multitasker π
situations where you lowkey need callbacks, like:
- An Error Handler π
so picture this: you made an XSUB interface to some app's C API. iconic. and like, a super common thing in apps is letting you define a C function that gets called whenever something nasty happens. so what we actually want is to be able to say "hey, call this Perl subroutine instead". that's the dream π
- An Event-Driven Program π¦
the classic callback moment fr fr: writing an event-driven program, like for an X11 application. you register functions that get called whenever specific events happen, e.g. a mouse button is pressed, the cursor moves into a window, or a menu item is selected. very main character of them π
and yes, all this technique stuff also applies when you're embedding Perl in a C program β but heads up, that's not the main goal of this doc. embedding has its own extra details you gotta think about, and those are specific to embedding. so if embedding Perl in C is your thing, go read perlembed. π«Ά
before you launch yourself head first into the rest of this document (and honestly, respect), it would be a good idea to have read these two: perlxs and perlguts. do your homework bestie, no cap π
π THE CALL_ FUNCTIONS π
ngl this stuff is way easier to explain with examples, but first you need to be aware of a few important definitions. no skipping, babe π
Perl has a number of C functions that let you call Perl subroutines. they are:
I32 call_sv(SV* sv, I32 flags);
I32 call_pv(char *subname, I32 flags);
I32 call_method(char *methname, I32 flags);
I32 call_argv(char *subname, I32 flags, char **argv);
the key function is call_sv. all the other ones are just fairly simple wrappers that make life easier when you're calling Perl subroutines in special cases. at the end of the day they will all call call_sv to invoke the Perl subroutine. they're giving entourage, not lead role π
every one of the call_* functions has a flags parameter, which is used to pass a bit mask of options to Perl. this bit mask operates identically for each of the functions, so no weird special-case drama. the settings available in the bit mask get discussed in "FLAG VALUES" β
kay now let's go through each function one at a time:
πΈ call_sv
call_sv takes two parameters. the first, sv, is an SV*. this lets you specify the Perl subroutine you want to call either as a C string (which you've first converted to an SV) or as a reference to a subroutine. the section "Using call_sv" shows you how to make use of call_sv. π
πΈ call_pv
the function call_pv is similar to call_sv except it expects its first parameter to be a C char* which identifies the Perl subroutine you want to call, e.g. call_pv("fred", 0). and if the subroutine you want to call lives in another package, just include the package name in the string, e.g. pkg::fred. easy β¨
πΈ call_method
the function call_method is used to call a method from a Perl class. the parameter methname corresponds to the name of the method to be called. important: the class that the method belongs to is passed on the Perl stack rather than in the parameter list π this class can be either the name of the class (for a static method) or a reference to an object (for a virtual method). go read perlobj for more on static and virtual methods, and "Using call_method" for an example of using call_method. π
πΈ call_argv
call_argv calls the Perl subroutine specified by the C string stored in the subname parameter. it also takes the usual flags parameter. the final parameter, argv, consists of a NULL-terminated list of C strings to be passed as parameters to the Perl subroutine. see "Using call_argv". π
all the functions return an integer. that integer is a count of the number of items returned by the Perl subroutine. and the actual items that came back from the subroutine are stored on the Perl stack. π
as a general rule you should always check the return value from these functions. even if you're expecting only a particular number of values to come back from the Perl subroutine, there is nothing stopping someone from doing something unexpected β don't say you haven't been warned bestie. π
π© FLAG VALUES π©
okay so the flags parameter in literally all the call_* functions is one of G_VOID, G_SCALAR, or G_LIST β those are the ones that say what context the call happens in β OR'ed together with a bit mask made of any combo of the other G_* symbols listed down below. iykyk π
β¨ G_VOID β¨
Calls the Perl subroutine in a void context. π
This flag does 2 things:
- It tells the subroutine being called that it's running in a void context (so if it runs
wantarray, the result is the undefined value). - It makes sure nothing actually comes back out of the subroutine.
The value returned by the call_* function tells you how many items the Perl subroutine handed back β and in this case, bestie, it's gonna be 0. zero. nada. π
β¨ G_SCALAR β¨
Calls the Perl subroutine in a scalar context. This is the default context flag setting for all the call_* functions. so if you don't say anything, this is the vibe you get πΈ
This flag also has 2 effects:
- It tells the subroutine being called that it's running in a scalar context (so if it runs
wantarray, the result is false). - It makes sure only a scalar actually comes back out of the subroutine. The subroutine can, of course, ignore the
wantarrayand return a list anyway. If it does that, then only the last element of the list gets returned. rude but true π
The value returned by the call_* function tells you how many items the Perl subroutine returned β in this case it's either 0 or 1.
- If it's 0, then you specified the
G_DISCARDflag. - If it's 1, then the item the Perl subroutine actually returned is stored on the Perl stack β the section "Returning a Scalar" shows how to get at this value on the stack. And remember: no matter how many items the Perl subroutine returns, only the last one is reachable from the stack. think of the case where only one value is returned as a list with only one element. any other items that came back are just gone by the time control returns from the
call_*function. the section "Returning a List in Scalar Context" has an example of this behavior. π
β¨ G_LIST β¨
Calls the Perl subroutine in a list context. Prior to Perl version 5.35.1 this was called G_ARRAY. (throwback era, we don't talk about it π«Ά)
Same energy as G_SCALAR β this flag has 2 effects:
- It tells the subroutine being called that it's running in a list context (so if it runs
wantarray, the result is true). - It makes sure all the items returned from the subroutine are reachable once control returns from the
call_*function.
The value returned by the call_* function tells you how many items the Perl subroutine returned.
- If it's 0, then you specified the
G_DISCARDflag. - If it's not 0, then it's a count of how many items the subroutine returned. Those items are stored on the Perl stack. the section "Returning a List of Values" has an example of using the
G_LISTflag and how the mechanics of grabbing the returned items off the Perl stack actually work. π₯
β¨ G_DISCARD β¨
By default, the call_* functions put the items returned by the Perl subroutine on the stack. If you literally do not care about those items, then setting this flag makes Perl get rid of them for you automatically. and bestie, you can still tell the Perl subroutine what context it's in by using either G_SCALAR or G_LIST alongside it. π
β οΈ If you do not set this flag then it is very important that you make sure any temporaries (i.e., parameters passed to the Perl subroutine and values returned from the subroutine) get disposed of by you. the section "Returning a Scalar" gives the deets on how to dispose of these temporaries explicitly, and the section "Using Perl to Dispose of Temporaries" talks about the specific circumstances where you can just ignore the problem and let Perl deal with it for you. don't skip this part fr fr π
β¨ G_NOARGS β¨
Whenever a Perl subroutine gets called using one of the call_* functions, it's assumed by default that parameters are being passed to the subroutine. If you aren't passing any parameters to the Perl subroutine, you can save a lil bit of time by setting this flag. It has the effect of not creating the @_ array for the Perl subroutine.
Although the functionality this flag provides may look straightforward, it should only be used if there's a genuinely good reason to do so. The reason to be cautious is that, even if you have specified the G_NOARGS flag, it's still possible for the Perl subroutine that got called to think you passed it parameters. π
In fact, what can happen is that the Perl subroutine you called can access the @_ array from a previous Perl subroutine. This happens when the code executing the call_* function has itself been called from another Perl subroutine. The code below illustrates this:
sub fred
{ print "@_\n" }
sub joe
{ &fred }
&joe(1,2,3);
This will print
1 2 3
What happened here is that fred accessed the @_ array that belongs to joe. sneakyyyy π
β¨ G_EVAL β¨
It's possible for the Perl subroutine you're calling to terminate abnormally β like, calling die explicitly, or just not actually existing. By default, when either of those things happens, the process terminates immediately. π± If you wanna trap this type of event, specify the G_EVAL flag. It puts an eval { } around the subroutine call.
Whenever control returns from the call_* function, you need to check the $@ variable, exactly like you would in a normal Perl script. πΈ
The value returned from the call_* function depends on what other flags you specified and whether an error happened. Here are all the different cases that can occur:
- If the
call_*function returns normally, then the value returned is as specified in the previous sections. - If
G_DISCARDis specified, the return value will always be 0. - If
G_LISTis specified and an error has occurred, the return value will always be 0. - If
G_SCALARis specified and an error has occurred, the return value will be 1 and the value on the top of the stack will beundef. This means that if you've already detected the error by checking$@and you want the program to keep going, you must remember to pop theundeffrom the stack. β οΈ don't forget, bestie, this one bites people.
See "Using G_EVAL" for the details on using G_EVAL.
β¨ G_KEEPERR β¨
Using the G_EVAL flag described above will always set $@: clearing it if there was no error, and setting it to describe the error if there was one in the called code. This is exactly what you want if your intention is to handle possible errors, but sometimes you just wanna trap errors and stop them from interfering with the rest of the program. π«Ά
This scenario mostly applies to code that's meant to be called from within destructors, asynchronous callbacks, and signal handlers. In those situations, where the code being called has little relation to the surrounding dynamic context, the main program needs to be insulated from errors in the called code β even if they can't be handled intelligently. It may also be useful to do this with code for __DIE__ or __WARN__ hooks, and tie functions.
The G_KEEPERR flag is meant to be used alongside G_EVAL in call_* functions that are used to implement such code, or with eval_sv. This flag has no effect on the call_* functions when G_EVAL is not used.
When G_KEEPERR is used, any error in the called code will terminate the call as usual, and the error will not propagate beyond the call (as usual for G_EVAL), but it will not go into $@. Instead, the error gets converted into a warning, prefixed with the string "\t(in cleanup)". This can be disabled using no warnings 'misc'. If there's no error, $@ will not be cleared.
Note that the G_KEEPERR flag does not propagate into inner evals; those may still set $@. π
The G_KEEPERR flag was introduced in Perl version 5.002.
See "Using G_KEEPERR" for an example of a situation that warrants using this flag.
β¨ Determining the Context β¨
As mentioned above, you can figure out the context of the currently executing subroutine in Perl with wantarray. The equivalent test can be made in C by using the GIMME_V macro, which returns G_LIST if you've been called in a list context, G_SCALAR if in a scalar context, or G_VOID if in a void context (i.e., the return value won't be used). A deprecated older version of this macro is called GIMME; in a void context it returns G_SCALAR instead of G_VOID. An example of using the GIMME_V macro is shown in section "Using GIMME_V". β
π EXAMPLES π
Enough of the definition talk! Let's have a few examples. finally, the fun part π
Perl provides many macros to assist in accessing the Perl stack. Wherever possible, these macros should always be used when interfacing with Perl internals. We hope this makes the code less vulnerable to any changes made to Perl in the future. future-proofing is the moment β¨
Another point worth noting: in the first series of examples I've used only the call_pv function. This was done to keep the code simpler and ease you into the topic. Wherever possible, if the choice is between using call_pv and call_sv, you should always try to use call_sv. See "Using call_sv" for details. π
πΈ No Parameters, Nothing Returned πΈ
This first trivial example will call a Perl subroutine, PrintUID, to print out the UID of the process.
sub PrintUID
{
print "UID is $<\n";
}
and here's a C function to call it
static void
call_PrintUID()
{
dSP;
PUSHMARK(SP);
call_pv("PrintUID", G_DISCARD|G_NOARGS);
}
Simple, eh? π¦
A few points to note about this example:
- Ignore
dSPandPUSHMARK(SP)for now. They'll be discussed in the next example. - We aren't passing any parameters to
PrintUIDsoG_NOARGScan be specified. - We aren't interested in anything returned from
PrintUID, soG_DISCARDis specified. Even ifPrintUIDgot changed to return some value(s), having specifiedG_DISCARDmeans they'll be wiped by the time control returns fromcall_pv. - As
call_pvis being used, the Perl subroutine is specified as a C string. In this case the subroutine name has been 'hard-wired' into the code. - Because we specified
G_DISCARD, it's not necessary to check the value returned fromcall_pv. It will always be 0.
πΈ Passing Parameters πΈ
Now let's make a slightly more complex example. This time we wanna call a Perl subroutine, LeftString, which takes 2 parameters β a string ($s) and an integer ($n). The subroutine just prints the first $n characters of the string.
So the Perl subroutine would look like this:
sub LeftString
{
my($s, $n) = @_;
print substr($s, 0, $n), "\n";
}
and bestie, we're just getting started, hold on π
The C function required to call LeftString would look like this:
static void
call_LeftString(a, b)
char * a;
int b;
{
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sv_2mortal(newSVpv(a, 0)));
PUSHs(sv_2mortal(newSViv(b)));
PUTBACK;
call_pv("LeftString", G_DISCARD);
FREETMPS;
LEAVE;
}
okay bestie, here are a few notes on that C function, call_LeftString, because there is so much lore happening in those twelve lines π
- Parameters get passed to the Perl subroutine via the Perl stack. That's literally the whole point of the chunk starting at the line
dSPand ending at the linePUTBACK. ThedSPline declares a local copy of the stack pointer, and you always access that local copy asSP. - If you're gonna put something onto the Perl stack, you need to know where to put it. That is the entire job of the
dSPmacro β it declares and initializes a local copy of the Perl stack pointer.
Every other macro in this example needs you to have used that macro first. The one exception? If you're calling a Perl subroutine directly from an XSUB function β then you don't have to use the dSP macro explicitly at all, it gets declared for you automatically. Cute of it honestly β¨
- Any parameters you push onto the stack have to be bracketed by the
PUSHMARKandPUTBACKmacros. Why? Because those two are counting how many parameters you're pushing, automatically. That way, whenever Perl builds the@_array for the subroutine, it knows exactly how big to make it.
PUSHMARK tells Perl to make a mental note of the current stack pointer. And even if you're passing zero parameters (like the example in the section "No Parameters, Nothing Returned") you still have to call PUSHMARK before calling any of the call_* functions β Perl still needs to know that there are no parameters. Not it guessing. it needs to KNOW.
PUTBACK sets the global copy of the stack pointer to be the same as our local copy. Skip that and call_pv would have no idea where those two parameters we pushed went β because remember, up to that point every bit of stack pointer fiddling we did was on the local copy, not the global one.
- Next up:
EXTENDandPUSHs. This is where the parameters actually get pushed onto the stack. In this case we're pushing a string and an integer.
Alternatively you can use the XPUSHs() macro, which combines EXTEND(SP, 1) and PUSHs() into one. Heads up though β that's less efficient if you're pushing multiple values.
For the full tea on how the PUSH macros work, see "XSUBs and the Argument Stack" in perlguts.
- Because we made temporary values (hi yes, via those
sv_2mortal()calls) we have to tidy up the Perl stack and dispose of the mortal SVs. That's what this is for:
ENTER;
SAVETMPS;
at the top of the function, and
FREETMPS;
LEAVE;
at the bottom. The ENTER/SAVETMPS pair creates a boundary for any temporaries we create β meaning the temporaries we throw out get limited to the ones created after those calls.
The FREETMPS/LEAVE pair will get rid of any values returned by the Perl subroutine (see the next example for that) plus it dumps the mortal SVs we created. Having ENTER/SAVETMPS at the start of the code makes sure no other mortals get destroyed. protect the mortals!!
Think of these macros as basically doing what { and } do in Perl β limiting the scope of local variables.
And if you want an alternative to using these macros, see the section "Using Perl to Dispose of Temporaries".
- Finally,
LeftStringcan be called via thecall_pvfunction. The only flag specified this time isG_DISCARD. And because we're passing 2 parameters to the Perl subroutine here, we have not specifiedG_NOARGS.
π Returning a Scalar π
Now for an example of dealing with the items returned from a Perl subroutine.
Here's a Perl subroutine, Adder, that takes 2 integer parameters and simply returns their sum.
sub Adder
{
my($x, $y) = @_;
$x + $y;
}
And because we actually care about the return value from Adder now, the C function required to call it gets a lil more complex. It's giving β¨ character development β¨
static void
call_Adder(a, b)
int a;
int b;
{
dSP;
int count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sv_2mortal(newSViv(a)));
PUSHs(sv_2mortal(newSViv(b)));
PUTBACK;
count = call_pv("Adder", G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Big trouble\n");
printf ("The sum of %d and %d is %d\n", a, b, POPi);
PUTBACK;
FREETMPS;
LEAVE;
}
Points to note this time:
- The only flag specified this time was
G_SCALAR. That means the@_array gets created, and the value returned byAdderstill exists after the call tocall_pv. Slay. - The purpose of the
SPAGAINmacro is to refresh the local copy of the stack pointer. Necessary because it's possible the memory allocated to the Perl stack got reallocated during thecall_pvcall. π
So if you're making use of the Perl stack pointer in your code, you must always refresh the local copy using SPAGAIN whenever you use the call_* functions or any other Perl internal function. No skipping, bestie.
- Even though only a single value was expected back from
Adder, it's still good practice to check the return code fromcall_pvanyway.
Expecting a single value is not quite the same thing as knowing there will be one. If someone modified Adder to return a list and we didn't check for that and act accordingly, the Perl stack would end up in an inconsistent state. And that is something you really don't want to happen. Ever. Not even once.
- The
POPimacro is used here to pop the return value off the stack. In this case we wanted an integer, soPOPiit is.
Here's the complete list of POP macros available, plus the types they return:
POPs SV
POPp pointer (PV)
POPpbytex pointer to bytes (PV)
POPn double (NV)
POPi integer (IV)
POPu unsigned integer (UV)
POPl long
POPul unsigned long
And since these macros have side-effects, don't use them as arguments to macros that might evaluate their argument several times. For example:
/* Bad idea, don't do this */
STRLEN len;
const char *s = SvPV(POPs, len);
Instead, use a temporary:
STRLEN len;
SV *sv = POPs;
const char *s = SvPV(sv, len);
or a macro that guarantees it'll evaluate its arguments only once:
STRLEN len;
const char *s = SvPVx(POPs, len);
- The final
PUTBACKleaves the Perl stack in a consistent state before exiting the function. Necessary because when we popped the return value off the stack withPOPi, it only updated our local copy of the stack pointer. And remember:PUTBACKsets the global stack pointer to be the same as our local copy. Teamwork, honestly. π«Ά
πΈ Returning a List of Values πΈ
Now let's extend the previous example to return both the sum of the parameters and the difference. Two for one, iconic behaviour.
Here's the Perl subroutine:
sub AddSubtract
{
my($x, $y) = @_;
($x+$y, $x-$y);
}
and this is the C function:
static void
call_AddSubtract(a, b)
int a;
int b;
{
dSP;
int count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sv_2mortal(newSViv(a)));
PUSHs(sv_2mortal(newSViv(b)));
PUTBACK;
count = call_pv("AddSubtract", G_LIST);
SPAGAIN;
if (count != 2)
croak("Big trouble\n");
printf ("%d - %d = %d\n", a, b, POPi);
printf ("%d + %d = %d\n", a, b, POPi);
PUTBACK;
FREETMPS;
LEAVE;
}
If call_AddSubtract is called like this:
call_AddSubtract(7, 4);
then here's the output:
7 - 4 = 3
7 + 4 = 11
Notes π₯
- We wanted list context, so
G_LISTwas used. - Not surprisingly,
POPiis used twice this time since we were retrieving 2 values from the stack. The important thing to note: when using thePOP*macros, they come off the stack in reverse order. Always. fr fr.
π¦ Returning a List in Scalar Context π¦
Say the Perl subroutine from the previous section got called in scalar context, like this:
static void
call_AddSubScalar(a, b)
int a;
int b;
{
dSP;
int count;
int i;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sv_2mortal(newSViv(a)));
PUSHs(sv_2mortal(newSViv(b)));
PUTBACK;
count = call_pv("AddSubtract", G_SCALAR);
SPAGAIN;
printf ("Items Returned = %d\n", count);
for (i = 1; i <= count; ++i)
printf ("Value %d = %d\n", i, POPi);
PUTBACK;
FREETMPS;
LEAVE;
}
The other modification made is that call_AddSubScalar will print the number of items returned from the Perl subroutine and their value (for simplicity it assumes they're integers, don't come for it). So if call_AddSubScalar is called:
call_AddSubScalar(7, 4);
then the output will be:
Items Returned = 1
Value 1 = 3
In this case the main point to note is that only the last item in the list gets returned from the subroutine. AddSubtract actually made it back to call_AddSubScalar. It ate and left no crumbs, but it also left some values behind. π
π Returning Data from Perl via the Parameter List π
It's also possible to return values directly via the parameter list β whether it's actually desirable to do it is another matter entirely, and honestly that's a whole separate conversation bestie.
The Perl subroutine Inc, below, takes 2 parameters and increments each directly.
sub Inc
{
++ $_[0];
++ $_[1];
}
and then here's the C function that hits it up π
static void
call_Inc(a, b)
int a;
int b;
{
dSP;
int count;
SV * sva;
SV * svb;
ENTER;
SAVETMPS;
sva = sv_2mortal(newSViv(a));
svb = sv_2mortal(newSViv(b));
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sva);
PUSHs(svb);
PUTBACK;
count = call_pv("Inc", G_DISCARD);
if (count != 0)
croak ("call_Inc: expected 0 values from 'Inc', got %d\n",
count);
printf ("%d + 1 = %d\n", a, SvIV(sva));
printf ("%d + 1 = %d\n", b, SvIV(svb));
FREETMPS;
LEAVE;
}
okay so real talk: to actually get at those two args you pushed onto the stack after call_pv comes back, you gotta write down where they live. That's literally the whole reason sva and svb exist bestie π
and why though?? because the chunk of the Perl stack that was holding them will almost definitely have been written over by something else by the time control comes back from call_pv. it's giving gone, reduced to atoms π
β¨ Using G_EVAL β¨
now the glow-up: an example with G_EVAL. Below is a Perl sub that computes the difference of its 2 parameters. if that would give you a negative result, the sub calls die π
sub Subtract
{
my ($x, $y) = @_;
die "death can be fatal\n" if $x < $y;
$x - $y;
}
and some C to call it
static void
call_Subtract(a, b)
int a;
int b;
{
dSP;
int count;
SV *err_tmp;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sv_2mortal(newSViv(a)));
PUSHs(sv_2mortal(newSViv(b)));
PUTBACK;
count = call_pv("Subtract", G_EVAL|G_SCALAR);
SPAGAIN;
/* Check the eval first */
err_tmp = ERRSV;
if (SvTRUE(err_tmp))
{
printf ("Uh oh - %s\n", SvPV_nolen(err_tmp));
POPs;
}
else
{
if (count != 1)
croak("call_Subtract: wanted 1 value from 'Subtract', got %d\n",
count);
printf ("%d - %d = %d\n", a, b, POPi);
}
PUTBACK;
FREETMPS;
LEAVE;
}
if call_Subtract gets called like this
call_Subtract(4, 5)
you get this printed out
Uh oh - death can be fatal
Notes
- we wanna be able to catch the
die, so bestie we used theG_EVALflag π«Ά not setting that flag would mean your program just ends immediately at thediestatement inside the subSubtract. periodt. - this chunk
err_tmp = ERRSV;
if (SvTRUE(err_tmp))
{
printf ("Uh oh - %s\n", SvPV_nolen(err_tmp));
POPs;
}
is the exact same energy as this bit of Perl
print "Uh oh - $@\n" if $@;
PL_errgv is a perl global of type GV * that points at the symbol table entry holding the error. so ERRSV refers to the C glow-up of $@ β¨ we use a local temporary, err_tmp, because ERRSV is a macro that calls a function, and SvTRUE(ERRSV) would end up calling that function multiple times. not efficient, not cute, we don't do that π
- notice the stack gets popped using
POPsin the block whereSvTRUE(err_tmp)is true. this is necessary β whenever acall_*function invoked withG_EVAL|G_SCALARreturns an error, the top of the stack is holding the value undef. since we want the program to keep going after spotting this error, tidying up that stack by removing the undef is lowkey essential. no cap.
β¨ Using G_KEEPERR β¨
okay now consider this kind of facetious example, where we've used an XS version of the call_Subtract example above inside a destructor:
package Foo;
sub new { bless {}, $_[0] }
sub Subtract {
my($x,$y) = @_;
die "death can be fatal" if $x < $y;
$x - $y;
}
sub DESTROY { call_Subtract(5, 4); }
sub foo { die "foo dies"; }
package main;
{
my $foo = Foo->new;
eval { $foo->foo };
}
print "Saw: $@" if $@; # should be, but isn't
this example will fail to recognize that an error happened inside the eval {}. and here's why π the call_Subtract code got run while perl was cleaning up temporaries when exiting the outer braced block, and because call_Subtract is implemented with call_pv using the G_EVAL flag, it immediately reset $@. which means the outermost test for $@ fails, and therefore the whole error trap fails. it's giving sabotage π
appending the G_KEEPERR flag, so the call_pv call in call_Subtract reads:
count = call_pv("Subtract", G_EVAL|G_SCALAR|G_KEEPERR);
will preserve the error and bring back reliable error handling. slay π«Ά
β¨ Using call_sv β¨
in all the previous examples I was hard-wiring the name of the Perl sub to be called from C. but like, most of the time it's way more convenient to be able to specify the name of the Perl sub from inside the Perl script itself, and that's where call_sv comes in bestie πΈ
peep the Perl code below
sub fred
{
print "Hello there\n";
}
CallSubPV("fred");
here's a snippet of XSUB which defines CallSubPV.
void
CallSubPV(name)
char * name
CODE:
PUSHMARK(SP);
call_pv(name, G_DISCARD|G_NOARGS);
that's fine as far as it goes. the thing is, the Perl sub can be specified as only a string β but Perl also allows references to subroutines and anonymous subroutines. that's where call_sv eats π
the code below for CallSubSV is identical to CallSubPV except the name parameter is now defined as an SV* and we use call_sv instead of call_pv.
void
CallSubSV(name)
SV * name
CODE:
PUSHMARK(SP);
call_sv(name, G_DISCARD|G_NOARGS);
because we're using an SV to call fred, all of these work:
CallSubSV("fred");
CallSubSV(\&fred);
$ref = \&fred;
CallSubSV($ref);
CallSubSV( sub { print "Hello there\n" } );
as you can see, call_sv gives you so much more flexibility in how you can specify the Perl subroutine. iconic behavior β¨
but you should note: if it's necessary to store the SV (name in the example above) which corresponds to the Perl subroutine so it can be used later in the program, it is not enough to just store a copy of the pointer to the SV. say the code above had been like this:
static SV * rememberSub;
void
SaveSub1(name)
SV * name
CODE:
rememberSub = name;
void
CallSavedSub1()
CODE:
PUSHMARK(SP);
call_sv(rememberSub, G_DISCARD|G_NOARGS);
the reason this is wrong is that by the time you come to use the pointer rememberSub in CallSavedSub1, it may or may not still refer to the Perl sub that was recorded in SaveSub1. and this is especially true for these cases:
SaveSub1(\&fred);
CallSavedSub1();
SaveSub1( sub { print "Hello there\n" } );
CallSavedSub1();
by the time each of those SaveSub1 statements has been executed, the SV*s that corresponded to the parameters just... don't exist anymore π expect an error message from Perl shaped like
Can't use an undefined value as a subroutine reference at ...
for each of the CallSavedSub1 lines. ouch.
similarly, with this code
$ref = \&fred;
SaveSub1($ref);
$ref = 47;
CallSavedSub1();
you can expect one of these messages (which one you actually get depends on the version of Perl you're on, iykyk)
Not a CODE reference at ...
Undefined subroutine &main::47 called ...
the variable $ref may have referred to the sub fred whenever the call to SaveSub1 was made, but by the time CallSavedSub1 gets called it's now holding the number 47. because we saved only a pointer to the original SV in SaveSub1, any changes to $ref get tracked by the pointer rememberSub. which means whenever CallSavedSub1 gets called, it'll try to execute the code referenced by the SV* rememberSub. except this time it refers to the integer 47, so expect Perl to complain loudly π
a similar but more subtle problem is illustrated with this code:
$ref = \&fred;
SaveSub1($ref);
$ref = \&joe;
CallSavedSub1();
this time whenever CallSavedSub1 gets called it will execute the Perl sub joe (assuming it exists) rather than fred as was originally requested in the call to SaveSub1. plot twist π
to get around these problems you gotta take a full copy of the SV. the code below shows SaveSub2 modified to do exactly that.
/* this isn't thread-safe */
static SV * keepSub = (SV*)NULL;
void
SaveSub2(name)
SV * name
CODE:
/* Take a copy of the callback */
if (keepSub == (SV*)NULL)
/* First time, so create a new SV */
keepSub = newSVsv(name);
else
/* Been here before, so overwrite */
SvSetSV(keepSub, name);
void
CallSavedSub2()
CODE:
PUSHMARK(SP);
call_sv(keepSub, G_DISCARD|G_NOARGS);
to avoid creating a new SV every single time SaveSub2 is called, the function first checks whether it's been called before. if not, space for a new SV gets allocated and the reference to the Perl sub name is copied to the variable keepSub in one operation using newSVsv. after that, whenever SaveSub2 is called, the existing SV, keepSub, gets overwritten with the new value using SvSetSV. efficient era π
Note: using a static or global variable to store the SV isn't thread-safe. you can either use the MY_CXT mechanism documented in "Safely Storing Static Data in XS" in perlxs which is fast, or store the values in perl global variables, using get_sv(), which is much slower. pick your poison bestie π
β¨ Using call_argv β¨
here's a Perl sub that prints whatever parameters you pass it.
sub PrintList
{
my(@list) = @_;
foreach (@list) { print "$_\n" }
}
and here's an example of call_argv which will call PrintList.
static char * words[] = {"alpha", "beta", "gamma", "delta", NULL};
static void
call_PrintList()
{
call_argv("PrintList", G_DISCARD, words);
}
Ok bestie, tiny little note before we move on: you do not have to call PUSHMARK in this case π
β because call_argv is gonna do it for you. It's got you. It's a whole gentleman. π«Ά
π Using call_method π
Obsessed with this next one, ok. Picture this Perl code, right:
{
package Mine;
sub new
{
my($type) = shift;
bless [@_]
}
sub Display
{
my ($self, $index) = @_;
print "$index: $$self[$index]\n";
}
sub PrintID
{
my($class) = @_;
print "This is Class $class version 1.0\n";
}
}
So this is literally just a super simple class for managing an array πΈ. Besides the constructor new, it declares two methods: one static, one virtual. The static one, PrintID, just prints the class name plus a version number. The virtual one, Display, prints out one single element of the array. Cute, right?
Here's an all-Perl example of it doing its thing:
$x = Mine->new('red', 'green', 'blue');
$x->Display(1);
Mine->PrintID;
and that will print
1: green
This is Class Mine version 1.0
Now β calling a Perl method from C? Honestly pretty straightforward, no cap. You just need three things:
- A reference to the object, if it's a virtual method β or the name of the class, if it's a static method
- The name of the method
- Any other parameters that are specific to the method
Here's a simple XSUB showing the mechanics of calling both the PrintID and Display methods from C π
void
call_Method(ref, method, index)
SV * ref
char * method
int index
CODE:
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(ref);
PUSHs(sv_2mortal(newSViv(index)));
PUTBACK;
call_method(method, G_DISCARD);
void
call_PrintID(class, method)
char * class
char * method
CODE:
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(class, 0)));
PUTBACK;
call_method(method, G_DISCARD);
So, the methods PrintID and Display can be invoked like this β¨:
$x = Mine->new('red', 'green', 'blue');
call_Method($x, 'Display', 1);
call_PrintID('Mine', 'PrintID');
The one and only thing to note: in both the static and virtual methods, the method name is not passed via the stack β it's used as the first parameter to call_method. That's the whole vibe check. π
π Using GIMME_V π
Here's a trivial little XSUB that prints the context it's currently executing in:
void
PrintContext()
CODE:
U8 gimme = GIMME_V;
if (gimme == G_VOID)
printf ("Context is Void\n");
else if (gimme == G_SCALAR)
printf ("Context is Scalar\n");
else
printf ("Context is Array\n");
And here's some Perl to test it out, bestie:
PrintContext;
$x = PrintContext;
@a = PrintContext;
The output from that will be
Context is Void
Context is Scalar
Context is Array
Iconic behaviour honestly. π
π Using Perl to Dispose of Temporaries π
So in all the examples we've done so far, any temporaries created in the callback β meaning parameters passed on the stack to the call_* function, or values returned via the stack β got freed by one of these two methods:
- Specifying the
G_DISCARDflag withcall_* - Explicitly using the
ENTER/SAVETMPS--FREETMPS/LEAVEpairing
But there's actually a third way, and it's giving lazy-girl luxury: you can just let Perl do it for you automatically whenever it regains control after the callback has terminated π. How? Simply by not using the
ENTER;
SAVETMPS;
...
FREETMPS;
LEAVE;
sequence in the callback β and obviously not specifying the G_DISCARD flag either.
β οΈ Heads up though, because there's a catch. If you're gonna use this method, you have to be aware of a possible memory leak that can arise under very specific circumstances. π
To explain those circumstances, you need to know a lil something about the flow of control between Perl and the callback routine.
The examples given at the start of the document β an error handler and an event driven program β are typical of the two main sorts of flow control you're likely to run into with callbacks. And there is a very important distinction between them, so pay attention bestie. π«Ά
In the first example, an error handler, the flow of control could go like this. You've created an interface to an external library. Control can reach the external library like this:
perl --> XSUB --> external library
Whilst control is in the library, an error condition occurs. You previously set up a Perl callback to handle this situation, so it gets executed. Once the callback has finished, control drops back to Perl again. Here's what the flow of control looks like in that situation:
perl --> XSUB --> external library
...
error occurs
...
external library --> call_* --> perl
|
perl <-- XSUB <-- external library <-- call_* <----+
After the error is processed using call_*, control reverts back to Perl more or less immediately. β¨
In the diagram, the further right you go, the more deeply nested the scope is. It's only when control is back with perl on the extreme left of the diagram that you've dropped back to the enclosing scope, and any temporaries you left hanging around will be freed.
In the second example β an event driven program β the flow of control is way more like this:
perl --> XSUB --> event handler
...
event handler --> call_* --> perl
|
event handler <-- call_* <----+
...
event handler --> call_* --> perl
|
event handler <-- call_* <----+
...
event handler --> call_* --> perl
|
event handler <-- call_* <----+
In this case the flow of control can consist of only the repeated sequence
event handler --> call_* --> perl
for practically the complete duration of the program. Which means control may never drop back to the surrounding scope in Perl at the extreme left. π
So what's the big problem? Wellβ¦ if you're expecting Perl to tidy up those temporaries for you, you might be in for a long wait, bestie. For Perl to dispose of your temporaries, control must drop back to the enclosing scope at some stage. In the event driven scenario that may never happen. Which means that, as time goes on, your program will create more and more temporaries, none of which will ever be freed. And as each of those temporaries consumes some memory, your program will eventually consume all the available memory in your system β kapow! π₯
So here's the bottom line β if you're sure control will revert back to the enclosing Perl scope fairly quickly after the end of your callback, then it isn't absolutely necessary to dispose explicitly of any temporaries you may have created. Mind you, if you're at all uncertain about what to do, it doesn't do any harm to tidy up anyway. π§
π¦ Strategies for Storing Callback Context Information π¦
Potentially one of the trickiest problems to overcome when you're designing a callback interface is figuring out how to store the mapping between the C callback function and the Perl equivalent. Lowkey the final boss of callback design. π
To help you understand why this can be a real problem, first consider how a callback gets set up in an all-C environment. Typically a C API provides a function to register a callback, and that function expects a pointer to a function as one of its parameters. Below is a call to a hypothetical function register_fatal, which registers the C function to get called when a fatal error occurs.
register_fatal(cb1);
The single parameter cb1 is a pointer to a function, so you must have defined cb1 in your code, say something like this:
static void
cb1()
{
printf ("Fatal Error\n");
exit(1);
}
Now change that to call a Perl subroutine instead π :
static SV * callback = (SV*)NULL;
static void
cb1()
{
dSP;
PUSHMARK(SP);
/* Call the Perl sub to process the callback */
call_sv(callback, G_DISCARD);
}
void
register_fatal(fn)
SV * fn
CODE:
/* Remember the Perl sub */
if (callback == (SV*)NULL)
callback = newSVsv(fn);
else
SvSetSV(callback, fn);
/* register the callback with the external library */
register_fatal(cb1);
where the Perl equivalent of register_fatal and the callback it registers, pcb1, might look like this:
# Register the sub pcb1
register_fatal(\&pcb1);
sub pcb1
{
die "I'm dying...\n";
}
The mapping between the C callback and the Perl equivalent is stored in the global variable callback. πΈ
This will be adequate if you ever need to have only one callback registered at any time. An example could be an error handler like the code sketched out above. Remember though β repeated calls to register_fatal will replace the previously registered callback function with the new one. Not a merge, just a swap. π
Say, for example, you want to interface to a library which allows asynchronous file i/o. In this case you may be able to register a callback whenever a read operation has completed. To be of any use, we want to be able to call separate Perl subroutines for each file that's opened. As it stands, the error handler example above would not be adequate, because it allows only a single callback to be defined at any time. What we require is a means of storing the mapping between the opened file and the Perl subroutine we want to be called for that file. π
Say the i/o library has a function asynch_read which associates a C function ProcessRead with a file handle fh β this assumes it has also provided some routine to open the file and so obtain the file handle.
asynch_read(fh, ProcessRead)
This may expect the C ProcessRead function of this form
void
ProcessRead(fh, buffer)
int fh;
char * buffer;
{
...
}
okay so bestie π to give this library a Perl interface we need to be able to map between the fh parameter and the Perl subroutine we want called. and a hash? literally the most convenient mechanism for storing this mapping. the code below shows a possible implementation:
static HV * Mapping = (HV*)NULL;
void
asynch_read(fh, callback)
int fh
SV * callback
CODE:
/* If the hash doesn't already exist, create it */
if (Mapping == (HV*)NULL)
Mapping = newHV();
/* Save the fh -> callback mapping */
hv_store(Mapping, (char*)&fh, sizeof(fh), newSVsv(callback), 0);
/* Register with the C Library */
asynch_read(fh, asynch_read_if);
and asynch_read_if could look like this π«Ά :
static void
asynch_read_if(fh, buffer)
int fh;
char * buffer;
{
dSP;
SV ** sv;
/* Get the callback associated with fh */
sv = hv_fetch(Mapping, (char*)&fh , sizeof(fh), FALSE);
if (sv == (SV**)NULL)
croak("Internal error...\n");
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sv_2mortal(newSViv(fh)));
PUSHs(sv_2mortal(newSVpv(buffer, 0)));
PUTBACK;
/* Call the Perl sub */
call_sv(*sv, G_DISCARD);
}
for completeness, here's asynch_close β this one shows how to remove the entry from the hash Mapping:
void
asynch_close(fh)
int fh
CODE:
/* Remove the entry from the hash */
(void) hv_delete(Mapping, (char*)&fh, sizeof(fh), G_DISCARD);
/* Now call the real asynch_close */
asynch_close(fh);
so the Perl interface would look like this πΈ :
sub callback1
{
my($handle, $buffer) = @_;
}
# Register the Perl callback
asynch_read($fh, \&callback1);
asynch_close($fh);
the mapping between the C callback and Perl is stored in the global hash Mapping this time. and using a hash has the distinct advantage that it allows an unlimited number of callbacks to be registered. periodt β¨
okay but what if the interface provided by the C callback doesn't contain a parameter which allows the file handle to Perl subroutine mapping? π say in the asynchronous i/o package, the callback function gets passed only the buffer parameter like this:
void
ProcessRead(buffer)
char * buffer;
{
...
}
without the file handle there is no straightforward way to map from the C callback to the Perl subroutine. π
in this case a possible way around this problem is to predefine a series of C functions to act as the interface to Perl, thus:
#define MAX_CB 3
#define NULL_HANDLE -1
typedef void (*FnMap)();
struct MapStruct {
FnMap Function;
SV * PerlSub;
int Handle;
};
static void fn1();
static void fn2();
static void fn3();
static struct MapStruct Map [MAX_CB] =
{
{ fn1, NULL, NULL_HANDLE },
{ fn2, NULL, NULL_HANDLE },
{ fn3, NULL, NULL_HANDLE }
};
static void
Pcb(index, buffer)
int index;
char * buffer;
{
dSP;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(buffer, 0)));
PUTBACK;
/* Call the Perl sub */
call_sv(Map[index].PerlSub, G_DISCARD);
}
static void
fn1(buffer)
char * buffer;
{
Pcb(0, buffer);
}
static void
fn2(buffer)
char * buffer;
{
Pcb(1, buffer);
}
static void
fn3(buffer)
char * buffer;
{
Pcb(2, buffer);
}
void
array_asynch_read(fh, callback)
int fh
SV * callback
CODE:
int index;
int null_index = MAX_CB;
/* Find the same handle or an empty entry */
for (index = 0; index < MAX_CB; ++index)
{
if (Map[index].Handle == fh)
break;
if (Map[index].Handle == NULL_HANDLE)
null_index = index;
}
if (index == MAX_CB && null_index == MAX_CB)
croak ("Too many callback functions registered\n");
if (index == MAX_CB)
index = null_index;
/* Save the file handle */
Map[index].Handle = fh;
/* Remember the Perl sub */
if (Map[index].PerlSub == (SV*)NULL)
Map[index].PerlSub = newSVsv(callback);
else
SvSetSV(Map[index].PerlSub, callback);
asynch_read(fh, Map[index].Function);
void
array_asynch_close(fh)
int fh
CODE:
int index;
/* Find the file handle */
for (index = 0; index < MAX_CB; ++ index)
if (Map[index].Handle == fh)
break;
if (index == MAX_CB)
croak ("could not close fh %d\n", fh);
Map[index].Handle = NULL_HANDLE;
SvREFCNT_dec(Map[index].PerlSub);
Map[index].PerlSub = (SV*)NULL;
asynch_close(fh);
in this case the functions fn1, fn2, and fn3 are used to remember the Perl subroutine to be called. each of the functions holds a separate hard-wired index which is used in the function Pcb to access the Map array and actually call the Perl subroutine. π§
there are some obvious disadvantages with this technique, ngl:
- firstly, the code is considerably more complex than with the previous example.
- secondly, there is a hard-wired limit (in this case 3) to the number of callbacks that can exist simultaneously. the only way to increase the limit is by modifying the code to add more functions and then recompiling. none the less, as long as the number of functions is chosen with some care, it is still a workable solution and in some cases is the only one available.
to summarize, here are a number of possible methods for you to consider for storing the mapping between C and the Perl callback π
- Ignore the problem - Allow only 1 callback β for a lot of situations, like interfacing to an error handler, this may be a perfectly adequate solution.
- Create a sequence of callbacks - hard wired limit β if it is impossible to tell from the parameters passed back from the C callback what the context is, then you may need to create a sequence of C callback interface functions, and store pointers to each in an array.
- Use a parameter to map to the Perl callback β a hash is an ideal mechanism to store the mapping between C and Perl. π
β¨ Alternate Stack Manipulation β¨
even though I've only made use of the POP* macros to access values returned from Perl subroutines, it is also possible to bypass these macros and read the stack using the ST macro (see perlxs for a full description of the ST macro).
most of the time the POP* macros should be adequate; the main problem with them is that they force you to process the returned values in sequence. this may not be the most suitable way to process the values in some cases. what we want is to be able to access the stack in a random order. the ST macro as used when coding an XSUB is ideal for this purpose. π
the code below is the example given in the section "Returning a List of Values" recoded to use ST instead of POP*:
static void
call_AddSubtract2(a, b)
int a;
int b;
{
dSP;
I32 ax;
int count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(sv_2mortal(newSViv(a)));
PUSHs(sv_2mortal(newSViv(b)));
PUTBACK;
count = call_pv("AddSubtract", G_LIST);
SPAGAIN;
SP -= count;
ax = (SP - PL_stack_base) + 1;
if (count != 2)
croak("Big trouble\n");
printf ("%d + %d = %d\n", a, b, SvIV(ST(0)));
printf ("%d - %d = %d\n", a, b, SvIV(ST(1)));
PUTBACK;
FREETMPS;
LEAVE;
}
Notes π
- notice that it was necessary to define the variable
ax. this is because theSTmacro expects it to exist. if we were in an XSUB it would not be necessary to defineaxas it is already defined for us. - the code
SPAGAIN;
SP -= count;
ax = (SP - PL_stack_base) + 1;
sets the stack up so that we can use the ST macro.
- unlike the original coding of this example, the returned values are not accessed in reverse order. so
ST(0)refers to the first value returned by the Perl subroutine andST(count-1)refers to the last.
π« Creating and Calling an Anonymous Subroutine in C π«
as we've already shown, call_sv can be used to invoke an anonymous subroutine. however, our example showed a Perl script invoking an XSUB to perform this operation. let's see how it can be done inside our C code: π¦
...
SV *cvrv
= eval_pv("sub {
print 'You will not find me cluttering any namespace!'
}", TRUE);
...
call_sv(cvrv, G_VOID|G_NOARGS);
eval_pv is used to compile the anonymous subroutine, which will be the return value as well (read more about eval_pv in "eval_pv" in perlapi). once this code reference is in hand, it can be mixed in with all the previous examples we've shown. π
π« LIGHTWEIGHT CALLBACKS π«
okay bestie so sometimes you just gotta call the same sub over and over and over π like, this is the whole thing with functions that operate on a list of values, think Perl's built-in sort(). You hand sort() a comparison function, and then it calls that function for every single pair of values that needs comparing. And first() and reduce() from List::Util? Yeah, same vibe, same pattern. it's giving "callback central".
So in that situation, you can speed the whole routine up β often quite substantially, no exaggeration β by using the lightweight callback API. The β¨ big idea β¨ is that the calling context only has to be created and destroyed once, and then the sub can be called however many times you want in between. Set it up, party, tear it down. iconic.
And the way you pass parameters here is usually via global variables (typically $_ when you've got one parameter, or $a and $b when you've got two) instead of going through @_. π«Ά
ngl though β it is possible to use the @_ mechanism if you really know what you're doing, but there's as yet no supported API for it, so proceed at your own risk. Also? It's inherently slower. so like. why would you. π
The pattern of macro calls goes like this, and yes you copy it exactly bestie:
dMULTICALL; /* Declare local variables */
U8 gimme = G_SCALAR; /* context of the call: G_SCALAR,
* G_LIST, or G_VOID */
PUSH_MULTICALL(cv); /* Set up the context for calling cv,
and set local vars appropriately */
/* loop */ {
/* set the value(s) af your parameter variables */
MULTICALL; /* Make the actual call */
} /* end of loop */
POP_MULTICALL; /* Tear down the calling context */
For some concrete examples (you know you want them π
), go look at how the first() and reduce() functions of List::Util 1.18 are actually implemented. And while you're in there, you'll also find a header file that emulates the multicall API on older versions of perl. very much ate. π
π SEE ALSO
perlxsperlgutsperlembed
go read them, i'll wait πΈ
π AUTHOR
This whole thing was written by Paul Marquess. π
And special thanks to the absolute icons who helped bring this document into existence:
- Jeff Okamoto
- Tim Bunce
- Nick Gianniotis
- Steve Kelem
- Gurusamy Sarathy
- Larry Wall
legends, all of them, fr fr πβ¨
π DATE
Last updated for perl 5.23.1.
and that's a wrap bestie!! π¦ this little masterpiece is from perl v5.44.0, dated 2026-10-17, and it lives in the man pages as PERLCALL(1perl). go forth and call your subs, you gorgeous genius π
πβ
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms