Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1972
  • Last Modified:

perl & errorlevel

is there a way to import back into perl  
dos errorlevel after executing
system(<some DOS command>);
from a perl program
1 Solution
> 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.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now