0

I am setting env variables using *.csh file to current terminal. When I use system("/bin/tcsh *.csh") in the perl script, the *csh file executing but not setting any env variables to current terminal.

When I use system("/bin/tcsh *.csh") in the perl script, the *csh file executing but not setting any env variables to current terminal.

sub veloce_env_setup_sub {
    printf "\n\n\t -veloce_env_setup  option enabled\n";
    system("/bin/tcsh /proj/I2BZA1/users/ssudi/SCRIPTS/veloce_env/vlab_4p4p0/veloce_setup.csh");
}
  • Expected: env variables should set to current terminal after sourcing *.csh file.

  • Actual results: only prints are comming but not setting env variables to current terminal.

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
user226751
  • 21
  • 2
  • Possible duplicate of [Can I export a variable to the environment from a bash script without sourcing it?](https://stackoverflow.com/questions/16618071/can-i-export-a-variable-to-the-environment-from-a-bash-script-without-sourcing-i) – melpomene Jul 31 '19 at 12:29
  • 1
    See also [Setting an environment variable through a Perl script](https://stackoverflow.com/q/19192682/2173773) – Håkon Hægland Jul 31 '19 at 12:33

5 Answers5

2

perldoc -q environment:

I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?

Unix

In the strictest sense, it can't be done--the script executes as a different process from the shell it was started from. Changes to a process are not reflected in its parent--only in any children created after the change. There is shell magic that may allow you to fake it by eval()ing the script's output in your shell; check out the comp.unix.questions FAQ for details.

In your code the problem appears twice:

  1. system spawns tcsh, which runs a script that sets environment variables. These environment variables only exist within the tcsh process. When system returns (i.e. when tcsh exits), the environment of the child process is gone.

  2. Even if you managed to modify the environment of the perl script (which you can do by assigning to %ENV), that wouldn't affect the parent shell that perl was started from.

Community
  • 1
  • 1
melpomene
  • 84,125
  • 8
  • 85
  • 148
2

This can now be done with Env::Modify.

use Env::Modify qw(:tcsh source);

sub veloce_env_setup_sub {
    printf "\n\n\t -veloce_env_setup  option enabled\n";
    source("/proj/I2BZA1/users/ssudi/SCRIPTS/veloce_env/vlab_4p4p0/veloce_setup.csh");
}
mob
  • 117,087
  • 18
  • 149
  • 283
  • [ssudi@polhyadcvnc ~/COMMON_SCRIPTS_SAM]$ ./main.pl -veloce_env_setup Can't locate Env/Modify.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at ./main.pl line 4. BEGIN failed--compilation aborted at ./main.pl line 4. – user226751 Aug 01 '19 at 04:48
1

The environment of a child process doesn't affect the environment of the parent process. That is, a process that you start doesn't change the environment of the thing that started it.

If you want to set up the environment for a Perl script, you have some options. Which one works best for you depends on what you are trying to do.

  1. Set up the options inside Perl. Instead of using a shell program, do it all in Perl by setting values in the %ENV hash. This works well if you just need it for that program. It's likely that whatever you are doing in tcsh you can do it Perl.

  2. Instead of calling the shell script from Perl, call your Perl program from the shell script. Now the shell script is the parent process and the child process (the Perl program) inherits the parent's environment.

#!tcsh
setenv SOME_VALUE foo
perl my_program
  1. In a child process, you could print the environment and read that from the parent process. You'd parse it and convert it appropriately. This is what the Env::Modify module does, but I wouldn't want that as my first option.
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
brian d foy
  • 129,424
  • 31
  • 207
  • 592
0

You can't access environment variables in a process that have been set by a child process. It's a fundamental property of how processes work.

JGNI
  • 3,933
  • 11
  • 21
0

You can set %ENV{'your_choice'} = 'as you like'; inside Perl.

Sure, it looks like a little bit hartverdrahtet (yt), but it works great again. So the environmental is mental just inner the mind of top instanced script and closed and removed on closing it.

Another way is calling system("set VARIABLE=VALUE"); Here the variable lefts after closing until next reboot.