getting return value of external application on win32

Discussion in 'Perl Misc' started by alfonsobaldaserra, Jul 15, 2009.

  1. 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.
     
    alfonsobaldaserra, Jul 15, 2009
    #1
    1. Advertising

  2. alfonsobaldaserra

    Guest

    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::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
     
    , Jul 15, 2009
    #2
    1. Advertising

  3. >
    > 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.
     
    alfonsobaldaserra, Jul 16, 2009
    #3
  4. alfonsobaldaserra

    linuxlover Guest

    On 16 jul, 13:30, alfonsobaldaserra <>
    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.
     
    linuxlover, Jul 20, 2009
    #4
  5. >
    Code:
    [color=blue]
    > 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"; }
    > 
    [/color]

    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.
     
    alfonsobaldaserra, Jul 22, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Seong-Kook Shin
    Replies:
    1
    Views:
    503
    Richard Bos
    Jun 18, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    848
    Keith Thompson
    Mar 6, 2005
  3. Tim Fröglich

    Getting ID, calling url, search for value, return value

    Tim Fröglich, Jan 7, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    148
    Josh Twist
    Jan 10, 2006
  4. Ferenc Engard
    Replies:
    2
    Views:
    157
  5. Ralph
    Replies:
    2
    Views:
    110
    Tad McClellan
    Aug 9, 2003
Loading...

Share This Page