如何从Perl脚本中的外部命令收集输出?

时间:2022-10-12 15:34:50

I have a a tool named TET.EXE, product of the PDFlib family, it is used to extract the co-ordinates of a particular text. Using those coordinates in the Perl script we can extract the required text. This is a manual process to run the .EXE and then give the co-ordinates to Perl, so could any one suggest me to make this entire process hands off.

我有一个名为TET.EXE的工具,它是PDFlib系列的产品,用于提取特定文本的坐标。在Perl脚本中使用这些坐标,我们可以提取所需的文本。这是一个手动进程来运行.EXE,然后将坐标给Perl,所以任何人都可以建议我完成整个过程。

What I mean is Perl script itself should run the .EXE and get the the coordinates required and extract the text. What are the commands to be used in linux to run this perl script? Please, I need your suggestions for the following.
Thanks in advance.

我的意思是Perl脚本本身应运行.EXE并获取所需的坐标并提取文本。在linux中使用哪些命令来运行这个perl脚本?请,我需要你的建议如下。提前致谢。

6 个解决方案

#1


If i understand correctly, you want perl to launch an executable and do something with the text printed to stdout.... in that case there are a few options:

如果我理解正确,你希望perl启动一个可执行文件并使用打印到stdout的文本执行某些操作....在这种情况下,有几个选项:

Using backticks:

my $output = `TED.EXE`;

This puts the output of the TED.EXE command in the variable $output, and is most likely sufficient for what you need.

这会将TED.EXE命令的输出放在变量$ output中,并且很可能足以满足您的需要。

using IPC::Open3:

use IPC::Open3;
my($wtr, $rdr, $err);
my $pid = open3($wtr, $rdr, $err,
                'some cmd and args', 'optarg', ...);

This runs your command and associates $wtr, $rdr and $err to the standard input, output and error streams.

这将运行您的命令并将$ wtr,$ rdr和$ err与标准输入,输出和错误流相关联。

There are other ways to do what you want (Expect.pm, Run3, etc), but i believe the above mentioned should be sufficient.

还有其他方法可以做你想要的(Expect.pm,Run3等),但我相信上面提到的应该足够了。

#2


Perl provides many methods for running an external program and gathering its output. Based on looking at tet.exe I would say your best bet is to use the open function and to loop over the output using a regex to find the coordinates:

Perl提供了许多运行外部程序和收集其输出的方法。基于查看tet.exe我会说你最好的选择是使用open函数并使用正则表达式循环输出以找到坐标:

open my $pdftext, "-|", "/path/to/tet.exe", "--text", $pdffile
    or die "could not open $pdffile using tet.exe: $!";

my ($x, $y);
while (my $line = <$pdftext>) {
    last if ($x, $y) = $line =~ /regex that matches the coords/;
}
die "file did not contain coordinates" unless defined $x;

#3


If TET.EXE outputs to the console you can capture that output with

如果TET.EXE输出到控制台,您可以捕获该输出

my $tetOutput = `tet.exe /myoptions`;

If you want to read about it, search for 'perl backtick'

如果您想了解它,请搜索'perl backtick'

#4


I don't understand question but may be:

我不明白问题,但可能是:

my $result = qx{TET.EXE some.pdf some params};

#5


You might also consider another approach: use a Perl library to extract the coordinates.

您可能还会考虑另一种方法:使用Perl库来提取坐标。

#6


The perlipc documentation shows many ways to interact with external processes from Perl.

perlipc文档显示了与Perl的外部进程交互的许多方法。

Many people tell you to use backticks, but you might also check out IPC::System::Simple which provides more robust ways of doing the same thing by handling operating system specific quirks.

许多人告诉你使用反引号,但你也可以查看IPC :: System :: Simple,它通过处理特定于操作系统的怪癖提供更强大的方法来做同样的事情。

#1


If i understand correctly, you want perl to launch an executable and do something with the text printed to stdout.... in that case there are a few options:

如果我理解正确,你希望perl启动一个可执行文件并使用打印到stdout的文本执行某些操作....在这种情况下,有几个选项:

Using backticks:

my $output = `TED.EXE`;

This puts the output of the TED.EXE command in the variable $output, and is most likely sufficient for what you need.

这会将TED.EXE命令的输出放在变量$ output中,并且很可能足以满足您的需要。

using IPC::Open3:

use IPC::Open3;
my($wtr, $rdr, $err);
my $pid = open3($wtr, $rdr, $err,
                'some cmd and args', 'optarg', ...);

This runs your command and associates $wtr, $rdr and $err to the standard input, output and error streams.

这将运行您的命令并将$ wtr,$ rdr和$ err与标准输入,输出和错误流相关联。

There are other ways to do what you want (Expect.pm, Run3, etc), but i believe the above mentioned should be sufficient.

还有其他方法可以做你想要的(Expect.pm,Run3等),但我相信上面提到的应该足够了。

#2


Perl provides many methods for running an external program and gathering its output. Based on looking at tet.exe I would say your best bet is to use the open function and to loop over the output using a regex to find the coordinates:

Perl提供了许多运行外部程序和收集其输出的方法。基于查看tet.exe我会说你最好的选择是使用open函数并使用正则表达式循环输出以找到坐标:

open my $pdftext, "-|", "/path/to/tet.exe", "--text", $pdffile
    or die "could not open $pdffile using tet.exe: $!";

my ($x, $y);
while (my $line = <$pdftext>) {
    last if ($x, $y) = $line =~ /regex that matches the coords/;
}
die "file did not contain coordinates" unless defined $x;

#3


If TET.EXE outputs to the console you can capture that output with

如果TET.EXE输出到控制台,您可以捕获该输出

my $tetOutput = `tet.exe /myoptions`;

If you want to read about it, search for 'perl backtick'

如果您想了解它,请搜索'perl backtick'

#4


I don't understand question but may be:

我不明白问题,但可能是:

my $result = qx{TET.EXE some.pdf some params};

#5


You might also consider another approach: use a Perl library to extract the coordinates.

您可能还会考虑另一种方法:使用Perl库来提取坐标。

#6


The perlipc documentation shows many ways to interact with external processes from Perl.

perlipc文档显示了与Perl的外部进程交互的许多方法。

Many people tell you to use backticks, but you might also check out IPC::System::Simple which provides more robust ways of doing the same thing by handling operating system specific quirks.

许多人告诉你使用反引号,但你也可以查看IPC :: System :: Simple,它通过处理特定于操作系统的怪癖提供更强大的方法来做同样的事情。