C 程序如何调用 Perl 函数?

发布于 2024-12-18 19:02:45 字数 655 浏览 0 评论 0原文

我正在使用 call_pv 从我的 C 程序调用 perl 子例程。 我有 2 个问题:

  1. C 程序如何找到在哪个 Perl 文件中定义了该子例程?有什么地方可以定义Perl文件名吗?

  2. 如果 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:

  1. 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?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

横笛休吹塞上声 2024-12-25 19:02:45

使用 argv[1] 来放置文件名,因此在 perl_run(my_perl_interpreter); 之前执行如下操作:

char *my_argv[] = { "", "NAME_HERE.pl" };
perl_parse(my_perl_interpreter, NULL, 2, my_argv, (char **)NULL);

关于返回值,您应该使用 POPs 来代替 POPp得到一个SV值,然后用SvTYPE()检查它以确定类型,并进行相应的处理。

查看 http://perldoc.perl.org/perlembed.htmlhttp://perldoc.perl.org/perlcall.html

use argv[1] to put the filename, so before of perl_run(my_perl_interpreter); do something like this:

char *my_argv[] = { "", "NAME_HERE.pl" };
perl_parse(my_perl_interpreter, NULL, 2, my_argv, (char **)NULL);

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

醉南桥 2024-12-25 19:02:45

1) call_pv 在文件中找不到子内容,就像 Perl 中的 ImageInfo($image) 一样。您需要像往常一样创建潜艇。

2)参考什么?例如,对字符串的引用:

SV * rv;
SV * sv;
char * buf;
STRLEN len;

rv = POPs;
if (!SvROK(rv)) {
   ... error ...
}

sv = SvRV(rv);
buf = SvPVutf8(sv, len);  # For text. Use SvPVbyte for strings of bytes.
...

对哈希的引用更像是:

SV * rv;
SV * sv;
HV * hv;

rv = POPs;
if (!SvROK(rv)) {
   ... error ...
}

sv = SvRV(rv);
if (SvTYPE(sv) != SVt_PVHV) {
   ... error ...
}

hv = MUTABLE_HV(sv);
... Use hv_* functions to look into the hash ...

perlapi

1) call_pv doesn't find subs in files any more than ImageInfo($image) in Perl does. You need to create the subs as always.

2) A reference to what? For example, a reference to a string:

SV * rv;
SV * sv;
char * buf;
STRLEN len;

rv = POPs;
if (!SvROK(rv)) {
   ... error ...
}

sv = SvRV(rv);
buf = SvPVutf8(sv, len);  # For text. Use SvPVbyte for strings of bytes.
...

A reference to a hash would be more like:

SV * rv;
SV * sv;
HV * hv;

rv = POPs;
if (!SvROK(rv)) {
   ... error ...
}

sv = SvRV(rv);
if (SvTYPE(sv) != SVt_PVHV) {
   ... error ...
}

hv = MUTABLE_HV(sv);
... Use hv_* functions to look into the hash ...

perlapi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文