On 11/12/06, siegfried <[EMAIL PROTECTED]> wrote:
Is it possible to write a perl script to manipulate the environment
variables in a windows CMD.EXE shell?



Normally, you can manipulate environment variables, but they only have
effect for sub processes. I need to change the values of environment
variables for the parent process.



I have a windows bat file and everyday I have to edit it to contain the
current date (because I have a batch job that creates a new directory with
the current date in the directory name).



Can perl help?



I think the best I could hope for would be to write a perl script that
generated a bat file and then I manually execute the bat file. I don't think
there is anyway to automate the execution of the bat file. Please tell me
I'm wrong!


perldoc -f exec
perldoc -f system

There may be quirks on Windows, but AFAIK one or the other of those
built-in functions should do what you need.

system() executes a system command. That means that you can can
execute the .bat file directly from within perl. Use Perl to make
whatever modifications to the file you need, and then
'system("yourfile.bat");'. The bat file will be executed as a child of
the Perl script, and inherit its environment.

Alternatively, you could use exec(). exec completely replaces the
parent process with the exec'd process instead of forking it as a
child. Just do whatever you need with Perl, and then as the last line
'exec("yourfile.bat")' Your bat file will inherit everything from
perl, including its environment and PID. This is a pretty common trick
for setting up an environment. In fact, it's the only use of exec I
know of. Start perl, set a bunch of things in %ENV, and exec, or
possibly fork and exec. You can even do something like
'exec("CMD.EXE")' to get a new interactive shell with the modified
environment.

Better yet, take Matt's advice and just rewrite the bat file in Perl.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to