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.
| [reply] [d/l] |
| [reply] [d/l] |
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 | [reply] [d/l] [select] |
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;
| [reply] [d/l] [select] |
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 | [reply] [d/l] [select] |