Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling Perl Subroutines from C/C++

Status
Not open for further replies.

JJ1

Programmer
Apr 19, 2001
104
GB
Hi All,

I am trying to call perl subroutines from within C (as documented in perlembed/perlcall), but I'm not having much luck. If anyone has knowledge/experience in doing this, I would really appreciate your help:

1. I have tried a (very simple) test to call a perl function (hi::say_hi) which is installed as a perl module on my system. Unfortunately, I know I've misunderstood something and I just can't get it to run even after RTFM. Could someone point out what I've done wrong please?

*****C Function - test.exe*****

#include <stdio.h>
#include <EXTERN.h>
#include <perl.h>

static PerlInterpreter *my_perl;

int main( void )
{
printf( &quot;This program invokes a Perl Script\n&quot; );

dSP;
PUSHMARK(SP);
//Perl Call
call_pv(&quot;hi::say_hi&quot;, G_DISCARD | G_NOARGS);

return 0;
}


*****Perl Function*****

package hi;

use strict;

...

#Function to be called from C
sub say_hi {
print &quot;Hello from Perl&quot;;
}


As for the IDE, I'm using VisualStudio.NET and have included perl56.lib as per the Perlembed instructions. The C compiles correctly, but when running, I see the following error as soon as the &quot;dSP;&quot; macro is run:

&quot;An unhandled exception of type 'System.NullReferenceException' occurred in test.exe
Additional information: Object reference not set to an instance of an object.&quot;

What am I doing wrong? Please help!

2. To call a perl sub, does the sub have to be installed as a package or can it be a simple script file (eg .pl file) ?

3. I don't know much about PerlXS. Do I need to create an XSUB interface?

Many thanks to anyone who replies.
Regards,

James.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top