|Just another Perl shrine|
Re: accessing an environment variable dynamicallyby BrowserUk (Pope)
|on Jan 01, 2013 at 20:49 UTC||Need Help??|
The errorlevel environment variable is set to the return value from programs and scripts run by the command shell (cmd.exe). It is set by the shell.
When you run a program from within Perl using system, there is often no command shell involved, so that environment variable does not get set.
But, you don't need errorlevel to be set, because when you use system, you can access the return code from the program by inspecting the $? variable:
There are caveats with this mechanism though. Some are universal, and some windows specific.
There is a further caveat with system as currently implemented. If the command or program you are running cannot be found, or if the perl internals decide according to some undocumented syntax inspection that you are using "shell characters" in your command; then the program you are invoking will not be run directly by Perl, but rather a copy of cmd.exe will be run and your command will be passed to it to run.
The problem with this is that the exit code that is returned in $? is then not the exit code from your command, but rather the exit code return by cmd.exe; which in most cases will tell you nothing about the success or failure of your command.
Now, when perl invokes the shell to run your command, it will set the exit from your command into the errorlevel environment variable. But it will be its copy of the environment, and by the time control returns to your perl script, that command shell and its copy of the environment will have been discarded, and the exit code from your command will no longer be accessible anywhere.
The bottom line is that the errorlevel environment variable is never useful to you from with your perl script.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.