[Kde-perl] Overloading of operators '==' and '!='
Johannes Plass
johannes.plass at web.de
Sun May 23 22:26:50 CEST 2004
> ...
> if($list[$i] eq $mypoint) { ...
> ...
Yep, this works :-)
But (and there always has to be a 'but' :-)
it shows that the meaning of '==' and 'eq'
operators is opposite to what it should be:
for Perl strings
'==' does reference comparison,
'eq' does content comparison
for (some) PerlQt Objects
'==' does content comparison,
'eq' (accidentally) does reference comparison
Things may be reverted to what they ought to be
by changing the overloading in Qt.pm
from '==' and '!=' to 'eq' and 'ne'.
That is, change
"==" => "Qt::base::_overload::op_equal",
"!=" => "Qt::base::_overload::op_not_equal",
to
"eq" => "Qt::base::_overload::op_equal",
"ne" => "Qt::base::_overload::op_not_equal",
For the test script below, which tests some Qt::Point
expressions, this gives the desired result
$p1 == $p1 .... yes
$p1 == $p2 .... no
$p1 == $p3 .... no
$p1 != $p1 .... no
$p1 != $p2 .... yes
$p1 != $p3 .... yes
$p1 eq $p1 .... yes
$p1 eq $p2 .... no
$p1 eq $p3 .... yes
$p1 ne $p1 .... no
$p1 ne $p2 .... yes
$p1 ne $p3 .... no
Well, might this be a solution ?
Johannes Plass
------------------------------------------------------
use Qt;
my $p1 = Qt::Point(1,0);
my $p2 = Qt::Point(2,0);
my $p3 = Qt::Point(1,0);
sub test { printf("%s .... %s\n",$_[0], eval $_[0] ? "yes" : "no"); }
test '$p1 == $p'.$_ foreach (1,2,3);
test '$p1 != $p'.$_ foreach (1,2,3);
test '$p1 eq $p'.$_ foreach (1,2,3);
test '$p1 ne $p'.$_ foreach (1,2,3);
More information about the Kde-perl
mailing list