Re: Exporting variables from perl script to shell
by revdiablo (Prior) on Dec 21, 2004 at 18:18 UTC
|
Aristotle touched on it, but I thought I would reinforce his point. In most [all?] Unix-like operating systems, it's not possible for a process to modify its parent's environment. When you execute a program from most shells, it is executed in a child process. Thus, a shell script cannot add environment variables to the interactive shell when run as ./shell-script. Nor can any other program. That's just how it is.
Now, you might be thinking "then how do environment variables in my config files get loaded?!?" The answer is that those files are not executed in a child process. They are sourced. That is, the file is read in and executed directly in the current process. This can only work with a script written for the shell that's sourcing it, so this technique will not work for a Perl script.
| [reply] [d/l] [select] |
Re: Exporting variables from perl script to shell
by Aristotle (Chancellor) on Dec 21, 2004 at 16:37 UTC
|
Depends on what “everywhere” means.
In all the programs that the Perl script invokes? That's simple: set some key in %ENV. F.ex, $ENV{PATH} is the same as what passes for $PATH in the shell and changes the path inside your Perl script and for all programs that your Perl script calls.
Do you mean in the program that invoked the shell script? That's either tricky or impossible, depending on the invoking program. If the Perl script is invoked from shell, then you need to print the values as shell code, capture this output with backticks, and eval it in the shell — as in eval `perl -le'print "PATH=$ENV{PATH}:/tmp/foobar/\n"'`. Yes, it's a bit dizzying… If the invoking program is not a shell, then the answer is probably “no can do”.
Makeshifts last the longest.
| [reply] |
Re: Exporting variables from perl script to shell
by dragonchild (Archbishop) on Dec 21, 2004 at 16:36 UTC
|
I'm assuming you're calling the shell script using the system() function. What's wrong with:
system(
"FOO='bar' ",
$command,
);
Update: system( "FOO='bar' $command" ); should work better, according to Aristotle. I was just trying to point out you can set shell parameters on a per-command basis.
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] [d/l] [select] |
|
|
I would code it like this:
do {
local $ENV{FOO} = 'bar';
system($command);
};
This gives you the temporary-env-value semantics of "FOO='bar' $command", yet doesn't assume that the user's shell has said semantics (Win32's cmd.exe certainly doesn't, for example).
--Stevie-O
$"=$,,$_=q>|\p4<6 8p<M/_|<('=>
.q>.<4-KI<l|2$<6%s!<qn#F<>;$,
.=pack'N*',"@{[unpack'C*',$_]
}"for split/</;$_=$,,y[A-Z a-z]
{}cd;print lc
| [reply] [d/l] [select] |
|
|
How about “that doesn't work”? :-) You need something like system( "FOO='bar' $command" );, ie you must go via shell — the list form of system won't.
Makeshifts last the longest.
| [reply] [d/l] |
|
|
| [reply] |
Re: Exporting variables from perl script to shell
by exussum0 (Vicar) on Dec 21, 2004 at 16:50 UTC
|
...
The env utility executes utility after modifying the environment as spec-
ified on the command line. The option name=value specifies an environ-
ment variable, name, with a value of value.
...
--
> which env
/usr/bin/env
----
Give me strength for today..
I will not talk it away..
Just for a moment..
It will burn through the clouds..
and shine down on me.
| [reply] |