Friday, January 22. 2010

Object identity in Perl

I ran across a surprising weakness in Perl, regarding object identity. I was writing a function to handle XMsgs, which are messages used by the Amanda transfer architecture to indicate the progress of a transfer. Messages sent from any transfer element are delivered to the same handler, and in this case I needed to know which element had sent the message. Fortunately, XMsg objects have a elt attribute pointing to the sending element.

Now, in Python, I would write,

  def handle_xmsg(xmsg):
    if xmsg.elt is interesting_elt:
      # handle message from interesting_elt
    else:
      # handle message from other elements

where the is operator tests for object identity. So how do I do this in perl? My first thought was to use eq, since the string interpolation of a hashref seems to have an object identifier in it: HASH(0x711210). I soon learned, in #perl, that == would work better, as the hashref's address is used as its integer value, and this conversion is much faster than stringification.

However, the element objects in this particular case are SWIG objects, and furthermore they use overload to implement a user-readable stringification. So neither technique worked.

What I did end up doing was this:

I overrode the == operator, and wrote a short comparison function in C to compare the un-SWIGged, un-magicked, un-blessed object pointers. Whew!


Trackbacks

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Markdown format allowed
 
 

Notice

The postings on this site are my own and don't necessarily represent the opinions of my employer, or anyone else.