in reply to More Efficient Than Eval
An idea that might or might not work, depending on how similar and consistent your "perl-strings" are:
my %dbhash = ( name => 'revdiablo', prize => 'pony', ); my $template = 'Hello $dbhash{name}, you won a $dbhash{prize}.'; #$template =~ s/\$dbhash{([^}]+)}/$dbhash{$1}/ge; # unnecessary $template =~ s/\$dbhash{([^}]+)}/$dbhash{$1}/g; print "$template\n";
Update: of course, you might notice that this too uses eval. It just does it in a more constrained manner. I don't know how the difference in performance will work out, but I think it's much less dangerous in any event.
Another Update: aye, I just realized the /e is unnecessary, as the replacement part of a s/// is already interpolated as a double-quoted string. It can simply be s/\$dbhash{([^}]+)}/$dbhash{$1}/g.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: More Efficient Than Eval
by dave_the_m (Monsignor) on Aug 19, 2005 at 10:52 UTC |