This thread討論了從Bash腳本內運行Python代碼的方法。在Perl腳本上使用另一個解釋器運行代碼
有什麼辦法可以在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腳本
是的,同樣的技術(heredocs)適用。另請參閱所謂「單線」的「-e」和「-E」選項。 – amon 2013-04-30 23:09:34
如果你描述你想要做什麼,這將有所幫助。你想從Perl腳本中運行Python代碼嗎?這是我寫的最好的猜測。 – Borodin 2013-04-30 23:26:39
@Borodin我更新了OP。讓我知道如果這仍然不清楚。 – 2013-04-30 23:43:42