Re: setting several environment variables in the current shell process
[email protected] (Christopher Hicks) Mon, 20 Sep 2004 07:56:07 -0400 (EDT)
| Newsgroups | perl.trainers |
|---|---|
| Message-ID | <[email protected]> |
On Mon, 20 Sep 2004, Gaurav Arora wrote:
> I am writing a perl script (environment.pl) which sets several environment
> variables which have to be applied to the current shell process.
Then you some how hae to figure out how to get the current shell process
to deal with the results of your perl script.
> I have set the values in the perl script using $ENV{'var1'} = "aaa" syntax.
> This would set the environment variables for the perl script process and the
> child process.
"child processes if any" would be more accurate, but yes.
> But I want the environment variables to be accessible from the
> shell which invoked the perl script ( i.e. $./environment.pl).
That's creative syntax, but not what you need.
> In csh based scripts, I can use the 'source' keyword, or in 'sh' based
> scripts, I can use the '.' symbol. But what would be the equivalent in
> Perl ?
You're heading the right direction and the wrong direction here. You have
to source the environment variables into the invoking shell somehow. It's
not really a question of "what is the equivalent in Perl", but "how do I
get this information out of Perl and into the shell environment. There
are two choices, both require a wrapper in the shell of some sort.
(A) The easiest choice is to take the output of the Perl script and have
the shell execute the result.
[chicks@wakko2b chicks]$ perl -e 'print "MYVAR=chicks\n";' > tmp.sh
[chicks@wakko2b chicks]$ echo $MYVAR
[chicks@wakko2b chicks]$ cat tmp.sh
MYVAR=chicks
[chicks@wakko2b chicks]$ . tmp.sh
[chicks@wakko2b chicks]$ echo $MYVAR
chicks
[chicks@wakko2b chicks]$
(B) Another useful variation on this is to make a function or an alias
that invokes the perl script, runs the temporary file, then deletes the
temporary file.
(C) You may have to have the perl script write the output file and not
redirect the output of the perl script so that it can be interactive. If
you don't need interactivity doing the shell redirection saves a little
typing.
> Please help. I have chosen to use Perl scripting language instead of Shell
> scripting as the script has to be shell type independant.
Perl is a good choice for scripting language. You might have good luck
asking this sort of question on Perl Monks in the future. This mailing
list is for folks doing training, teaching perl classes, and isn't the
ideal place for general perl questions.
--
</chris>
There are two ways of constructing a software design. One way is to make
it so simple that there are obviously no deficiencies. And the other way
is to make it so complicated that there are no obvious deficiencies.
-- C.A.R. Hoare