perl & errorlevel

is there a way to import back into perl  
dos errorlevel after executing
system(<some DOS command>);
from a perl program
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

> 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

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.
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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");
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.