summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGurusamy Sarathy <gsar@cpan.org>1998-08-08 03:33:33 +0000
committerGurusamy Sarathy <gsar@cpan.org>1998-08-08 03:33:33 +0000
commit3f943bd979a211fe18bd0f4528b190c72f7180e1 (patch)
treeb1de7a9b581a13e8fe16dfcc507c5bc9483b4a11
parent4f7e78b905cd2e1ff7ceb03a8e8b0cc49a57f5ae (diff)
downloadperl-3f943bd979a211fe18bd0f4528b190c72f7180e1.tar.gz
prevent lexical leaks from Benchmark into target code (inspired by
an attempt by John Allen) p4raw-id: //depot/maint-5.005/perl@1757
-rw-r--r--lib/Benchmark.pm22
1 files changed, 17 insertions, 5 deletions
diff --git a/lib/Benchmark.pm b/lib/Benchmark.pm
index f490998039..a28f510d11 100644
--- a/lib/Benchmark.pm
+++ b/lib/Benchmark.pm
@@ -238,6 +238,13 @@ functionality.
=cut
+# evaluate something in a clean lexical environment
+sub _doeval { eval shift }
+
+#
+# put any lexicals at file scope AFTER here
+#
+
use Carp;
use Exporter;
@ISA=(Exporter);
@@ -280,7 +287,7 @@ sub real { my($r,$pu,$ps,$cu,$cs) = @{$_[0]}; $r ; }
sub timediff {
my($a, $b) = @_;
my @r;
- for ($i=0; $i < @$a; ++$i) {
+ for (my $i=0; $i < @$a; ++$i) {
push(@r, $a->[$i] - $b->[$i]);
}
bless \@r;
@@ -329,10 +336,15 @@ sub runloop {
last if $pack ne $curpack;
}
- my $subcode = (ref $c eq 'CODE')
- ? "sub { package $pack; my(\$_i)=$n; while (\$_i--){&\$c;} }"
- : "sub { package $pack; my(\$_i)=$n; while (\$_i--){$c;} }";
- my $subref = eval $subcode;
+ my ($subcode, $subref);
+ if (ref $c eq 'CODE') {
+ $subcode = "sub { for (1 .. $n) { local \$_; package $pack; &\$c; } }";
+ $subref = eval $subcode;
+ }
+ else {
+ $subcode = "sub { for (1 .. $n) { local \$_; package $pack; $c;} }";
+ $subref = _doeval($subcode);
+ }
croak "runloop unable to compile '$c': $@\ncode: $subcode\n" if $@;
print STDERR "runloop $n '$subcode'\n" if $debug;