Re: Variable Interpolation
by Tanktalus (Canon) on Apr 29, 2005 at 16:50 UTC
|
To be honest, interpolating strings is best done in a format slightly unlike perl. For example, using Text::Template or something. However, if you can completely trust your input to not do something silly (for example, it's written by you or a teammate, and not coming in from a web user or marketing), your method is fine.
my $interpolated = eval(qq("$need_to_interpolate"));
Slightly simpler, slightly easier to read, IMO. | [reply] [d/l] |
Re: Variable Interpolation
by dragonchild (Archbishop) on Apr 29, 2005 at 15:47 UTC
|
print "smell my $string\n";
The Perfect is the Enemy of the Good.
| [reply] [d/l] |
|
|
I think I'm being stupid, but not that stupid :-). I probably should have mentioned that I'm bringing in the value in $need_to_interpolate from a file. I can't just code it like that.
| [reply] |
Re: Variable Interpolation
by ikegami (Patriarch) on Apr 29, 2005 at 17:07 UTC
|
my $string = "cheese";
my $need_to_interpolate = 'smell my "$string"\n';
$need_to_interpolate =~ s/"/\\"/g;
print eval(qq{"$need_to_interpolate"});
| [reply] [d/l] |
Re: Variable Interpolation
by salva (Canon) on Apr 29, 2005 at 16:04 UTC
|
I suppose that simple interpolation "as in $foo" is not what you are looking for, then you can try something simple as:
$string=~s/(\$\w+)/$1/eeg
that will interpolate simple vars as $foo but nothing fancier (say $_->4)... though you can improve the regexp to match more complex expressions. | [reply] [d/l] |
|
|
It also won't handle
$string = '\\$var'; # escaped '$'
and
$string = '${foo}bar'; # $foo, not $foobar
which serve necessary functions (unlike
$string = '$_->[4]';
which is just a feature).
As a bonus, I've added support for \n and \t. It's easy to add the more escapes.
The following code does, and like yours, limits the strings being eval'ed to a minumum.
You can even get rid of eval completely if you don't interpolate from lexicals:
All told, though, it's safer and cleaner to only interpolate from variables in hash:
our %ESCAPES = (
n => "\n",
t => "\t",
);
sub interpolate {
local *_ = \$_[0]; # Alias $_ to $_[0].
my $symtab = $_[1];
my $interpolated = '';
for (;;) {
if (/\G \$(\w+) /gcsx || /\G \${(\w+)} /gcsx) {
if (!exists($symtab->{$1})) {
$interpolated .= "[unknown symbol \$$1]";
} elsif (!defined($symtab->{$1})) {
$interpolated .= "[undefined symbol \$$1]";
} else {
$interpolated .= $symtab->{$1};
}
next;
}
if (/\G \\(.) /gcsx) {
$interpolated .= exists($ESCAPES{$1}) ? $ESCAPES{$1} : $1;
next;
}
/\G
(
. # Catchall.
(?: # These four lines are optional.
(?!\\) # They are here to speed things up
(?!\$) # by avoiding adding individual
.)* # characters to the $interpolated.
)
/gcsx && do { $interpolated .= $1; next; };
last;
}
return $interpolated;
}
my %symtab = (
string => "cheese",
#user => $user,
#...
);
my $need_to_interpolate = 'smell my $string\n';
print interpolate($need_to_interpolate, \%symtab);
| [reply] [d/l] [select] |
Re: Variable Interpolation
by trammell (Priest) on Apr 29, 2005 at 19:03 UTC
|
my $string = "cheese";
my $need_to_interpolate = "smell my %s\n";
printf $need_to_interpolate, $string;
| [reply] [d/l] |
|
|
ok, i'll bite. certainly running "perl -d cheese.pl" didn't educate me much....
debugger output:
...
main::(cheese.pl:4): my $need_to_interpolate = "smell my %s\n";
DB<1> s
main::(cheese.pl:5): printf $need_to_interpolate, $string;
DB<1> x %s
empty array
....
How does the empty hash work, here?
| [reply] [d/l] |
|
|
Perl's "sprintf" permits the following universally-known
conversions:
%% a percent sign
%c a character with the given number
%s a string
...
| [reply] |
|
|
Re: Variable Interpolation
by chas (Priest) on Apr 29, 2005 at 15:49 UTC
|
my $string = "cheese";
my $need_to_interpolate = "smell my $string\n";
chas
| [reply] [d/l] |