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 pgodfrin on February 21, 2008, 12:15 pm
Please log in for more thread options
...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'll send some example code shortly...
> pg

Well - here's the scoop. I believe this is not necessarily a scoping
issue, but one of understanding how perl interprets environment
variables. (or maybe how I interpret how... <grin>). It appears that
if one tries to set $ENV="$ENV-VAR" (with or without quotes)
then the resulting hash value is the literal $ENV-VAR - perl is not
evaluating the scalar variable. (as opposed to use Env qw(VAR-NAME)
which permits using $VAR-NAME as a scalar variable in the subsequent
code).

I'm a little stumped as to why.

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:
package MymodSimp;
use Env;
Env::import;
my(@rvars,$l,$e,$v);
open RVARS,"/home/pgodfrin/perl/et/etfile" ;
@rvars=<RVARS>; close RVARS;
foreach (@rvars)
{
if (!/(^export)(\s+)\w+=/) { next;} # skips garbage
($l,$e,$v) = (/^(export\s+)(\w+)=(.+)/);
$ENV="$v";
}
$ENV='myliteral';
1;

The calling program:
[pgodfrin:~/perl/et]> cat envtest
#!/usr/bin/perl
use warnings;
no warnings 'once';
use Env;
Env::import;
use MymodSimp ;

$ENV="$TMPTOO";
$ENV=$ENV;
print "TMPTEST: $ENV\n";
print "TMPTOO: $ENV\n";
print "TMPTREE: $ENV\n";
print "TMPFOUR: $ENV\n";
print "TMPFIVE: $ENV\n";

chdir("$TMPTEST") or die "Can't change dir to $TMPTEST\n";
print "Change to dir $TMPTEST:"; system("pwd"); print "\n";
chdir("$TMPTOO") or die "Can't change dir to $TMPTOO\n";
print "Change to dir $TMPTOO:"; system("pwd"); print "\n";
chdir("$TMPFIVE") or die "Can't change dir to $TMPFIVE\n";
print "Change to dir $TMPFIVE:"; system("pwd"); print "\n";

exit;

The execution.
[pgodfrin:~/perl/et]> envtest
TMPTEST: /tmp
TMPTOO: $TMPTEST
TMPTREE: myliteral
TMPFOUR: $TMPTEST
TMPFIVE: /tmp
Change to dir /tmp:/tmp

Can't change dir to $TMPTEST

So - setting $ENV="$TMPTEST" doesn't evaluate the variable.

I was hoping to be able to have a perl program that uses environment
variables via the use Env qw(yada-yada) construct, but also be able to
load the env vars from a file in the event the same perl program is
called via the crontab. There is a workaround for the crontab thing:
(. varfile; myperlpgm) - this will first source the varfile and any
env vars defined there will be available to myperlpgm via use Env
qw(yada-yada), but that doesn't seem very elegant. Another work around
is to create a wrapper for any perl program that is executed in the
crontab: 22 13 * * * run-job - where run-job is either a shell script
or a calling perl program which set's the variables, but that also
seem inelegant and has it's own set of problems). In any case, that's
the source of my consternation...

regards,
pg

Posted by Jim Gibson on February 21, 2008, 1:50 pm
Please log in for more thread options
In article


> Well - here's the scoop. I believe this is not necessarily a scoping
> issue, but one of understanding how perl interprets environment
> variables. (or maybe how I interpret how... <grin>). It appears that
> if one tries to set $ENV="$ENV-VAR" (with or without quotes)
> then the resulting hash value is the literal $ENV-VAR - perl is not
> evaluating the scalar variable. (as opposed to use Env qw(VAR-NAME)
> which permits using $VAR-NAME as a scalar variable in the subsequent
> code).

That is most definitely not true. You are extracting the string
'$TMPTEST' from a string using a regular expression and storing it into
the variable $v. When you assign the value of $v, using the expression
"$v", to the value of the %ENV hash, that string gets stored as is. If
you want to evaluate what $v contains as a Perl expression, then you
should use the eval operator:

$ENV = eval $v;

See the difference here:

% perl -e '$x=q($y);$y="abc";print $x,"\n";'
$y
% perl -e '$x=q($y);$y="abc";print eval $x,"\n";'
abc

The Env package (I have not used it) ties the values of the members of
%ENV to scalar variables (and arrays if needed), so it will tie
$ENV to $TMPTEST, and changing one will change the other.
Perhaps this fact is confusing you. If so, you might be better off not
using Env.pm and sticking to just using %ENV.

