It's said, that

The open2() and open3() functions are unlikely to work anywhere except on a Unix system, or at least one purporting POSIX compliance.

There are, however, examples of the opposite, i.e. open2 can work on Windows.

I want my Perl script to dynamically create Postscript code, invoke the Ghostscript, feed it, collect the output for further tasks. Without any IPC:

use strict; use warnings; my $in_fn = 'in.ps'; my $out_fn = 'out.pdf'; open my $fh, '>', $in_fn; print $fh <<'END'; %!PS % % skipped % showpage END close $fh; system 'gswin32c', '-q', '-sDEVICE=pdfwrite', '-o', $out_fn, $in_fn; # now do something useful with out.pdf

Ghostscript can use pipes, therefore I thought about more efficient implementation. Below, for simplicity, GS doesn't produce any output except error messages. E.g., using IPC::Run3:

use strict; use warnings; use IPC::Run3; run3 [ 'gswin32c', '-q', '-sDEVICE=nullpage', '-' ], \ 'syntax error!', \ my $out, \ my $err, { binmode_stdin => 1, binmode_stdout => 1, }; print "*** STDOUT ***\n$out\n"; print "*** STDERR ***\n$err\n";

It works, but module's documentation says about nearly always creating temporary files. Therefore it's all nothing but syntactic sugar compared to no-IPC solution above.

The IPC::Open2, however, won't work, it just hangs indefinitely (and so does GS process):

use strict; use warnings; use IPC::Open2; my ( $in, $out, ); my $pid = open2 $out, $in, 'gswin32c -q -sDEVICE=nullpage -'; $in-> autoflush( 1 ); print $in "syntax error!\n\cZ\n"; close $in; print do { local $/; <$out> }; close $out; waitpid( $pid, 0 );

Autoflushing, and ctrl-Z were my futile attempts to make it work.

I understand, efficiency gain of in-memory buffers vs disk-IO can be negligible. But, if not for this case, but for the future -- are there clear advice about when IPC::Open\d will work on Windows, and when not to waste time trying?


In reply to When does IPC::Open\d work on Windows, and when doesn't? by vr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.