如何从Perl调用python脚本?

时间:2022-12-19 20:28:53

I need to call "/usr/bin/pdf2txt.py" with few arguments from my Perl script. How should i do this ?

我需要使用Perl脚本中的少量参数调用“/usr/bin/pdf2txt.py”。我该怎么做?

3 个解决方案

#1


14  

If you need to capture STDOUT:

如果你需要捕获STDOUT:

my $ret = `/usr/bin/pdf2txt.py arg1 arg2`;

You can easily capture STDERR redirecting it to STDOUT:

您可以轻松捕获STDERR将其重定向到STDOUT:

my $ret = `/usr/bin/pdf2txt.py arg1 arg2 2>&1`;

If you need to capture the exit status, then you can use:

如果您需要捕获退出状态,那么您可以使用:

my $ret = system("/usr/bin/pdf2txt.py arg1 arg2");

Take in mind that both `` and system() block until the program finishes execution.

请记住,``和system()都会阻塞,直到程序完成执行。

If you don't want to wait, or you need to capture both STDOUT/STDERR and exit status, then you should use IPC::Open3.

如果您不想等待,或者您需要同时捕获STDOUT / STDERR和退出状态,那么您应该使用IPC :: Open3。

#2


8  

my $output = `/usr/bin/pdf2txt.py arg1 arg2`;

#3


2  

If you don't need the script output, but you want the return code, use system():

如果您不需要脚本输出,但需要返回代码,请使用system():

...
my $bin = "/usr/bin/pdf2txt.py";
my @args = qw(arg1 arg2 arg3);
my $cmd = "$bin ".join(" ", @args);

system ($cmd) == 0 or die "command was unable to run to completion:\n$cmd\n";

#1


14  

If you need to capture STDOUT:

如果你需要捕获STDOUT:

my $ret = `/usr/bin/pdf2txt.py arg1 arg2`;

You can easily capture STDERR redirecting it to STDOUT:

您可以轻松捕获STDERR将其重定向到STDOUT:

my $ret = `/usr/bin/pdf2txt.py arg1 arg2 2>&1`;

If you need to capture the exit status, then you can use:

如果您需要捕获退出状态,那么您可以使用:

my $ret = system("/usr/bin/pdf2txt.py arg1 arg2");

Take in mind that both `` and system() block until the program finishes execution.

请记住,``和system()都会阻塞,直到程序完成执行。

If you don't want to wait, or you need to capture both STDOUT/STDERR and exit status, then you should use IPC::Open3.

如果您不想等待,或者您需要同时捕获STDOUT / STDERR和退出状态,那么您应该使用IPC :: Open3。

#2


8  

my $output = `/usr/bin/pdf2txt.py arg1 arg2`;

#3


2  

If you don't need the script output, but you want the return code, use system():

如果您不需要脚本输出,但需要返回代码,请使用system():

...
my $bin = "/usr/bin/pdf2txt.py";
my @args = qw(arg1 arg2 arg3);
my $cmd = "$bin ".join(" ", @args);

system ($cmd) == 0 or die "command was unable to run to completion:\n$cmd\n";