[Edinburgh-pm] Spot the bug

Miles Gould miles at assyrian.org.uk
Sun Apr 28 10:57:24 PDT 2013


Hi all,

I recently encountered some Perl behaviour that surprised me, and 
thought you might be amused and/or able to provide further insight.

Here's the buggy function:

sub is_unate {
     my ($self, $idx) = @_;
     return ($self->true_count($idx) > 0) xor ($self->false_count($idx) 
 > 0);
}

and here are the tests which allowed me to diagnose the problem:

is($cubes->true_count(1), 1);
is($cubes->true_count(2), 1);
is($cubes->true_count(3), 1);
is($cubes->true_count(4), 1);
is($cubes->true_count(5), 1);

is($cubes->false_count(1), 1);
is($cubes->false_count(2), 0);
is($cubes->false_count(3), 1);
is($cubes->false_count(4), 1);
is($cubes->false_count(5), 0);

ok(!$cubes->is_unate(1));    # fails
ok($cubes->is_unate(2));
ok(!$cubes->is_unate(3));    # fails
ok(!$cubes->is_unate(4));    # fails
ok($cubes->is_unate(5));

All tests pass apart from the ones marked "fails".

So, my challenges:

1) Can you spot the bug in my code, and find a fix for it?
2) Can you explain why it makes sense for Perl to behave this way?

Miles


More information about the Edinburgh-pm mailing list