Easy Embedding API

We have learned enough (and more) to implement the convenience API introduced in Chapter 19. They are perl_call_va , perl_eval_va, and the set of functions for accessing or modifying scalar values: get_int, set_int, and so on. We’ll implement only perl_call_va in this section. perl_eval_va is a shorter form of this procedure since it doesn’t expect any input parameters (the string to be eval‘d contains all the information). The API functions to modify scalars are simple wrappers over sv_set*, av_store, and hv_store, and are left as an exercise to the reader.[78]

Recall that perl_call_va takes a NULL-terminated list of typed arguments. This list contains both input and output parameters. The following implementation processes the entire list by XPUSH‘ing the input parameters and storing the output parameters in an array of Out_Param structures. Knowing the number of output parameters expected by the caller allows us to specify G_SCALAR, G_ARRAY, or G_DISCARD. The full code is shown in Example 20.3.

Example 20-3. perl_call_va Implementation

#define MAX_PARAMS 20 typedef struct { char type; void *pdata; } Out_Param; /* To remember the "Out" section */ int perl_call_va (char *subname, ...) { char *p = NULL; char *str = NULL; int i = 0; double d = 0; int nret = 0; /* number of return params expected*/ int ii = 0; va_list vl; int out = 0; int result = 0; Out_Param op[MAX_PARAMS]; dSP; /* Standard ... */ ENTER; /* ... Prologue */ SAVETMPS; PUSHMARK(sp); va_start (vl, ...

Get Advanced Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.