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

Hi monks, I am searching for a way to access the ERRORLEVEL environment variable in Win32. This variable isn't included in %ENV. I need the return code of the last command executed in the shell / batch before my sricpt is started.

Replies are listed 'Best First'.
Re: access to ERRORLEVEL on Win32
by VinsWorldcom (Prior) on Jun 29, 2009 at 12:26 UTC
    I don't think there is a way to access ERRORLEVEL from a Perl script to check what the ERRORLEVEL is *before* starting the Perl script. You can access it after calling a command with 'system', such as:

    # successful command and exit code system ('dir'); print qq(ERR Code = $?\n); # UN-successful command and exit code system ('dri'); print qq(ERR Code = $?\n);
    Note, ERRORLEVEL is a "special" Windows environment variable:

    C:> set /? [...] If Command Extensions are enabled, then there are several dynamic envi +ronment variables that can be expanded but which don't show up in the + list of variables displayed by SET. These variable values are compu +ted dynamically each time the value of the variable is expanded. If t +he user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below: %CD% - expands to the current directory string. %DATE% - expands to current date using same format as DATE command. %TIME% - expands to current time using same format as TIME command. %RANDOM% - expands to a random decimal number between 0 and 32767. %ERRORLEVEL% - expands to the current ERRORLEVEL value %CMDEXTVERSION% - expands to the current Command Processor Extensions +version number. %CMDCMDLINE% - expands to the original command line that invoked the C +ommand Processor.
    You can try to get around this. Are you using pl2bat to get your Perl scripts to run on Windows? Just edit the resulting batch script to include a conditional:

    if %ERRORLEVEL% 2 perl -x -S %0 %*
    Where '2' above is the desired ERRORLEVEL, or use 'if NOT %ERRORLEVLE% ...' to get the opposite effect.

    What launches your Perl script? Is it a batch file? What is the command that's running before it? Perhaps there is a way to include the conditional check for the ERRORLEVEL in that "thing" that launches your Perl script.

      I have a batch file to filter a compiler output like this:
      cmd "/c c:\CCStudio_v3.1\DosRun.bat > nul && make -f project.mak ../Ou +tput/Debug/obj/%1%.obj 2>&1 | perl vcproj_filter.pl && EXIT errorleve +l"
      The return code from the compiler is evaluated by the caller but will be lost due to the call of perl. So I want to read ERRORLEVEL - the return code of last command *before* my script was started - and return it as exit code by the perl script.
Re: access to ERRORLEVEL on Win32
by Marshall (Canon) on Jun 29, 2009 at 12:22 UTC
    You might find this link helpful: http://www.robvanderwoude.com/errorlevel.php
    I'm no expert on Win .bat files, but on WinXP (at least on my machine), ECHO.%ERRORLEVEL% will print last errorlevel. There is some Windows versions specific stuff about this. I think you'll find something helpful in the above link.
Re: access to ERRORLEVEL on Win32
by ramlight (Friar) on Jun 29, 2009 at 13:50 UTC
    How about something simple like:
    >perl -e "exit 5;" >echo %errorlevel% 5 >set lasterrorlevel=%errorlevel% >perl -e "print $ENV{'lasterrorlevel'};" 5

      If you're going to modify the program, you might as well just pass the errorlevel as a parameter.

      >cmd /c exit 5 >echo %ERRORLEVEL% 5 >perl -le"print @ARGV" %ERRORLEVEL% 5
        Thanks for all your advice. Passing %ERRORLEVEL% as parameter is one workaround, but it would break the ease of implementation by using my script as filter. There obviously exits no way accessing %ERRORLEVEL% from perl without passing it as parameter, so things like
        cmd /c "make 2>&1 | perl vcproj_filter.pl"
        must be changed in batch file like
        make > output.txt 2>&1 perl %ERRORLEVEL% output.txt"
        Unfortunatly the following doesn't work:
        cmd /c "make > output.txt 2>&1 && perl %ERRORLEVEL% output.txt"
        It seems if you start more then one process using && or | ERRORLEVEL is not set for each process but only for the last one. That's why using my script in a pipe and reading %ERRORLEVEL% would never work. I finally use a seperate perl script from where I make the call to make via system() and gather the return code from $?. The output is filtered afterwards.

        Thanks for the help!

Re: access to ERRORLEVEL on Win32
by gemoroy (Beadle) on Jun 29, 2009 at 12:13 UTC
    Maybe i missunderstand you, but
    my $cmd = "echo %ERRORLEVEL%"; my $ret = system($cmd); return $ret;
      This won't help because it always return zero:
      c:> perl -e "exit 5" c:> echo %ERRORLEVEL% 5 c:>perl -e "print system('echo %ERRORLEVEL%');" 0 0 c:>