in reply to Class::Std problem

Your issue isn't specific to Class::Std objects. In Perl, the default stringification of an object contains its class name and its memory address, so different instances will generate a different string.

If you want to use eq, you should overload stringification ('""') or comparison (cmp). See overload for details.

Another approach would be to implement an equals method, and check if( $manager1->equals( $manager2 ) ).

Replies are listed 'Best First'.
Re^2: Class::Std problem
by herveus (Prior) on Dec 21, 2006 at 12:23 UTC
    Howdy!

    I take the question to be "how do I fix my 'new' so the code snipped works right?" If new({ id => 1 }) returns the same thing each time, the comparison should work fine. The trick is to get 'new' to not only construct one object for each distinct value of id that it sees.

    yours,
    Michael

      This was more along the lines of what I was looking for. I must have not made the problem clear enough because most of the posts focus upon the comparison aspect rather than the object generation.

      I use Class::Std to generate new objects. However, if I am getting an object that already exists, I wanted to return the existing object, not generate a new one for comparison. So in my example, if $manager1 was this 'Manager=SCALAR(0x83ef950)' then $manager2 would return the reference.

      After sleeping on it, I think that I just have to sub-class Class::Std that keeps track of all the existing objects and returns that object if it already exists.