cjbailey1 has asked for the wisdom of the Perl Monks concerning the following question:

I have read in a series of lines in from a file but need to be able to interpolate these strings, how do I go about doing the interpolation?

eg:
$ENV{VARIABLE}/my/path
should interpolate to
p:/my/path

Many thanks guys,
Chris

Replies are listed 'Best First'.
Re: String Interpolation on single quote string?
by hardburn (Abbot) on May 18, 2005 at 17:03 UTC

    Use string eval:

    my $code = '$ENV{VARIABLE}/my/path'; eval $code;

    Be very, very careful with this. Eval is like a Ring of Power: you can use it to get out of a bad situation, but you'll be attracting evil in the process.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: String Interpolation on single quote string?
by Fletch (Bishop) on May 18, 2005 at 17:03 UTC

    perldoc -q "expand variables"

Re: String Interpolation on single quote string?
by davidrw (Prior) on May 18, 2005 at 17:03 UTC
    What are some of the lines in the line and what (how) were they read into? Just "$ENV{VARIABLE}/my/path" or the like ($ENV{VARIABLE}."/my/path" or sprintf "%s/my/path", $ENV{VARIABLE}) isn't working for you?

    Update: Ah -- as i look as hardburn's response below, now it makes sense to me that a sample line was '$ENV{VARIABLE}/my/path' ... so, yeah, eval can work, but can also be very bad, as this thread recently discussed
Re: String Interpolation on single quote string?
by TedPride (Priest) on May 18, 2005 at 17:51 UTC
    You can also use regex, if you know the format the interspersed variables will be in. For instance:
    $ENV{VARIABLE} = '/base/path'; $_ = '$ENV{VARIABLE}/my/path'; s/\$ENV\{(\w+?)\}/$ENV{$1}/g; print;
    Or even the following, if you don't mind breaking some rules:
    $ENV{VARIABLE} = '/base/path'; $path = 'path'; $_ = '$ENV{VARIABLE}/my/$path'; s/\$(\w+)\{(\w+?)\}/${$1}{$2}/g; s/\$(\w+)/${$1}/g; print;
      Thanks for all the help guys, I have had a play and because I want to be able to put a number of different commands in without worrying, and the format is something that I can set myself for the input file I've opted for the file containing any commands, that need to be parsed, within a set of asterisks. The asterisk is not needed anywhere else within the file so I have decided on using the following code:
      sub ParseArguments { # Parses any perl commands with * delimeters, eg: + '*$ENV{CONFIG_DIR}*/contents' --> 'MY/CONFIG/DIR/contents' my $Line = pop @_; my @Args = split /\*/, $Line; foreach my $Arg (@Args) { if ($Arg ne "") { if ($Arg =~ /^[\@\$\%]/) { my $Temp; $Arg = "\$Arg = $Arg"; eval $Arg; $Arg =~ s/^\$Arg = //; } } } my $Return = ""; foreach my $Arg (@Args) { $Return = $Return . $Arg; } return $Return; }

      The contents of the file can now contain a number of different items that I need, examples are:
      *$ENV{COMPANY}*/Custom_Perl/Logs *$Config->{PerlDir}*/Lib/Chris/Pantheon/Border.CoOrds

      I know this isn't the best way for me to do this as it leaves all kinds of security vulnerabilities etc, but I'm not worried about those as the program ultimately is run by me and a few other people who don't have any knowledge of perl anyway!
      Thanks Again Guys,
      Chris Bailey
A reply falls below the community's threshold of quality. You may see it by logging in.