Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > getting return value of external application on win32

Reply
Thread Tools

getting return value of external application on win32

 
 
alfonsobaldaserra
Guest
Posts: n/a
 
      07-15-2009
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.
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-15-2009
On Wed, 15 Jul 2009 17:16:54 +0100, Ben Morrow <> wrote:

>
>Quoth alfonsobaldaserra <>:
>>
>> 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 /;

> ^^^^
>This means that the command will be run through cmd.exe, which exits
>successfully (despite the command having failed).
>
>> now i need to get the status of executed command
>>
>> if ( $? == 0 ) { print "yay"; }

>
>$? 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:rocess 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), ($? & 12 ? 'with' : 'without';
}
else {
-->>> printf "child exited with value %d\n", $? >> 8;
}

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

-sln
 
Reply With Quote
 
 
 
 
alfonsobaldaserra
Guest
Posts: n/a
 
      07-16-2009
>
> 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.
 
Reply With Quote
 
linuxlover
Guest
Posts: n/a
 
      07-20-2009
On 16 jul, 13:30, alfonsobaldaserra <alfonso.baldase...@gmail.com>
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.
 
Reply With Quote
 
alfonsobaldaserra
Guest
Posts: n/a
 
      07-22-2009
>
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"; }
> 


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WIN32::OLE MSSQL return value Saya Perl Misc 0 12-01-2008 10:16 PM
Getting ID, calling url, search for value, return value Tim Fröglich ASP .Net Web Services 1 01-10-2006 09:18 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
controlling external process' stdin/stdout AND getting its return value Ferenc Engard Ruby 2 02-17-2004 12:14 AM



Advertisments