Click here to get back home

setting %ENV in a module

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
setting %ENV in a module pgodfrin 02-20-2008
Posted by pgodfrin on February 20, 2008, 7:59 pm
Please log in for more thread options
Greetings,
So - I'd like to have a subroutine that reads a file with lines like:
export MYENVVAR=some_data and sets the hash $ENV=some_data.
This part is easy....

I'd like to put that subroutine in a module so that I can use that
subroutine wherever I want to. I having problems with scoping - seems
the module subroutine sets the environment but then loses when control
is returned to the calling program. This part is hard.

Any thoughts?

pg

Posted by Gunnar Hjalmarsson on February 20, 2008, 8:41 pm
Please log in for more thread options
pgodfrin wrote:
> So - I'd like to have a subroutine that reads a file with lines like:
> export MYENVVAR=some_data and sets the hash $ENV=some_data.
> This part is easy....
>
> I'd like to put that subroutine in a module so that I can use that
> subroutine wherever I want to. I having problems with scoping - seems
> the module subroutine sets the environment but then loses when control
> is returned to the calling program.

I can't reproduce the problem you describe.

C:\home>type MyModules\ENVSet.pm
package MyModules::ENVSet;
$ENV='Hello';
1;

C:\home>type test.pl
use MyModules::ENVSet;
print "$ENV\n";

C:\home>test.pl
Hello

C:\home>

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by pgodfrin on February 20, 2008, 11:41 pm
Please log in for more thread options
> pgodfrin wrote:
> > So - I'd like to have a subroutine that reads a file with lines like:
> > export MYENVVAR=some_data and sets the hash $ENV=some_data.
> > This part is easy....
>
> > I'd like to put that subroutine in a module so that I can use that
> > subroutine wherever I want to. I having problems with scoping - seems
> > the module subroutine sets the environment but then loses when control
> > is returned to the calling program.
>
> I can't reproduce the problem you describe.
>
> C:\home>type MyModules\ENVSet.pm
> package MyModules::ENVSet;
> $ENV='Hello';
> 1;
>
> C:\home>type test.pl
> use MyModules::ENVSet;
> print "$ENV\n";
>
> C:\home>test.pl
> Hello
>
> C:\home>
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl

I got the same results as you - with your code. My other code is still
screwy - more on this tomorrow...
pg

Posted by Joost Diepenmaat on February 21, 2008, 3:02 am
Please log in for more thread options

> I'd like to put that subroutine in a module so that I can use that
> subroutine wherever I want to. I having problems with scoping - seems
> the module subroutine sets the environment but then loses when control
> is returned to the calling program. This part is hard.

I'm not sure if this applies to your problem, but tou may be unaware
that on most operating systems it's impossible to set the environment
for anything other than the "current process" (and any child processes
will then inherit the parent's environment).

In other words, if program A fork()s program B, and program B
changes its %ENV, program A will not see those changes. Neither will
program B see any changes in program A's environment made after B is
forked off.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by pgodfrin on February 21, 2008, 8:58 am
Please log in for more thread options
> > I'd like to put that subroutine in a module so that I can use that
> > subroutine wherever I want to. I having problems with scoping - seems
> > the module subroutine sets the environment but then loses when control
> > is returned to the calling program. This part is hard.
>
> I'm not sure if this applies to your problem, but tou may be unaware
> that on most operating systems it's impossible to set the environment
> for anything other than the "current process" (and any child processes
> will then inherit the parent's environment).
>
> In other words, if program A fork()s program B, and program B
> changes its %ENV, program A will not see those changes. Neither will
> program B see any changes in program A's environment made after B is
> forked off.
>
> --
> Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/

Yes I'm aware of this - I think this is a scoping issue. So program A
loads the modules, and then calls it to set the environment values. I
only care for the variables set during that process, and any other
from the calling program. However, certain variables get set and
others are not set exactly right....

I'll send some example code shortly...
pg

Similar ThreadsPosted
setting uid gid after fork August 28, 2007, 3:35 am
Setting chmod March 9, 2008, 12:26 pm
Re: Setting up mod_perl March 10, 2008, 2:50 pm
Re: Setting up mod_perl March 11, 2008, 11:17 pm
Net::DNS-> About setting more than one nameserver to lookup. August 7, 2004, 9:05 pm
setting cookies (mod_perl) October 14, 2004, 9:15 pm
CGI.PM not setting HTTP header November 24, 2004, 10:12 pm
Setting RTS with Win32::SerialPort February 14, 2005, 2:12 am
Setting environment with a script March 4, 2005, 3:35 am
Problem with setting LD_LIBRARY_PATH April 21, 2005, 6:04 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap