Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

system call returns incorrect in windows

by ericaQA (Novice)
on Apr 06, 2018 at 17:58 UTC ( [id://1212447]=perlquestion: print w/replies, xml ) Need Help??

ericaQA has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm working on a multi platform test script that needs to be able to run on windows, and system() doesn't seem to be correctly passing the return value from ant. To demonstrate I've made this simple script. If it's run from within a directory that contains build.xml it will pass. If not it will fail. In both cases perl is claiming the return value to be 0. But if I just run "ant clean" directly from the command line then check the return code in the windows system var it isn't 0 in the failing case. It is 1. Oddly I've tried the same perl code with windows commands like DIR in known passing and known failing ways and in that case the system() call is returning the error code faithfully. I'm not sure in this case whether it's an ant issue or a perl issue. Might there be any other way to do this that would be more reliable?

#!/usr/bin/perl -w use warnings; use strict; use POSIX; my $command = "ant clean"; my $return = system ($command); print "RETURN VALUE: $return\n"; print "CHILD_ERROR_NATIVE: ${^CHILD_ERROR_NATIVE}\n";
####OUTPUT###### #PASSING CASE c:\BUILD_DIRECTORY>perl test.pl Buildfile: build.xml Trying to override old definition of task javac clean: BUILD SUCCESSFUL Total time: 0 seconds RETURN VALUE: 0 CHILD_ERROR_NATIVE: 0 c:\BUILD_DIRECTORY>ant clean Buildfile: build.xml Trying to override old definition of task javac clean: BUILD SUCCESSFUL Total time: 0 seconds c:\BUILD_DIRECTORY>echo %ERRORLEVEL% 0 <------------------------------- # FAILING CASE c:\NOT_BUILD_DIRECTORY>perl jet\test.pl Buildfile: build.xml does not exist! Build failed RETURN VALUE: 0 CHILD_ERROR_NATIVE: 0 c:\NOT_BUILD_DIRECTORY>ant clean Buildfile: build.xml does not exist! Build failed c:\NOT_BUILD_DIRECTORY>echo %ERRORLEVEL% 1 <-------------------------

Replies are listed 'Best First'.
Re: system call returns incorrect in windows
by Lotus1 (Vicar) on Apr 07, 2018 at 01:49 UTC

    From the Windows command prompt you can find out which program is being run from a command by using the 'where' command.

    C:\Users\Lotus1>where perl C:\Strawberry\perl\bin\perl.exe C:\Users\Lotus1>where test1 C:\Users\Lotus1\test1.bat

    I found ant.bat at https://github.com/apache/ant/blob/master/src/script/ant.bat. I noticed this interesting comment at line 188:

    :end rem bug ID 32069: resetting an undefined env variable changes the erro +rlevel. if not "%_JAVACMD%"=="" set _JAVACMD= if not "%_ANT_CMD_LINE_ARGS%"=="" set ANT_CMD_LINE_ARGS= if "%ANT_ERROR%"=="0" goto mainEnd goto omega

    The issue you are seeing is not a Perl bug but something that ant.bat is doing to the ERRORLEVEL. I don't know if the bug mentioned above is the source of the problem. Looking at bat files is too annoying so I'm giving up on it. Good luck.

    Update: I found a bugreport for this issue with ant.bat. https://bz.apache.org/bugzilla/show_bug.cgi?id=41039 Which version of ant.bat are you using?

Re: system call returns incorrect in windows
by ikegami (Patriarch) on Apr 06, 2018 at 18:15 UTC

    That should work on Windows.

    >perl -E"say system('perl -e exit($ARGV[0]) 0'); say $?; say ${^CHILD_ +ERROR_NATIVE}" 0 0 0 >perl -E"say system('perl -e exit($ARGV[0]) 1'); say $?; say ${^CHILD_ +ERROR_NATIVE}" 256 256 256

    Please provide more info (e.g. perl -V).

    What's ant's file extension (e.g. .exe)? (Not sure if that matters.)

      Hi, yes ant in this case would be ant.exe

      E:\>perl -v This is perl, v5.10.1 built for MSWin32-x86-multi-thread

      So not the very latest but not ancient. Latest from ActiveState is 5.26.1.2601. I'll see if I can upgrade and notice a difference

      Edit: I'm getting the same results after upgrading to the latest version, weird that

Re: system call returns incorrect in windows
by Anonymous Monk on Apr 06, 2018 at 21:15 UTC
    Do you get different output with system 'fullpathto.exe','arg';
Re: system call returns incorrect in windows
by Anonymous Monk on Apr 06, 2018 at 21:16 UTC
    Is ant a batch file or exe?

      My bad, I said above that ant was exe, but it is in fact, .bat, or .cmd. However the search order in windows is PATHEXT=.COM;.EXE;.BAT;.CMD...

      I tried providing the full path to ant.bat and am seeing the same type of results. The cmd version apparently has bugs and won't run at all.

      I checked out the ant.bat file and it appears to be setting the err variable in the file in the event of an error. In any event, when executing it directly from the command prompt it does leave %ERRORLEVEL% set to 1. Not sure if that is what system() consults or not?

        Hi,

        ERRORLEVEL merely reflects errno, and that can change each time you execute a command

        I think the problem is the /b in  exit /b %ANT_ERROR%

        here is why

        $ perl -le" print system 'errorexit1.bat' " $ exit 1 256 $ perl -le" print system 'errorexitb1.bat' " $ exit /b 1 0 $

        This tells me you are probably supposed to system antRun.bat or https://github.com/apache/ant/blob/master/src/script/antRun.pl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1212447]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-19 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found