vr has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: When does IPC::Open\d work on Windows, and when doesn't?
by BrowserUk (Patriarch) on Apr 01, 2017 at 13:35 UTC | |
|
Re: When does IPC::Open\d work on Windows, and when doesn't?
by haukex (Archbishop) on Apr 01, 2017 at 12:05 UTC | |
by vr (Curate) on Apr 01, 2017 at 13:16 UTC |