Environment Variable in cshell and perl?

John Macdonald john-Z7w/En0MP3xWk0Htik3J/w at public.gmane.org
Fri Jun 4 19:51:25 UTC 2004


On Fri, Jun 04, 2004 at 02:28:57PM -0400, hui xu wrote:
> 
> All,
> 
> Does anybody know how to setup environment variables of cshell by using perl code?
> 
> I use the following line in a perl code
> 
> $ENV{MYPAHT}="/HOME/HUI/PERLCODE/";
> 
> But after perl program is done, I use the following command to check 
> 
> the environment variable:
> 
> echo $MYPATH
> 
> it says $MYPATH is not defined.
> 
> The question is how to setup the $MYPATH by using perl program.
> 
> Thanks in advanced.
> 
> Louie

ENV is copied from a parent process to each child that it
creates, but it does not get copied back when the child
terminates.

You have a couple of choices.

1a. If you need to have the setting in place for a specific
    purpose, then using:

	$ENV{MYPAHT}="/HOME/HUI/PERLCODE/";
	# any additional setting you need...
	system("csh");

    will start up a new child csh process that has the
    env setting in place.  After you've finished the task
    that need these settings, you exit the csh, which
    will return to the perl program.  The perl program
    can continue on with other steps.

1b. If the perl program in 1a has nothing to do after the
    csh task completes, then you can replace the system("csh")
    statement with exec("csh") instead.  The new csh will still
    inherit the modified environment, but when it exits you will
    be back to the parent shel - the perl program will already
    be gone (replaced by the child csh using the exec call).

2. If you really need to change the environment in the parent
    shell, then you can do this:

    in the perl program:

	# ...
	print "setenv MYPANY /HOME/HUI/PERLCODE\n";
	# ... additional setenv command as need ...
	exit;

    and in your parent csh:

	eval `perl-program`

    (Hope I've got the setenv syntax right, it's been more than
    15 years since I last used csh in any significant way.)

-- 
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list