in reply to Resolving Imbedded Variables
Here's one way...
In other words, just take your string and eval it as a double quoted string.my $string = 'foo: $foo and bar: $bar\n'; print $string, "\n"; my $foo = "foozleberry pie"; my $bar = "barzleberry foo"; print eval 'qq(' . $string . ')';
Another technique which is a little less prone to error is to use a hash and perform a substitution on it. In this case, your placeholders only look like perl variables...
my %vars = ( foo => 'foozleberry pie', bar => 'barzleberry foo', ); my $string = 'foo: $foo and bar: $bar'; $string =~ s/\$(\w+)/$vars{$1}/g; print $string, "\n";
-sauoq "My two cents aren't worth a dime.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Resolving Imbedded Variables
by Roy Johnson (Monsignor) on Nov 07, 2005 at 18:03 UTC |