in reply to String Interpolation on single quote string?

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;

Replies are listed 'Best First'.
Re^2: String Interpolation on single quote string?
by cjbailey1 (Initiate) on May 19, 2005 at 10:22 UTC
    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