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
Get Chitika Premium
Posted by Joost Diepenmaat on February 21, 2008, 2:00 pm
Please log in for more thread options

> the input file (I'd like to be compatible with unix export statements.
> I strip that out later)
> export TMPTEST=/tmp
> export TMPTOO=$TMPTEST
>
> The simplified module:

[ ... ]

> ($l,$e,$v) = (/^(export\s+)(\w+)=(.+)/);
> $ENV="$v";

This is your problem.

Since you're TMPTOO=$TMPTEST is read from a file it's not
interpolated. You're effectively doing

my $e = 'TMPTOO';
my $v = '$TMPTEST';
$ENV=$v;

Also note that "$v" only interpolates the value of $v, not any $vars in
the value of $v. This is intentional, since otherwise reading from a
file or assigning strings would interpolate all over the place.

I'd do something like this:

my ($l,$e,$v) = (/^(export\s+)(\w+)=(.+)/);
$v = s/$(\w+)/$ENV/eg; #look up $somethings in $ENV
$ENV=$v;

Note 2: this does not handle quotes in the export statement.

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

Posted by Dennis Roesler on February 21, 2008, 3:48 pm
Please log in for more thread options
pgodfrin wrote:
> ...snip...
>> 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 found the snippet of code inside this sub on the net somewhere and has
been tweaked for our use. I put it in a module that is library of
general purpose functions we use.

sub getEnv {
my $perlCommand = "perl -MData::Dumper -e 'print Dumper(\%ENV)';";
#my $sourceCommand = "/bin/ksh";
my $sourceCommand = "";
while(my $file = shift) {
carp("$file does not exist") && next unless(-e $file);
my $sourceLine = "$sourceCommand $file 1>&2";
my %tmp = %{eval('my ' . `. $sourceLine\n$perlCommand`)};
$ENV = $tmp for (keys %tmp);
}
}


Dennis
d underscore roesler at agilent dot com

Posted by Tad J McClellan on February 21, 2008, 6:22 pm
Please log in for more thread options


> ($l,$e,$v) = (/^(export\s+)(\w+)=(.+)/);
> $ENV="$v";


What values will those variables have if the string
in $_ does not happen to match your pattern?

if ( ($l,$e,$v) = (/^(export\s+)(\w+)=(.+)/) ) {
# do stuff with $l, $e and $v
}


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

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