Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Equality operators

by rhesa (Vicar)
on Apr 26, 2006 at 11:37 UTC ( [id://545768]=note: print w/replies, xml ) Need Help??


in reply to Equality operators

Just to confuse matters a bit, because Perl lets me:
#!/usr/bin/perl use strict; no warnings; use Scalar::Util qw/ dualvar /; my $foo = dualvar 1, "foo"; my $bar = dualvar 2, "foo"; print "Using dualvar$/"; print '$foo', ($foo eq $bar ? " eq " : " ne "), '$bar', $/; print '$foo', ($foo == $bar ? " == " : " != "), '$bar', $/; __END__ Using dualvar $foo eq $bar $foo != $bar

Using overload could result in similarly confusing comparisons. This isn't as contrived as it sounds:

#!/usr/bin/perl use strict; use warnings; use URI; my $foo = URI->new('http://www.perlmonks.org'); my $bar = URI->new('http://www.perlmonks.org'); print "Using URI$/"; print '$foo', ($foo eq $bar ? " eq " : " ne "), '$bar', $/; print '$foo', ($foo == $bar ? " == " : " != "), '$bar', $/; __END__ Using URI $foo eq $bar $foo != $bar

URI defines an overload for stringification, but not for numification. In numerical context, objects evaluate to their location in memory. And the two different objects are stored in different locations, so == returns false.

So be careful with == and eq :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://545768]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-26 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found