Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Inelegant in the extreme, but I dug out some old Windows-only code that I used years ago on an ancient Perl that still seems to work with latest Strawberry Perl.

use strict; use warnings; sub read_file_contents { my $fname = shift; open( my $fh, '<', $fname ) or die "error: open '$fname': $!\n"; local $/ = undef; # slurp mode my $s = <$fh>; close($fh); return $s; } # Run a Windows executable synchronously. # Return a three element list: # the return code; the stdout of the command; and the stderr of the co +mmand. # Die if something goes wrong. sub run_cmd_sync { my ( $exe, $cmd, $workdir ) = @_; defined($workdir) or $workdir = "."; require Win32::Process; my $tmpout = "klink-out-$$.tmp"; my $tmperr = "klink-err-$$.tmp"; -f $exe or die "error: file '$exe' not found"; local *SAVOUT; local *SAVERR; # save original stdout and stderr open( SAVOUT, ">&STDOUT" ) or die "error: open SAVOUT: $!"; open( SAVERR, ">&STDERR" ) or die "error: open SAVERR: $!"; open( STDOUT, '>', $tmpout ) or die "error: can't redirect stdout"; open( STDERR, '>', $tmperr ) or die "error: can't redirect stderr"; Win32::Process::Create( my $hProc, # process object $exe, # executable $cmd, # command line 1, # inherit handles Win32::Process::NORMAL_PRIORITY_CLASS(), $workdir # working dir ) or die "error: Win32::Process::Create: $^E ($!)"; my $pid = $hProc->GetProcessID(); # parent continues (redirect back to original) ... close(STDOUT); close(STDERR); open( STDOUT, ">&SAVOUT" ) or die "error: open SAVOUT: $!"; open( STDERR, ">&SAVERR" ) or die "error: open SAVERR: $!"; print "started exe:$exe (cmd:$cmd) ok, pid=$pid.\n"; my $rc = 0; $hProc->Wait( Win32::Process::INFINITE() ) or die "error: Wait: $^E + ($!)"; $hProc->GetExitCode($rc) or die "error: GetExitCode: $^E ($!)"; my $outstr = read_file_contents($tmpout); my $errstr = read_file_contents($tmperr); unlink($tmpout) or die "error: unlink '$tmpout': $!\n"; unlink($tmperr) or die "error: unlink '$tmperr': $!\n"; return ( $rc, $outstr, $errstr ); } my ( $rc, $outstr, $errstr ) = run_cmd_sync( $^X, 'perl -e "print q{hello stdout}; print STDERR q{hello stderr}"', '.' ); print "rc='$rc'\n"; print "stdout='$outstr'\n"; print "stderr='$errstr'\n";
Running the above program produces:
started exe:C:\Strawberry\perl\bin\perl.exe (cmd:perl -e "print q{hell +o stdout}; print STDERR q{hello stderr}") ok, pid=3132. rc='0' stdout='hello stdout' stderr='hello stderr'

In case it's of use, running this simpler and more portable test program tt1.pl works from the Windows shell at least, but will probably hit the same problems you are currently suffering if run without a shell.

# Test program tt1.pl use strict; use warnings; # Run a command without invoking the command shell. # exe is the command name # @_ contains the command line arguments (including argv[0]) sub run_cmd_noshell { my $exe = shift; print "run '$exe' with args:\n '@_'\n"; system { $exe } @_; my $rc = $? >> 8; $rc == 0 or warn "error: exit code=$rc\n"; } run_cmd_noshell($^X, $^X, '-le', 'print q{hello one};'); run_cmd_noshell($^X, 'perl', '-le', 'print q{hello two}; exit 42;');
produces the following output:
run 'C:\Strawberry\perl\bin\perl.exe' with args: 'C:\Strawberry\perl\bin\perl.exe -le print q{hello one};' hello one run 'C:\Strawberry\perl\bin\perl.exe' with args: 'perl -le print q{hello two}; exit 42;' hello two error: exit code=42


In reply to Re: Reading output of external program without Shell by eyepopslikeamosquito
in thread Reading output of external program without Shell by Anonymous Monk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found