Re: How to manipulate environment variables in parent process?



On 11/12/06, siegfried <siegfried@xxxxxxxxxxx> 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!

Relevant Pages

  • Re: AGAIN: how can I install modules if Im not root?
    ... an environment variable for setting @INC which behaves as -Mlib ... during Perl configure. ... [Oh - and I never install manpages. ... OK - that situation mya not obtain in the environment you are thinking of, but it *can* obtain - *did* ...
    (comp.lang.perl.moderated)
  • Re: DBD::Informix -- Error Installing On Mac OSX
    ... And don't install it in the system locations. ... Has anyone successfully built a 64 bit perl on OSX? ... the environment correctly, as long as the module reasonably clearly ... Yes, you need a working Informix environment, including a stores ...
    (comp.databases.informix)
  • Re: PATCH [0/3]: Simplify the kernel build by removing perl.
    ... If we only use 0.5% of a certain language ... Dependencies very quickly become dependency hell. ... - Cross-compiling perl is hard. ... #2 is another byproduct of your environment and generally a non-issue. ...
    (Linux-Kernel)
  • Re: System call
    ... And it is what OP got - his bat file was executed by CMD and he got the .bat ... But it has nothing to do with PHP itself. ... and security considerations mean this would NOT be possible. ... I'm talking about the system environment. ...
    (comp.lang.php)
  • Re: System call
    ... And it is what OP got - his bat file was executed by CMD and he got the .bat ... But it has nothing to do with PHP itself. ... and security considerations mean this would NOT be possible. ... I'm talking about the system environment. ...
    (comp.lang.php)