Setting an environment variable through a Perl script

I am trying to set the environment variable LD_LIBRARY_PATH through a Perl script as follows:

I created .profile under /root

.profile has an export command to say:

 export LD_LIBRARY_PATH=/ 

My Perl script is equal to test.pl and it has:

 #!/usr/bin/perl system(". /root/.profile"); 

When I execute ./test.pl , LD_LIBRARY_PATH does not change.

What am I doing wrong?

+6
source share
5 answers

Your current script does not even change the environment variable in the Perl script itself. Rather, it calls the shell as a subprocess; this shell process performs . /root/.profile . /root/.profile , which updates $LD_LIBRARY_PATH only in this shell process.

You can change the environment variable in a Perl script (more precisely, while working with a Perl script) by updating %ENV :

 $ENV{LD_LIBRARY_PATH} = '/'; # or some more reasonable value 

As perldoc -v %ENV says:

%ENV hash contains the current environment. Setting the value to "ENV" changes the environment for any child processes that you subsequently " fork() " are disabled.

But it probably won't do what you want anyway; it will not (and cannot) affect the process environment that calls the Perl script (your interactive shell), only the Perl process itself and everything that it calls.

Suppose you want to update $LD_LIBRARY_PATH in the current interactive shell mode. To do this, you can force the Perl script to print a shell command that will update $LD_LIBRARY_PATH . Then, instead of just running your Perl script, you can execute it and then evaluate its output. For instance:

 $ cat env.pl #!/usr/bin/perl use strict; use warnings; print "export LD_LIBRARY_PATH=/\n"; $ ./env.pl # just prints the command without executing it export LD_LIBRARY_PATH=/ $ eval $(./env.pl) # executes the command in the current shell $ echo $LD_LIBRARY_PATH / $ 

This assumes your current bash shell or something similar.

Another option: after changing %ENV your Perl script may invoke another command, even a new interactive shell. The new process inherits its environment from the Perl script. This can be a little cumbersome; for example, if the new process is an interactive shell, it will not inherit outstanding variables or history from the parent shell.

(One point not directly related to your question: the fact that you /root/.profile with /root/.profile implies that you are doing something as root (superuser). This can be dangerous. Use root (or by logging in , or through sudo only for things that really need root privileges, use a personal user account for anything else.

+11
source

system starts a new process, and changing the environment does not affect the environment in the process of your script (usually - there are usually os-dependent means of changing the environment of other processes).

The environment in the perl program is associated with %ENV , which is similar (in fact, it is not a hash) to the environment: changing it will change the environment. In this way:

 $ENV{LD_LIBRARY_PATH} = '/'; 
+5
source

To change the environment in a Perl script, assign the %ENV hash:

 $ENV{'LD_LIBRARY_PATH'} = '/'; 

If you want to write a program used by the shell to change its environment, then how this is usually done is to let the script command write shell commands to stdout. The shell then does this with command substitution and uses eval to execute the received commands:

Perl script:

 #!/usr/bin/perl print 'LD_LIBRARY_PATH=\n'; 

Shell script:

 eval "$(/path/to/perlscript)" 

For examples of commands that work as follows, see tset and ssh-agent .

+3
source

Now this can be done using Env::Modify module :

 use Env::Modify 'source'; source("/root/.profile"); ... env settings of .profile are now available to Perl ... 
+2
source

You cannot do this.

These are from Perl FAQs :

In the strict sense, this cannot be done - the script is executed as another process from the shell with which it was launched. Changes in the process are not reflected in its parent - only in any children created after the change. There is a shell of magic that can allow you to fake it by eval () outputting a script in your shell; Check out the comp.unix.questions FAQ.

+1
source

Source: https://habr.com/ru/post/955281/


All Articles