<html>
At 11:23 PM 12/20/03 -0800, Fred Morris wrote:<br>
<blockquote type=cite class=cite cite>Interestingly, my brain-dead
workaround seems to perform slightly better<br>
than eval for a smallish number of substitutions; it also doesn't
kick<br>
errors when a variable isn't defined (it just leaves it alone). (8
seconds<br>
versus 10 seconds)<br>
</blockquote><br>
[snip]<br>
<br>
<blockquote type=cite class=cite cite> for (my $i = 0; $i <
20000; $i++) {<br>
my $s = eval ' my $s = "' . $source .
'";';<br>
}<br>
</blockquote><br>
You are doing "my $s" twice in the same lexical context.
Additionally, looking at 'perldoc -f eval' reveals <br>
<br>
> In both forms, the value returned is the value of the last<br>
> expression evaluated inside the mini-program;<br>
<br>
So you really only need <br>
<br>
<x-tab> </x-tab>my $s =
eval qq["$source"];<br>
<br>
Further reading of the perldoc tells us that in this form, the EXPR is
parsed every time the statement executes. That is where you get the
slowdown. But that also is where you get the ability to expand
variables of which you don't know the names, as Brian has already pointed
out. I tried playing around with a regex kind of like
"s/\$(\w+)/$$1/g" (turning off strict) but it was not
interpolating correctly. The only way it will work is like
so:<br>
<dl>
<dl>
<dd>#! /usr/bin/perl<br>
<br>
<dd>$foo_str = "foo";
<dd>$bar_str = "bar";<br>
<br>
<dd>my $foo = 'substitute $foo_str for $bar_str';<br>
<br>
<dd>my $s = "";
<dd>print "go?>";
<dd>$line = <>;
<dd>for (my $i = 0; $i<2000; $i++) {
<dd> $s = eval {
<dd> my $str = $foo;
<dd> $str =~ s/\$(\w+)/$$1/g;
<dd> return $str;
<dd> };
<dd>}
<dd>print "\$s = $s;\n";<br>
<br>
</dl>
</dl>I don't know exactly why, but putting "my" on $foo_str and
$bar_str caused things to break. I know it has to do with scope
some-how, but I don't know the exact reason. So, if you want fast,
but completely non-strict, there's a solution for you.<br>
<br>
Ivan</html>