Calling Perl from C

Sometimes you’ll need to call a Perl function from your C code. For example, in the Tk graphical environment, Perl functions are called when a window event occurs, such as when a button is clicked.

In this example we’re going to update our code to call the Perl function handler error. Up to now all your C functions have called croak when they encountered an error. Aborting the program for any little problem is a drastic way of handling a problem.

So for a change, you’ll call the Perl function handle_error instead. In C, you can execute any Perl command using the eval_pv function. For example:

eval_pv("main::handle_error('message')", 0);

The arguments to this function are a string, which contains executable Perl code, and ...

Get Perl for C Programmers 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.