embedding perl within perl....

S

suraj

Hello,

i have a strange situation. i am writing a perl extension on win32 with perl
5.6 from activestate.

however, this might be a general situation somebody might get into and hence
hoping some perl gurus might be able to answer me.

1. i have written a XS extention in C++ which provides some functionality. i
wrote a small test perl script and everything works fine.

2. however, now i have to enhance the functionality in the C++ layer of this
extension so that it does some code execution/expression parsing at runtime. i
wanted to use the perl engine to do this since perl is so flexible and
powerful.

3. so i tried to embed perl in the way described in the perl embed man pages
and so now i have a perl extension embedding perl inturn. it compiled ok but
now crashes at random places when i hit that code.

does anybody know if this is possible at all? is there any help about this in
any manpages. i tried couple of them but most of them talk about using perl in
a C/C++ executable. but my situation is not this straight forward. i have perl
using a C++ extension which tries to embed perl in turn.

thanks for any help,
suraj bhide.
 
B

Bart Lateur

suraj said:
1. i have written a XS extention in C++ which provides some functionality. i
wrote a small test perl script and everything works fine.

2. however, now i have to enhance the functionality in the C++ layer of this
extension so that it does some code execution/expression parsing at runtime. i
wanted to use the perl engine to do this since perl is so flexible and
powerful.

3. so i tried to embed perl in the way described in the perl embed man pages
and so now i have a perl extension embedding perl inturn. it compiled ok but
now crashes at random places when i hit that code.

How about using callbacks in perl, to call the primary perl interpreter,
instead of embedding another one? A bit like how HTML::parser, and
XML::parser::Expat work. These modules provide ways to invoke user
supplied subs, but you needn't go that far, and just call subs defined
in your own module.
 
S

suraj

How about using callbacks in perl, to call the primary perl interpreter,
instead of embedding another one? A bit like how HTML::parser, and
XML::parser::Expat work. These modules provide ways to invoke user
supplied subs, but you needn't go that far, and just call subs defined
in your own module.

how do i do this? can this be done from C++? what are the function calls to be
used to get hold of the primary perl interpreter?

thanks,
suraj
 
J

James Richardson

how do i do this? can this be done from C++? what are the function calls to be
used to get hold of the primary perl interpreter?

thanks,
suraj

How to call a static function ( at least used to be! )

static const char *my_function = "My::package::function";
SV* retval;
SV* param1, param2, param3;
int count = 0;

dSP;
PUSHMARK(SP);
XPUSHs ( param1 );
XPUSHs ( param2 );
XPUSHs ( param3 );
PUTBACK;
count = call_pv ( my_function , G_SCALAR );
SPAGAIN;

if ( count != 1 ) {
croak ("Called %s, expected 1 return value, got %d\n",
my_function, count );
}

retval = POPs;
PUTBACK;




Hope thats of use!

James
 
A

Al Tobey

Hello,

i have a strange situation. i am writing a perl extension on win32 with
perl 5.6 from activestate.

however, this might be a general situation somebody might get into and
hence hoping some perl gurus might be able to answer me.

1. i have written a XS extention in C++ which provides some
functionality. i wrote a small test perl script and everything works
fine.

2. however, now i have to enhance the functionality in the C++ layer of
this extension so that it does some code execution/expression parsing at
runtime. i wanted to use the perl engine to do this since perl is so
flexible and powerful.

perldoc -f eval
3. so i tried to embed perl in the way described in the perl embed man
pages and so now i have a perl extension embedding perl inturn. it
compiled ok but now crashes at random places when i hit that code.

This is probably not a good idea. What you probably want is eval() which
can interpret and execute code at runtime. Your C/C++ module could pass
code into the perl interpreter and eval it there, or your perl code can
eval it directly.

file1.pl:
print "bar\n";

file2.pl:
#!/usr/bin/perl
open( X, "<file1.pl" );
$/ = undef;
my $code = <X>;
close( X );
eval( $code );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top