|
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
|