>
> I'm a little stumped as to why.
>
> 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:
> package MymodSimp;
> use Env;
> Env::import;
> my(@rvars,$l,$e,$v);
> open RVARS,"/home/pgodfrin/perl/et/etfile" ;
> @rvars=<RVARS>; close RVARS;
> foreach (@rvars)
> {
> if (!/(^export)(\s+)\w+=/) { next;} # skips garbage
> ($l,$e,$v) = (/^(export\s+)(\w+)=(.+)/);
> $ENV="$v";
> }
> $ENV='myliteral';
> 1;

It is an inefficient use of memory to read a file into an array,
process the lines of that array one-at-a-time, and discard the array.
It is more efficient to process the lines when they are read.

There is no need to use two identical regular expressions to test for a
match and then extract data. Do it in one step.

It is better to use private variables for file handles and use the
three-argument version of open.

There is no need to quote scalar variables.

Therefore:


package MymodSimp;
open my $rvars, '<', "/home/pgodfrin/perl/et/etfile" ;
foreach (<$rvars>)
{
if( /^export\s+(\w+)=(.+)/ ) {
$ENV=$2;
}
}
$ENV='myliteral';
1;

>
> The calling program:
> [pgodfrin:~/perl/et]> cat envtest
> #!/usr/bin/perl

use strict;

> use warnings;
> no warnings 'once';
> use Env;
> Env::import;
> use MymodSimp ;
>
> $ENV="$TMPTOO";
> $ENV=$ENV;
> print "TMPTEST: $ENV\n";
> print "TMPTOO: $ENV\n";
> print "TMPTREE: $ENV\n";
> print "TMPFOUR: $ENV\n";
> print "TMPFIVE: $ENV\n";
>
> chdir("$TMPTEST") or die "Can't change dir to $TMPTEST\n";
> print "Change to dir $TMPTEST:"; system("pwd"); print "\n";
> chdir("$TMPTOO") or die "Can't change dir to $TMPTOO\n";
> print "Change to dir $TMPTOO:"; system("pwd"); print "\n";
> chdir("$TMPFIVE") or die "Can't change dir to $TMPFIVE\n";
> print "Change to dir $TMPFIVE:"; system("pwd"); print "\n";
>
> exit;
>
> The execution.
> [pgodfrin:~/perl/et]> envtest
> TMPTEST: /tmp
> TMPTOO: $TMPTEST
> TMPTREE: myliteral
> TMPFOUR: $TMPTEST
> TMPFIVE: /tmp
> Change to dir /tmp:/tmp
>
> Can't change dir to $TMPTEST
>
> So - setting $ENV="$TMPTEST" doesn't evaluate the variable.

It does, but that is not what you are doing. You are doing

$ENV = "$TMPTOO";

