perl & errorlevel

Posted on 1997-09-04
Last Modified: 2008-03-04
is there a way to import back into perl  
dos errorlevel after executing
system(<some DOS command>);
from a perl program
Question by:weinerk

Expert Comment

ID: 1206006
> perldoc -f system

=item system LIST

Does exactly the same thing as "exec LIST" except that a fork is done first, and the parent process waits for the child process to complete. Note that argument processing varies depending on the number of arguments. The return value is the exit status of the program as returned by the wait() call. To get the actual exit value divide by 256. See also L</exec>. This is I<NOT> what you want to use to capture the output from a command, for that you should use merely backticks or qx//, as described in L<perlop/"`STRING`">.

I suspect that it works the same on all platforms, meaning that the DOS error level should be returned, multiplied by 256


Expert Comment

ID: 1206007
I think mgjv is on the right track.  In UNIX I would just try something like...

$ouput = system("some","command");

Then the output from some command is returned and stored in $output.  Try this on your PC.  Good luck.

Accepted Solution

dmethvin earned 100 total points
ID: 1206008
The problem is that COMMAND.COM (the shell) doesn't pass back the exit status of the last command. Also, some builtin commands like DIR don't set (change) errorlevel at all. The only way I could figure out to get the status is to use a batch file as a helper that checks errorlevel and either sends it back as the output or writes it to a temp file for later inspection. Here's an example...send the children out of the room:


REM The command to be tested. Find will return 1 if the string
REM isn't found, 2 if it is.  So to test, call HELPER using
REM HELPER.BAT as the arg and then some other file that
REM doesn't have "nauseum" in it.

find "nauseum" %1

REM Check errorlevel. This must be done in descending order
REM because errorlevel implies greater-than-or-equal. I only
REM care about three returns here so I didn't do all 255 codes.
REM If the commands you run don't have output it would
REM probably be easier to echo the exit status directly to
REM stdout and capure it in Perl with backticks.

if errorlevel 255 goto ERR255
if errorlevel 2   goto ERR2
if errorlevel 1   goto ERR1
rem no error
echo 0 >err.dat
goto DONE
echo 255 >err.dat
goto DONE
REM Ad nauseum here
echo 2 >err.dat
goto DONE
echo 1 >err.dat

REM The errorlevel is now in ERR.DAT, the Perl program
REM can read that and do something.

LVL 84

Expert Comment

ID: 1206009
If the problem is that the shell isn't passing back the exit status,
might you be able to bypass COMMAND.COM the same way you can bypass /bin/sh by calling
          $ouput = system('command', $arg1, $arg2);
instead of
          $ouput = system("command $arg1 $arg2");

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question