Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

A simple Comparison

by vtprogrammer (Friar)
on Jun 30, 2000 at 21:07 UTC ( #20627=perlquestion: print w/replies, xml ) Need Help??
vtprogrammer has asked for the wisdom of the Perl Monks concerning the following question:

How do you compare two objects. I used if ($Part eq $PN) { do stuff; } But the comparison doesn't work. I've tried using "==" but that doesn't work either. It never equals even when they are exactly the same. Any suggestions?

Replies are listed 'Best First'.
Re: A simple Comparison
by btrott (Parson) on Jun 30, 2000 at 21:11 UTC
    If you compare them using eq or ==, all you're doing is comparing their values as references. And since they're two different objects--ie. not references to the same object--they're two different references. So they won't be equal.

    To do a proper object comparison, you'll need to write a method that compares two of your objects. One possibility would be to serialize the objects and compare the serialized strings:

    my $obj1 = new Obj; my $obj2 = new Obj; ## The two objects above are now equal, let's say. ## Serialize them, then compare the serialized ## data. use Storable qw/freeze/; if (freeze($obj1) eq freeze($obj2)) { ## They're the same. }
    In fact, you could even use overload to overload the comparison operator so that the proper comparison happens automagically:
    package Obj; use Storable qw/freeze/; use overload '==' => \&compare; sub compare { return freeze($_[0]) eq freeze($_[1]) }
    Now you can just say:
    if ($obj1 == $obj2) { # They're the same. }
      This doesn't work if one or both of the objects is global, Storable::freeze will fail with "Can't store GLOB items".
        GLOB and "global" are two different things. You can easily freeze global variables, but you can't freeze typeglobs, see "Typeglobs and Filehandles" in perldata.
        use Storable; bless our $x = [], 'My::class'; print Storable::freeze($x); # No error, $x is global. bless my $y = \*STDERR, 'My::class'; print Storable::freeze($y); # Can't store GLOB items, $y is le +xical.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://20627]
Approved by root
[marto]: "Eugenie Scott, executive director of the National Center for Science Education, dubbed this approach the Gish gallop, describing it as "where the creationist is allowed to run on for 45 minutes or an hour, spewing forth torrents of error that the
[marto]: evolutionist hasn't a prayer of refuting in the format of a debate." She also criticized Gish for failing to answer objections raised by his opponents"
[erix]: one would hope evolutionists haven't any prayers anyway
[marto]: obviously someone could be religious, but not creationist
[erix]: "Nothing in Intelligent Design makes sense except in the light of Creationism" <-- I made that one up myself (free after Dobzhansky )
[erix]: yes. Deplorable marto, deplorable.
[marto]: the situation seemed similar to this one, majority of the contributrions are nonsense, doesn't address any questions ...
[marto]: meh, I've been called worse :P

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-07-28 15:45 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.