C 程序如何调用 Perl 函数?
我正在使用 call_pv
从我的 C 程序调用 perl 子例程。 我有 2 个问题:
C 程序如何找到在哪个 Perl 文件中定义了该子例程?有什么地方可以定义Perl文件名吗?
如果 Perl 返回一个哈希引用作为输出,我如何在 C 中读取它?
这是我的 C 函数:
static int call_perl_fn(char* image)
{
dSP;
int count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(image, 0))); //parameter to perl subroutine
PUTBACK;
count = call_pv("ImageInfo", G_SCALAR); //Invoking ImageInfo subroutine
SPAGAIN;
if (count != 1)
{
printf("ERROR in call_pv");
}
printf("VALUE:%s", (char*)(SvRV(POPp))); //How to read has reference output?
PUTBACK;
FREETMPS;
LEAVE;
return count;
}
I am invoking a perl subroutine from my C program using call_pv
.
I have 2 questions:
How will the C program find in which Perl file this subroutine is defined? Is there any place we can define the Perl file name?
If the Perl returns a hash reference as output, how can I read it in C?
This is my C function:
static int call_perl_fn(char* image)
{
dSP;
int count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(image, 0))); //parameter to perl subroutine
PUTBACK;
count = call_pv("ImageInfo", G_SCALAR); //Invoking ImageInfo subroutine
SPAGAIN;
if (count != 1)
{
printf("ERROR in call_pv");
}
printf("VALUE:%s", (char*)(SvRV(POPp))); //How to read has reference output?
PUTBACK;
FREETMPS;
LEAVE;
return count;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 argv[1] 来放置文件名,因此在 perl_run(my_perl_interpreter); 之前执行如下操作:
关于返回值,您应该使用 POPs 来代替 POPp得到一个SV值,然后用SvTYPE()检查它以确定类型,并进行相应的处理。
查看 http://perldoc.perl.org/perlembed.html 和 http://perldoc.perl.org/perlcall.html
use
argv[1]
to put the filename, so before ofperl_run(my_perl_interpreter);
do something like this:About the return value, instead of POPp you should use POPs to get an SV value, then inspect it to determine the type, with SvTYPE(), and do the correspondent processing.
Take a look at http://perldoc.perl.org/perlembed.html and http://perldoc.perl.org/perlcall.html
1)
call_pv
在文件中找不到子内容,就像 Perl 中的ImageInfo($image)
一样。您需要像往常一样创建潜艇。2)参考什么?例如,对字符串的引用:
对哈希的引用更像是:
perlapi
1)
call_pv
doesn't find subs in files any more thanImageInfo($image)
in Perl does. You need to create the subs as always.2) A reference to what? For example, a reference to a string:
A reference to a hash would be more like:
perlapi