where $TMPTOO contains '$TMPTEST'.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Posted by pgodfrin on February 22, 2008, 9:13 am
Please log in for more thread options
> In article
>
> > Well - here's the scoop. I believe this is not necessarily a scoping
> > issue, but one of understanding how perl interprets environment
> > variables. (or maybe how I interpret how... <grin>). It appears that
> > if one tries to set $ENV="$ENV-VAR" (with or without quotes)
> > then the resulting hash value is the literal $ENV-VAR - perl is not
> > evaluating the scalar variable. (as opposed to use Env qw(VAR-NAME)
> > which permits using $VAR-NAME as a scalar variable in the subsequent
> > code).
>
> That is most definitely not true. You are extracting the string
> '$TMPTEST' from a string using a regular expression and storing it into
> the variable $v. When you assign the value of $v, using the expression
> "$v", to the value of the %ENV hash, that string gets stored as is. If
> you want to evaluate what $v contains as a Perl expression, then you
> should use the eval operator:
>
> $ENV = eval $v;
>
> See the difference here:
>
> % perl -e '$x=q($y);$y="abc";print $x,"\n";'
> $y
> % perl -e '$x=q($y);$y="abc";print eval $x,"\n";'
> abc
>
> The Env package (I have not used it) ties the values of the members of
> %ENV to scalar variables (and arrays if needed), so it will tie
> $ENV to $TMPTEST, and changing one will change the other.
> Perhaps this fact is confusing you. If so, you might be better off not
> using Env.pm and sticking to just using %ENV.
>
>
>
>
>
> > I'm a little stumped as to why.
>
> > 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:
> > package MymodSimp;
> > use Env;
> > Env::import;
> > my(@rvars,$l,$e,$v);
> > open RVARS,"/home/pgodfrin/perl/et/etfile" ;
> > @rvars=<RVARS>; close RVARS;
> > foreach (@rvars)
> > {
> > if (!/(^export)(\s+)\w+=/) { next;} # skips garbage
> > ($l,$e,$v) = (/^(export\s+)(\w+)=(.+)/);
> > $ENV="$v";
> > }
> > $ENV='myliteral';
> > 1;
>
> It is an inefficient use of memory to read a file into an array,
> process the lines of that array one-at-a-time, and discard the array.
> It is more efficient to process the lines when they are read.
>
> There is no need to use two identical regular expressions to test for a
> match and then extract data. Do it in one step.
>
> It is better to use private variables for file handles and use the
> three-argument version of open.
>
> There is no need to quote scalar variables.
>
> Therefore:
>
> package MymodSimp;
> open my $rvars, '<', "/home/pgodfrin/perl/et/etfile" ;
> foreach (<$rvars>)
> {
> if( /^export\s+(\w+)=(.+)/ ) {
> $ENV=$2;
> }}
>
> $ENV='myliteral';
> 1;
>
>
>
> > The calling program:
> > [pgodfrin:~/perl/et]> cat envtest
> > #!/usr/bin/perl
>
> use strict;
>
>
>
> > use warnings;
> > no warnings 'once';
> > use Env;
> > Env::import;
> > use MymodSimp ;
>
> > $ENV="$TMPTOO";
> > $ENV=$ENV;
> > print "TMPTEST: $ENV\n";
> > print "TMPTOO: $ENV\n";
> > print "TMPTREE: $ENV\n";
> > print "TMPFOUR: $ENV\n";
> > print "TMPFIVE: $ENV\n";
>
> > chdir("$TMPTEST") or die "Can't change dir to $TMPTEST\n";
> > print "Change to dir $TMPTEST:"; system("pwd"); print "\n";
> > chdir("$TMPTOO") or die "Can't change dir to $TMPTOO\n";
> > print "Change to dir $TMPTOO:"; system("pwd"); print "\n";
> > chdir("$TMPFIVE") or die "Can't change dir to $TMPFIVE\n";
> > print "Change to dir $TMPFIVE:"; system("pwd"); print "\n";
>
> > exit;
>
> > The execution.
> > [pgodfrin:~/perl/et]> envtest
> > TMPTEST: /tmp
> > TMPTOO: $TMPTEST
> > TMPTREE: myliteral
> > TMPFOUR: $TMPTEST
> > TMPFIVE: /tmp
> > Change to dir /tmp:/tmp
>
> > Can't change dir to $TMPTEST
>
> > So - setting $ENV="$TMPTEST" doesn't evaluate the variable.
>
> It does, but that is not what you are doing. You are doing
>
> $ENV = "$TMPTOO";
>
> where $TMPTOO contains '$TMPTEST'.
>
> --
> Jim Gibson
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com

Hi - I knew there was a better way! I will try this out. I realized as
I was posting my code that it is indeed silly to dump the file into
memory. I've changed that. Why is the three argument version of open
better though?

thanks,
pg

Posted by Jim Gibson on February 22, 2008, 1:56 pm
Please log in for more thread options
In article


> Hi - I knew there was a better way! I will try this out. I realized as
> I was posting my code that it is indeed silly to dump the file into
> memory. I've changed that. Why is the three argument version of open
> better though?

It is more explicit and more flexible. See 'perldoc -f open' for all
the things you can do with the 3-argument version of open. For simple
programs, it doesn't really matter. But for more complicated programs,
the explicitness is helpful and consistency is good for
maintainability.

One slightly facetious difference: with 2-argument open you can't open
a file named '<stupid_file_name.txt'.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Posted by Ben Morrow on February 22, 2008, 3:21 pm
Please log in for more thread options

> In article
>
> > Hi - I knew there was a better way! I will try this out. I realized as
> > I was posting my code that it is indeed silly to dump the file into
> > memory. I've changed that. Why is the three argument version of open
> > better though?
>
> It is more explicit and more flexible. See 'perldoc -f open' for all
> the things you can do with the 3-argument version of open. For simple
> programs, it doesn't really matter. But for more complicated programs,
> the explicitness is helpful and consistency is good for
> maintainability.
>
> One slightly facetious difference: with 2-argument open you can't open
> a file named '<stupid_file_name.txt'.

Much more importantly, if someone passes you a filename of
'rm -rf /; echo |' you won't delete all your files by mistake.

Ben


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