如何从 Perl 将输入传送到 Java 命令?
我需要通过 Java 程序运行一个字符串,然后检索输出。 Java 程序通过标准输入接受字符串。以下内容有效:
my $output = `echo $string | java -jar java_program.jar`;
有一个问题:$string
可以是任何东西。对于解决这个问题有什么好的想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议您查看
IPC::Run3
模块。它使用非常简单的界面,并允许获取STDERR
和STDOUT
。这是一个小例子:请参阅
IPC::Run3
与其他模块的比较。I suggest you to look at
IPC::Run3
module. It uses very simple interface and allow to getSTDERR
andSTDOUT
. Here is small example:See
IPC::Run3
comparation with other modules.如果您可以使用 CPAN 模块(我假设大多数人都可以),请查看 Ivan 关于使用 的回答 IPC::Run3。它应该处理你需要的一切。
如果您无法使用模块,请按照以下方法以简单的方式执行操作。
您可以使用管道来进行输入,它将避免所有这些命令行引用问题:
不过,看起来您实际上需要命令的输出。您可以使用类似 IPC::Open2 (往返于 java 进程),但是尝试同时处理两个管道可能会让自己陷入死锁。
您可以通过将 java 输出到文件,然后从该文件读取来避免这种情况:
另一种选择是以相反的方式执行操作。将 $string 放入临时文件中,然后将其用作 java 的输入:
请注意,这不是处理临时文件的最佳方法;为了简单起见,我刚刚使用了
output.txt
和input.txt
。查看 File::Temp 文档 各种更干净的方法来更干净地创建临时文件。If you can use CPAN modules (and I'm assuming most people can), look at Ivan's answer on using IPC::Run3. It should handle everything you need.
If you can't use modules, here's how to do things the plain vanilla way.
You can use a pipe to do your input, and it will avoid all those command line quoting issues:
It looks like you actually need the output of the command, though. You could open two pipes with something like IPC::Open2 (to and from the java process) but you risk putting yourself in deadlock trying to deal with both pipes at the same time.
You can avoid that by having java output to a file, then reading from that file:
The other option is to do things the other way around. Put $string in a temporary file, then use it as input to java:
Note that this isn't the greatest way to do temporary files; I've just used
output.txt
andinput.txt
for simplicity. Look at the File::Temp docs for various cleaner ways to create temporary files more cleanly.您是否研究过
IPC::Run
?您可能正在寻找与此类似的语法:
Have you looked into
IPC::Run
?Syntax similar to this might be what you are looking for:
创建一个管道,就像您的 shell 一样。
这是我们可怕的字符串:
我们将向后构建管道,因此首先我们收集 Java 程序的输出:
注意 Perl 的 参数perldoc.perl.org/functions/open.html" rel="nofollow noreferrer">
open
运算符。unpack
用于查看从管道读取的数据的内容。在您的程序中,您需要运行 Java 程序,但下面的代码使用了合理的传真:
最后,不起眼的孙子只是打印可怕的字符串(到达 Java 程序的标准输入)并退出。将
$|
设置为 true 值会刷新当前选定的文件句柄并将其置于无缓冲模式。其输出:
请注意,NUL 在行程中幸存下来。
Create a pipeline just like your shell would.
Here's our scary string:
We'll build our pipeline backwards, so first we gather the output from the Java program:
Note the special
"-|"
argument to Perl'sopen
operator.The
unpack
is there to peek into the contents of the data read from the pipe.In your program, you'll want to run the Java program, but the code below uses a reasonable facsimile:
Finally, the humble grandchild simply prints the scary string (which arrives on the standard input of the Java program) and exits. Setting
$|
to a true value flushes the currently selected filehandle and puts it in unbuffered mode.Its output:
Note that the NUL survives the trip.
内置 IPC::Open2 模块提供了无需外部文件即可处理双向管道的功能。
The builtin IPC::Open2 module provides a function to handle bidirectional-piping without an external file.