Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Evaluating environment variables

Reply
Thread Tools

Evaluating environment variables

 
 
prattm@gmail.com
Guest
Posts: n/a
 
      06-14-2006
I am reading in a config file that has a bunch of absolute file paths,
most with embedded environment variables, such as:

${HOME}/bin/myscript.pl

To ultimate goal is to do some file tests on these items, specifically
readlink, but I can't because of the embedded env variables. So I
thought I would try a nifty little search and replace:

$file =~ s/\${/\$ENV{/;

The goal being the replace all occurences of "${" with "$ENV{" so that
Perl will resolve the env variables for me using the ENV hash. The
search and replace works, but I still get errors passing in the new
variable to readlink because Perl is not evaluating the $ENV{...}
substrings first.

Any ideas of how to get around this? My fallback is to just do
something like

$file = `echo $file`;

But this seems a bit goofy to me and it seems like there ought to be a
way to do in Perl.

Thanks, Mike

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      06-14-2006
>>>>> "GJ" == Glenn Jackman <(E-Mail Removed)> writes:

GJ> At 2006-06-14 05:49PM, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
>> I am reading in a config file that has a bunch of absolute file paths,
>> most with embedded environment variables, such as:
>>
>> ${HOME}/bin/myscript.pl


GJ> Assuming you have braces around all your variable names:

GJ> s/\${(.*?)}/$ENV{$1}/eg

you don't need /e as the replacement there is a valid string and doesn't
need to be evaled. and you will likely need to escape the { as perl will
interpret {} as a quantifier. i haven't tested that and i leave that as
an exercise to the OP.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
 
prattm@gmail.com
Guest
Posts: n/a
 
      06-19-2006
Thanks for the replies. Glenn's suggestion did work as is, although I
have yet to try it without the /e modifier.

Mike


Uri Guttman wrote:
> >>>>> "GJ" == Glenn Jackman <(E-Mail Removed)> writes:

>
> GJ> At 2006-06-14 05:49PM, (E-Mail Removed) <(E-Mail Removed)> wrote:
> >> I am reading in a config file that has a bunch of absolute file paths,
> >> most with embedded environment variables, such as:
> >>
> >> ${HOME}/bin/myscript.pl

>
> GJ> Assuming you have braces around all your variable names:
>
> GJ> s/\${(.*?)}/$ENV{$1}/eg
>
> you don't need /e as the replacement there is a valid string and doesn't
> need to be evaled. and you will likely need to escape the { as perl will
> interpret {} as a quantifier. i haven't tested that and i leave that as
> an exercise to the OP.
>
> uri
>
> --
> Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
> Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
70-284 Lab Environment, Need Virtual Environment brooklynbridge508@hotmail.com MCSA 4 05-02-2007 09:49 AM
Setting an environment variable from another environment variable marcwentink@hotmail.com Java 5 04-04-2007 10:39 PM
Evaluating nester variables Geoff Perl Misc 11 11-14-2003 02:57 AM
Evaluating url trust level Hagay Lupesko ASP .Net 0 10-19-2003 07:27 PM



Advertisments