免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 11293 | 回复: 17
打印 上一主题 下一主题

[C调用Perl] 如何在C中调用Perl,希望大家能给个简单的例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-19 10:21 |只看该作者 |倒序浏览
5可用积分
想在C中调用perl来做些文本的处理, 环境为linux下, 编译器gcc

通过给 c程序不同的参数,来调用perl执行相关的操作(这里不是用C写include perl.h写个编译器。然后c_prog perl.pl,不是这样)
如: c_prog -p   那么 c_prog就会自动调用目前下的print.pl做操作, 或是最好能将print.pl与C一起编译就更好了

网上查了下,讲直接用C 包含perl的2个头文件,然后编译出一个perl解释器,感觉这样不太好,所以请大家指教,该如何做

所以还请大家能帮忙给出个最简单的例子,就是简单的C调用perl输出 Hello ChinaUnix!

谢谢!

最佳答案

查看完整内容

用popen管道吧,不过我只知道用popen管道调用shell读取返回值,应该和调用perl一个道理,下面是简单例子:[mgqw@localhost Desktop]$ ./a.out Output wasinux localhost.localdomain 2.6.27.25-78.2.56.fc9.i686 #1 SMP Thu Jun 18 12:47:50 EDT 2009 i686 i686 i386 GNU/Linux

论坛徽章:
0
2 [报告]
发表于 2010-03-19 10:21 |只看该作者
本帖最后由 mgqw 于 2010-03-19 11:09 编辑

用popen管道吧,不过我只知道用popen管道调用shell读取返回值,应该和调用perl一个道理,下面是简单例子:
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>

  5. int main()
  6. {
  7.     FILE *read_fp;
  8.     char buffer[BUFSIZ + 1];
  9.     int chars_read;
  10.     memset(buffer, '\0', sizeof(buffer));
  11.     read_fp = popen("uname -a", "r");
  12.     if (read_fp != NULL) {
  13.         chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
  14.         if (chars_read > 0) {
  15.             printf("Output was:-\n%s\n", buffer);
  16.         }
  17.         pclose(read_fp);
  18.         exit(EXIT_SUCCESS);
  19.     }
  20.     exit(EXIT_FAILURE);
  21. }
复制代码
[mgqw@localhost Desktop]$ ./a.out
Output was:-
Linux localhost.localdomain 2.6.27.25-78.2.56.fc9.i686 #1 SMP Thu Jun 18 12:47:50 EDT 2009 i686 i686 i386 GNU/Linux

论坛徽章:
0
3 [报告]
发表于 2010-03-19 11:36 |只看该作者
再把里面的shell命令换成从网络上抄来的hello perl脚本(我不会perl-_-!):
myp.pl
  1. #!/usr/bin/perl
  2.    printf "helo,perl!\n"
复制代码
popen1.c
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>

  5. int main()
  6. {
  7.     FILE *read_fp;
  8.     char buffer[BUFSIZ + 1];
  9.     int chars_read;
  10.     memset(buffer, '\0', sizeof(buffer));
  11.     read_fp = popen("./myp.pl", "r");
  12.     if (read_fp != NULL) {
  13.         chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
  14.         if (chars_read > 0) {
  15.             printf("Output was:-\n%s\n", buffer);
  16.         }
  17.         pclose(read_fp);
  18.         exit(EXIT_SUCCESS);
  19.     }
  20.     exit(EXIT_FAILURE);
  21. }
复制代码
下面是命令输出
[mgqw@localhost Desktop]$ ./myp.pl
helo,perl!
[mgqw@localhost Desktop]$ cc popen1.c
[mgqw@localhost Desktop]$ ./a.out
Output was:-
helo,perl!

论坛徽章:
0
4 [报告]
发表于 2010-03-19 11:47 |只看该作者
非常感谢楼上的朋友的指教

但是感觉这样的话,执行那个myp.pl其实还是调用perl的解释器来解析的,而并非C程序本身

论坛徽章:
0
5 [报告]
发表于 2010-03-19 11:49 |只看该作者
你要C语言程序本身来解析的话,就只有找找C语言有没有perl库了。

论坛徽章:
0
6 [报告]
发表于 2010-03-19 11:54 |只看该作者
最简单的应该是直接system调用吧。

论坛徽章:
0
7 [报告]
发表于 2010-03-19 13:14 |只看该作者
恩,有perl库 perl.h 和EXTERN.h

可以 c_pro  perl.pl 这样将 perl.pl中的内容解释执行

但是不知道如何这么做:

就是 c_proc  -r  通过参数来控制执行的功能  而不是 将perl.pl文件作为参数

论坛徽章:
0
8 [报告]
发表于 2010-03-19 13:15 |只看该作者
直接system调用,移植性是不是不太好呢?

论坛徽章:
0
9 [报告]
发表于 2010-03-19 14:05 |只看该作者
直接用system好像取不到输出值,这才是问题。
移植性基本不需要考虑。
要移植程序肯定不只这个地方有依赖。

论坛徽章:
0
10 [报告]
发表于 2010-03-19 14:59 |只看该作者
system调用,失败了返回值不为0。发个例子
  1. /home/ray001/tmp >cat p.c
  2. #include <stdio.h>
  3. int
  4. main(  )
  5. {
  6.         char cmd_str[]="./hello.pl";
  7.         if ( system(cmd_str) )
  8.         {
  9.                 printf ( "调用外部程序失败\n" );
  10.                 exit (1);
  11.         }
  12.         exit (0);
  13. }
  14. /home/ray001/tmp >./p
  15. hello,cu!
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP