Exporting environment variables 
Author Message
 Exporting environment variables

Quote:


> > eval `setenv.pl`

> > where setenv.pl prints (on stdout):
> >    export A=B
> >    export C=D
> > or whatever

> Now that's slick!  I've received a lot of good ideas here; thanks
> everyone.  This is the coolest so far.

> We might need to have the same setenv.pl script support a Unix and NT
> environment, so we'll just have setenv.pl print 'set A=B' if the OS eq
> 'NT', else 'export A=B'.  

Except that NT doesn't support 'eval'... (though maybe cygwin's bash does --
but that would require 'export' rather than 'set').

Cheers,
Philip



Sat, 01 Mar 2003 20:05:58 GMT  
 Exporting environment variables

Quote:



> > > eval `setenv.pl`

> > > where setenv.pl prints (on stdout):
> > >    export A=B
> > >    export C=D
> > > or whatever

> > Now that's slick!  I've received a lot of good ideas here; thanks
> > everyone.  This is the coolest so far.

> > We might need to have the same setenv.pl script support a Unix and NT
> > environment, so we'll just have setenv.pl print 'set A=B' if the OS eq
> > 'NT', else 'export A=B'.  

> Except that NT doesn't support 'eval'... (though maybe cygwin's bash does --
> but that would require 'export' rather than 'set').

> Cheers,
> Philip

I took it to mean that in stand-alone mode, you would just run
setenv.pl (or 'perl setenv.pl'), and when calling from another perl
script you would eval it.

--
Steve Molitor



Sat, 01 Mar 2003 22:24:38 GMT  
 Exporting environment variables

Quote:


> > > > eval `setenv.pl`

> > > > where setenv.pl prints (on stdout):
> > > >    export A=B
> > > >    export C=D
> > > > or whatever

> I took it to mean that in stand-alone mode, you would just run
> setenv.pl (or 'perl setenv.pl'), and when calling from another perl
> script you would eval it.

I took it to mean that you use the sh/ksh builtin "eval" to execute the
output of the Perl script. Especially since "export A=B" is sh/ksh variable
setting syntax, not Perl variable setting syntax (or csh, for that matter --
I believe it's "setenv A B" there).

If you want to call a Perl script from another Perl script, use $ENV{'A'} =
'B' and then 'do' or 'require' this variable-setting script from another.

Cheers,
Philip



Sun, 02 Mar 2003 00:27:19 GMT  
 Exporting environment variables

Quote:




> > > > eval `setenv.pl`

> > > > where setenv.pl prints (on stdout):
> > > >    export A=B
> > > >    export C=D
> > > > or whatever

> > > Now that's slick! I've received a lot of good ideas here; thanks
> > > everyone. This is the coolest so far.

> > > We might need to have the same setenv.pl script support a Unix
> > > and NT environment, so we'll just have setenv.pl print 'set A=B'
> > > if the OS eq 'NT', else 'export A=B'.

> > Except that NT doesn't support 'eval'... (though maybe cygwin's
> > bash does -- but that would require 'export' rather than 'set').

> > Cheers,
> > Philip

> I took it to mean that in stand-alone mode, you would just run
> setenv.pl (or 'perl setenv.pl'), and when calling from another perl
> script you would eval it.

Back-ticks in perl (and in the unix shells) fork a subshell. Hence
doing `setenv.pl` gets you nothing, because any changes are gone when
the subshell returns. It is when you evaluate the output returned
_from_ the back-ticks that your _current_ environment gets set. This
is what the eval example does on unix. Look at the first line from
John Wiersba again. The current shell evaluates the stuff that
setenv.pl is printing to stdout.

If you want to run the myprog.pl perl program with a specific set of
env-vars, you _can_ do it as follows.

-- runmyprog.bat Batch file to start myprog.pl with correct ENV:---
setenv.pl > set-my-env.bat
call set-my-env.bat
perl myprog.pl
-- end of batch file

The unix equivalent:
-- runmyprog shell script to start myprog.l with correct ENV:---
#!/bin/sh
eval `setenv.pl`
myprog.pl
-- end of shell script

These two examples do _exactly_ the same thing on their respective
platforms. Now, this gets cumbersome, so on unix you will run the
first line of this thing at some system startup/login time, and then
make sure whoever runs myprog.pl is a child process which has
inherited its environment from this startup process. On windows 98
this would be by putting the first two lines of the batch-file in
autoexec.bat or wherever. On NT and 2000 you would need to do some
fancy registry things (using perl of course) or you could use some env
setting command that comes with the NT recource kit.

Now if you plan to run setenv.pl from another perl process, its much
easier just to put stuff like:


$ENV{'A'}='B'; # notice no print statement
--myprog.pl--
#!/usr/bin/perl -w
use strict;
do setenv.pl;
--

This is possible becaues it would be the _same_ perl process running
myprog.pl and setenv.pl, so the ENV changes would stick.

--
H?kon Alstadheim, Montreal, Quebec, Canada  



Sun, 02 Mar 2003 04:14:24 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. TUTIL32 and DLL index value help?

2. Nuno Ferreira

3. ACCESS + DELPHI or VB ??

4. A problem with Indexes in Paradox tables

5. Validators and Exceptions

6. Exporting environment variables

7. Exporting environment variables

8. Exporting environment variables in CGI-script

9. Export environment variable to parent

10. Exporting environment variables to parent shell

11. How do I export environment variables?

12. Exporting environment variables in CGI-script

 

 
Powered by phpBB® Forum Software