gpt4 book ai didi

python - 在 Perl 脚本上使用另一个解释器运行代码

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:36 25 4
gpt4 key购买 nike

This thread讨论了一种从 Bash 脚本中运行 Python 代码的方法。

有没有什么方法可以在 Perl 脚本中做类似的事情?即有什么方法可以运行在 Perl 脚本上键入的 Python 代码?请注意,我不是在询问从 Perl 脚本运行 Python 文件。我问的是如何运行直接在具有 Perl 脚本的同一文件中键入的 Python 代码(与另一个线程讨论如何从 Bash 脚本运行 Perl 代码的方式相同)。

例子:

# /bin/perl
use 5.010
my $some_perl_variable = 'hello';


# ... BEGIN PYTHON BLOCK ...
# We are still in the same file. But we are now running Python code
import sys;
print some_perl_variable # Notice that this is a perl variable
for r in range(3):
print r
# ... END PYTHON BLOCK ...

say "We are done with the Perl script!"
say "The output of the Python block is:"
print $output"
1;

应该打印:

We are done with the Perl script! 
The output of the Python block is:
hello
1
2
3

我们完成了 perl 脚本

最佳答案

听起来您会对 Inline 感兴趣模块。它允许 Perl 调用许多其他语言的代码,并依赖于每种语言的支持模块。

你没有说你想做什么,但是你提到了 Python 并且有一个 Inline::Python .

关于python - 在 Perl 脚本上使用另一个解释器运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16310370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com