首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Perl Inline::Python模块,如何将代码放入字符串

Perl Inline::Python模块,如何将代码放入字符串
EN

Stack Overflow用户
提问于 2012-08-07 01:54:25
回答 1查看 1.5K关注 0票数 8

我正在学习perl Inline::Python库。在cpan网站的例子中,我们有

代码语言:javascript
运行
复制
   print "9 + 16 = ", add(9, 16), "\n";
   print "9 - 16 = ", subtract(9, 16), "\n";

   use Inline Python => <<'END_OF_PYTHON_CODE';
   def add(x,y): 
      return x + y

   def subtract(x,y):
      return x - y

   END_OF_PYTHON_CODE

可以将python代码放到字符串中,这样我就可以在运行时创建python代码了吗?例如,如下所示:

代码语言:javascript
运行
复制
my $python_code = "
def add(x,y):
   return x + y
";
print $python_code;
use Inline Python => "$python_code";
print "9 + 16 = ", add(9, 16), "\n";

我们有一个在运行时动态创建python函数的项目。我们想要调用这些函数。py_eval()是要走的路吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-07 02:25:36

没有使用Inline::Python的经验,但使用Inline::C you can use the bind function to set code at runtime,所以这可能会起作用:

代码语言:javascript
运行
复制
my $python_code = "
def add(x,y):
   return x + y
";
print $python_code;
Inline->bind( Python => $python_code );
print "9 + 16 = ", add(9, 16), "\n";
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11833382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档