getting return value of external application on win32

A

alfonsobaldaserra

hello,

i am calling an external command in perl on win32 as follows

my $app = 'c:\program files\foo\flarp.exe status quux';
my $spam = qx/ $app 2>&1 /;

now i need to get the status of executed command

if ( $? == 0 ) { print "yay"; }

the problem is it always returns 0. since quux is not running, when i
run the same command on cmd.exe i get return value as 3
echo %ERRORLEVEL%
3

i have checked the archives with similar question but no help. i have
also checked system() and qx// documentation but they don't have
anything like this.

is there any other way to do this?

thanks.
 
S

sln

^^^^
This means that the command will be run through cmd.exe, which exits
successfully (despite the command having failed).


$? contains the exit value of the cmd.exe process (since that's the only
thing cmd.exe knows about). Apparently this is always 0.

I would recommend using Win32::process instead. It's a little awkward,
but should let you do the redirection without involving cmd. You will
have to create the pipe and read from it by hand, of course.

Ben

Why wouldn't system work?

Docs:

if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
-->>> printf "child exited with value %d\n", $? >> 8;
}

Or does Perl/system spawn processes on everything except Windowz?

-sln
 
A

alfonsobaldaserra

Why wouldn't system work?

   if ($? == -1) {
        print "failed to execute: $!\n";
    }

thank you for response. i tried with system and $? but it still
returns 0. however it should return 3.

any more ideas?

thanks.
 
L

linuxlover

On 16 jul, 13:30, alfonsobaldaserra <[email protected]>
wrote:
[...]
any more ideas?

I would recommend module IPC::Run3, which does a very portable and
Perlish way to do the redirects:

Code:
use strict;
use IPC::Run3;
my ($stdout, $stderr);
my @app = ('c:/program files/foo/flarp.exe', 'status',  'quux');
run3(\@app, \undef, \$stdout, \$stderr);
if ( $? == 0 ) { print "yay"; }

Note that the external application and its command-line parameters are
in an array, avoiding the detour via cmd.exe and that stdout and
stderr can be captured separately. The \undef connects stdin to /dev/
null (or the Win32 equivalent NUL). Also, pathnames in Win32 Perl may
be written with forward slashes.
 
A

alfonsobaldaserra

Code:
[QUOTE]
use strict;
use IPC::Run3;
my ($stdout, $stderr);
my @app = ('c:/program files/foo/flarp.exe', 'status',  'quux');
run3(\@app, \undef, \$stdout, \$stderr);
if ( $? == 0 ) { print "yay"; }
[/QUOTE]

i tried running this code. for some obscure reasons it was showing
unexpected results when using command as array, it wasn't executing
the program in background at all, so i did
my $app = q[ c:/program files/foo/flarp.exe status quux ];
and it started executing but there came another problem. the return
value ($?) was always 0 now. i tried the same code with other
commands and it was working just not for this particular program.

before i give up i want to thank you for making me aware of
IPC::Run3. plus i learnt that there exists a File::Spec->devnull() to
redirect $stdout and $stderr messages to.

many thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top