Evaluating environment variables

Discussion in 'Perl Misc' started by prattm@gmail.com, Jun 14, 2006.

  1. Guest

    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
     
    , Jun 14, 2006
    #1
    1. Advertising

  2. Uri Guttman Guest

    >>>>> "GJ" == Glenn Jackman <> writes:

    GJ> At 2006-06-14 05:49PM, <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jun 14, 2006
    #2
    1. Advertising

  3. Guest

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

    >
    > GJ> At 2006-06-14 05:49PM, <> 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 ------ -------- http://www.stemsystems.com
    > --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    > Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    , Jun 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hagay Lupesko

    Evaluating url trust level

    Hagay Lupesko, Oct 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    Hagay Lupesko
    Oct 19, 2003
  2. microsoft.private.windows.netserver.setup

    Evaluating Formulas in ASP.NET

    microsoft.private.windows.netserver.setup, Mar 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    379
    Steve C. Orr [MVP, MCSD]
    Mar 4, 2004
  3. Replies:
    5
    Views:
    660
  4. Replies:
    9
    Views:
    957
  5. Geoff

    Evaluating nester variables

    Geoff, Nov 13, 2003, in forum: Perl Misc
    Replies:
    11
    Views:
    153
    Uri Guttman
    Nov 14, 2003
Loading...

Share This Page