develooper Front page | perl.perl5.porters | Postings from April 2006

[PATCH] threads.pm should overload != operator

Thread Next
From:
Jan Dubois
Date:
April 3, 2006 18:06
Subject:
[PATCH] threads.pm should overload != operator
Message ID:
02fc01c65783$fe4820f0$2217a8c0@candy
The threads module overloads '==' to compare threads by thread id.
The 'fallback' option in overload.pm will only autogenerate the
'!=' operator from '<=>', but not from '==', so the module needs
to supply it explicitly.

In general I think that overload should do magic autogeneration
of '!=' from '==' and vice versa, but I don't understand the
overloading code enough to make it so.  If this is not desirable,
then maybe the module should at least generate a warning if one
of the operators is defined but not the other one?

Cheers,
-Jan

--- ext/threads/threads.pm.orig	Mon Apr 03 09:22:18 2006
+++ ext/threads/threads.pm	Mon Apr 03 17:55:53 2006
@@ -29,6 +29,7 @@
 
 use overload
     '==' => \&equal,
+    '!=' => sub { !equal(@_) },
     'fallback' => 1;
 
 BEGIN {
--- ext/threads/t/basic.t.orig	Mon Apr 03 09:22:18 2006
+++ ext/threads/t/basic.t	Mon Apr 03 17:51:45 2006
@@ -15,7 +15,7 @@
 
 use ExtUtils::testlib;
 
-BEGIN { $| = 1; print "1..30\n" };
+BEGIN { $| = 1; print "1..32\n" };
 use threads;
 
 
@@ -131,28 +131,31 @@
 my $thr2 = threads->create(sub {});
 my $thr3 = threads->object($thr1->tid());
 
-ok(20, $thr1 != $thr2,   'Treads not equal');
-ok(21, $thr1 == $thr3,   'Threads equal');
+# Make sure both overloaded '==' and '!=' are working correctly
+ok(20,   $thr1 != $thr2,  'Treads not equal');
+ok(21, !($thr1 == $thr2), 'Treads not equal');
+ok(22,   $thr1 == $thr3,  'Threads equal');
+ok(23, !($thr1 != $thr3), 'Threads equal');
 
-ok(22, $thr1->_handle(), 'Handle method');
-ok(23, $thr2->_handle(), 'Handle method');
+ok(24, $thr1->_handle(), 'Handle method');
+ok(25, $thr2->_handle(), 'Handle method');
 
-ok(24, threads->object($thr1->tid())->tid() == 11, 'Object method');
-ok(25, threads->object($thr2->tid())->tid() == 12, 'Object method');
+ok(26, threads->object($thr1->tid())->tid() == 11, 'Object method');
+ok(27, threads->object($thr2->tid())->tid() == 12, 'Object method');
 
 $thr1->join();
 $thr2->join();
 
-my $sub = sub { ok(26, shift() == 1, "Test code ref"); };
+my $sub = sub { ok(28, shift() == 1, "Test code ref"); };
 threads->create($sub, 1)->join();
 
 my $thrx = threads->object(99);
-ok(27, ! defined($thrx), 'No object');
+ok(29, ! defined($thrx), 'No object');
 $thrx = threads->object();
-ok(28, ! defined($thrx), 'No object');
+ok(30, ! defined($thrx), 'No object');
 $thrx = threads->object(undef);
-ok(29, ! defined($thrx), 'No object');
+ok(31, ! defined($thrx), 'No object');
 $thrx = threads->object(0);
-ok(30, ! defined($thrx), 'No object');
+ok(32, ! defined($thrx), 'No object');
 
 # EOF
End of Patch.



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About