How to import environment settings into my Perl program?

I have a script whose contents simply export the variable to linux.

export LD_LIBRARY_PATH=.... 

I want to run this script in my Perl script so that the one who runs my Perl script will have its own LD_LIBRARY_PATH set. Can I just do this at the beginning of my Perl script:

 #!/usr/bin/perl -w system(". /myfolder1/myfolder2/myScript.sh"); 
+4
source share
10 answers
 #!/bin/sh . /myfolder1/myfolder2/myScript.sh exec perl -wxS "$0" " $@ " #!/usr/bin/perl -w # .. the rest of your script as normal 

When you run this, it will first execute /bin/sh , which is able to load myScript.sh into the local environment. sh , and then exec Perl, who is prompted to continue execution from the next line.

+10
source

This will not work. To change the environment inside your Perl script (and change the environment that will be passed to commands from within your Perl script), change the %ENV variable.

 $ENV{"LD_LIBRARY_PATH"} = ... ; 
+5
source

This will not work. It is not possible to manipulate the environment of the parent process with a subshell.

But you could echo your script the line you want to set as LD_LIBRARY_PATH, and then from inside your Perl script you could do something like this:

 $ENV{LD_LIBRARY_PATH} = `path/to/your/script.sh`; 

Of course, a little error checking may also be a good idea.

+4
source

Not. Changes to your environment made to children cannot affect the parent. This means that running the script will not affect perl. Also, perl will not affect the shell from which it was called. You can edit the environment inside perl by changing a special variable %ENV . If there is some irreproducible calculation in this script, perhaps the script should just have an echo parameter, and perl can select this on STDOUT and use it.

I {changed directory, changed my environment} in a perl script. Why did the change disappear when I exited the script? How will my changes be visible?

Unix In the strict sense, this cannot be done - the script is executed like another process from the shell it started with. Changes to the process are not reflected in its parent, only in their own children created after the change.

+2
source

I had a similar problem a few years ago and hacked into a small module, Env :: Sourced , which should do the trick.

  use Env::Sourced qw(/myfolder1/myfolder2/myScript.sh); 

...

+2
source

Another option (other than making changes directly to Perl %ENV ) is to make the changes you want to the Perl module so you can say:

 use MyEnvironment; 

and change your environment in all your scenarios. This will make it easier to make changes after there is no need to edit each script.

The module itself will be simple, something like this:

 package MyEnvironment; $ENV{LD_LIBRARY_PATH} .= ":/some/path/you/want/appended"; # Any other changes you want here. 1; 
+1
source

This will not work. An alternative would be to replace / usr / bin / perl with a shell script that first executes your script and then executes the perl executable.

0
source

It is impossible to do the way you are trying to do it.

It either needs a shell script shell that sets LD_LIBRARY_PATH and then calls your perl script, or any user running the script must have LD_LIBRARY_PATH properly configured first.

If you are doing the latter, then this can be controlled globally by editing the shells /etc/profile and /etc/cshrc (for ksh, sh, bash, csh and tcsh). Then you can check the LD_LIBRARY_PATH value in the script, and if you do not install / install it incorrectly, then print a friendly message to the user. Alternatively, individual users can install this in their local .profile / .cshrc files.

Note. You did not provide any information about the environment or users that can run it, so there is also a possibility that users can set LD_LIBRARY_PATH what they need. If you check LD_LIBRARY_PATH for a “good” value in a script, then keep in mind that several paths can be specified, so you will need to parse this environment variable correctly.

0
source

If you can find the right place in your perl script, this works as in my example:

 $ENV{"LD_LIBRARY_PATH"} = "/oracle/product/10g/lib"; 

And that did not require me to call another script to install env var.

0
source

The Env::Modify module solves this problem, at least for POSIX-y platforms:

 use Env::Modify 'source'; source("/myfolder1/myfolder2/myScript.sh"); ... environment settings from myScript.sh are now available to Perl ... 
0
source

All Articles