summaryrefslogtreecommitdiff
path: root/t/op/each.t
diff options
context:
space:
mode:
authordemerphq <demerphq@gmail.com>2009-10-15 14:27:30 +0100
committerNicholas Clark <nick@ccl4.org>2009-10-15 14:27:30 +0100
commit867fa1e2da145229b4db2c6e8d5b51700c15f114 (patch)
tree83fd35002b63cf9db21ebf85cfa939ebaa370f1b /t/op/each.t
parent1c85afcecc8ee030e2780aa5bfa85692c8db64df (diff)
downloadperl-867fa1e2da145229b4db2c6e8d5b51700c15f114.tar.gz
Optimise if (%foo) to be faster than if(keys %foo)
Thread was "[PATCH] Make if (%hash) {} act the same as if (keys %hash) {}" http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-11/msg00432.html but the implementation evolved from the approach described in the subject, to instead add a new opcode pp_boolkeys, to exactly preserve the existing behaviour. Various conflicts with the passage of time resolved, 'register' removed, and a $VERSION bump.
Diffstat (limited to 't/op/each.t')
-rw-r--r--t/op/each.t37
1 files changed, 36 insertions, 1 deletions
diff --git a/t/op/each.t b/t/op/each.t
index b88f1ea825..02438f2002 100644
--- a/t/op/each.t
+++ b/t/op/each.t
@@ -6,7 +6,7 @@ BEGIN {
require './test.pl';
}
-plan tests => 42;
+plan tests => 52;
$h{'abc'} = 'ABC';
$h{'def'} = 'DEF';
@@ -193,3 +193,38 @@ for my $k (qw(each keys values)) {
eval $k;
like($@, qr/^Not enough arguments for $k/, "$k demands argument");
}
+
+{
+ my %foo=(1..10);
+ my ($k,$v);
+ my $count=keys %foo;
+ my ($k1,$v1)=each(%foo);
+ my $yes = 0;
+ if (%foo) { $yes++ }
+ my ($k2,$v2)=each(%foo);
+ my $rest=0;
+ while (each(%foo)) {$rest++};
+ is($yes,1,"if(%foo) was true");
+ isnt($k1,$k2,"if(%foo) didnt mess with each (key)");
+ isnt($v1,$v2,"if(%foo) didnt mess with each (value)");
+ is($rest,3,"Got the expect number of keys");
+ my $hsv=1 && %foo;
+ like($hsv,'/',"Got bucket stats from %foo in scalar assignment context");
+}
+{
+ our %foo=(1..10);
+ my ($k,$v);
+ my $count=keys %foo;
+ my ($k1,$v1)=each(%foo);
+ my $yes = 0;
+ if (%foo) { $yes++ }
+ my ($k2,$v2)=each(%foo);
+ my $rest=0;
+ while (each(%foo)) {$rest++};
+ is($yes,1,"if(%foo) was true");
+ isnt($k1,$k2,"if(%foo) didnt mess with each (key)");
+ isnt($v1,$v2,"if(%foo) didnt mess with each (value)");
+ is($rest,3,"Got the expect number of keys");
+ my $hsv=1 && %foo;
+ like($hsv,'/',"Got bucket stats from %foo in scalar assignment context");
+}