How do I import environment settings into my Perl program?

吃可爱长大的小学妹 提交于 2019-11-30 23:28:18
#!/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 be executed by /bin/sh, which is capable of loading myScript.sh into the local environment. sh then execs Perl, which is told to continue from the following line.

This won't work. To change the environment inside your Perl script (and to change the environment that will be passed on to commands run from inside your Perl script), change the %ENV variable.

$ENV{"LD_LIBRARY_PATH"} = ... ;

This won't work. There is no way for a subshell to manipulate the environment of the parent process.

But you could make your script echo the string you want to set as LD_LIBRARY_PATH and then from within your Perl script you could do something like that:

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

Of course, a bit of error checking might also be a good idea.

dlamblin

No. Your environment changes made in a child cannot affect the parent. This means running a 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 the special variable %ENV. If there's some kind of unreproducible calculation done in that script, maybe the script should just echo the setting and perl can pick that up on STDOUT and use it.

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 its own children created after the change.

Josh McAdams

I had a similar problem a few years ago and whipped up a little module, Env::Sourced, that should do the trick.

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

...

zigdon

Another option (other than making the changes directly in Perl's %ENV) is to make the changes you want a Perl module, so that you can say:

use MyEnvironment;

and have it modify your environment in all your scripts. It would make it simple to make changes after the fact that will not require editing every 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;

That won't work. An (unpleasant) alternative might be to replace /usr/bin/perl with a shell script that first executes your script and then executes the perl executable.

This can't be done in the way you're trying to do this.

It either needs a wrapper shell script that sets LD_LIBRARY_PATH and then calls your perl script, or any user executing the script needs to have LD_LIBRARY_PATH set correctly in the first place.

If doing the latter, then this can be managed globally by editing /etc/profile and /etc/cshrc (for ksh, sh, bash, csh and tcsh) shells. You can then test for the value of LD_LIBRARY_PATH in your script and if not set/set incorrectly then print a friendly message to the user. Alternatively individual users can set this in their local .profile/.cshrc files.

Note: you haven't given any information about the environment or useres that might run this, so there's also the possibility that users may set LD_LIBRARY_PATH to something they need. If you do check LD_LIBRARY_PATH for a "good" value in your script, then keep in mind that several paths may have been specified, so you will need to parse this environment variable properly.

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 it didn't require me to call another script to set the env var.

The Env::Modify module addresses this issue, 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 ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!