in reply to Perl & Cshell
It's worth pointing out that setting environment variables in a Perl script, however, will not change your shell's environment variables. That's because you are running Perl in a subprocess, which has its own environment variable list; as soon as the Perl script exits, you're back with the environment variables that the parent had originally.
To test this is as easy as running the following script:
When you run this, you should see def printed. But when you're back to your shell/prompt, (assuming variable abc wasn't defined in the first place):#!/usr/bin/perl -w use strict; use warnings; $ENV{'abc'} = 'def'; system('echo $abc');
That's because:% echo $abc abc: Undefined variable.
So, when you've set the environment variable abc in the Perl subprocess, it is only altering the copy of the environment list inherited from the parent, not the parent's environment.[Parent process] ======> [Perl subprocess] ====> [Sub process shell] Env list #1, Env list #2 Env list #3 "abc" NOT defined copied from the copied from the Parent process, Perl subprocesses, define "abc" here "abc" still defined
Moral of the story is: there is no way that you can ever set an environment variable in a subprocess and have it "persist" to the parent process.
Update: And the same thing is true, of course, for the process' notion of current directory.
|
|---|