Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can use CPAN modules (and I'm assuming most people can), look at <a href="https://stackoverflow.com/questions/1909262/pipe-input-into-command-in-perl/1909470#1909470">Ivan's answer</a> on using <a href="http://search.cpan.org/~rjbs/IPC-Run3-0.043/lib/IPC/Run3.pm" rel="nofollow noreferrer">IPC::Run3</a>. It should handle everything you need.</p> <p>If you can't use modules, here's how to do things the plain vanilla way.</p> <p>You can use a pipe to do your input, and it will avoid all those command line quoting issues:</p> <pre><code>open PIPE, "| java -jar java_program.jar"; print PIPE "$string"; close(PIPE); </code></pre> <p>It looks like you actually need the output of the command, though. You <em>could</em> open two pipes with something like <a href="http://search.cpan.org/~nwclark/perl-5.8.3/lib/IPC/Open2.pm" rel="nofollow noreferrer">IPC::Open2</a> (to and from the java process) but you risk putting yourself in deadlock trying to deal with both pipes at the same time.</p> <p>You can avoid that by having java output to a file, then reading from that file:</p> <pre><code>open PIPE, "| java -jar java_program.jar &gt; output.txt"; print PIPE "$string"; close(PIPE); open OUTPUT, "output.txt"; while (my $line = &lt;OUTPUT&gt;) { # do something with $line } close(OUTPUT); </code></pre> <p>The other option is to do things the other way around. Put $string in a temporary file, then use it as input to java:</p> <pre><code>open INPUT, "input.txt"; print INPUT "$string"; close(INPUT); open OUTPUT, "java -jar java_program.jar &lt; input.txt |"; while (my $line = &lt;OUTPUT&gt;) { # do something with the output } close(OUTPUT); </code></pre> <p>Note that this isn't the greatest way to do temporary files; I've just used <code>output.txt</code> and <code>input.txt</code> for simplicity. Look at the <a href="http://search.cpan.org/~jhi/perl-5.8.0/lib/File/Temp.pm" rel="nofollow noreferrer">File::Temp docs</a> for various cleaner ways to create temporary files more cleanly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload