in reply to access to ERRORLEVEL on Win32

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.

Replies are listed 'Best First'.
Re^2: access to ERRORLEVEL on Win32
by gkraus (Novice) on Jun 29, 2009 at 13:14 UTC
    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.