Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^3: Inline::C-Cookbook's "Object Oriented Inline" (bless'd ref)

by sfink (Deacon)
on Sep 06, 2007 at 18:18 UTC ( #637492=note: print w/replies, xml ) Need Help??

in reply to Re^2: Inline::C-Cookbook's "Object Oriented Inline" (bless'd ref)
in thread Inline::C-Cookbook's "Object Oriented Inline"

In Perl, take two references to the same variable. Bless one. Note that the other reference is now blessed. Conclude that the blessed state is stored in the referenced variable not in the reference (not that surprising).

I proved it to myself by blessing a reference in Perl then passing it to dump_sv. My question is "why?"

The semantic difference is exactly what you said -- the blessing is shared by all references to the value. But I still don't understand why those semantics are more useful than the alternative. I'm not claiming that it should be the other way around, just that I personally don't know the reason why it is one way and not the other.

Thinking through it, I realize that if it were the other way around (blessed references were actually blessed references), then you could have the same data item with multiple references pointing to it, each blessed with a different package. If each of them defines a DESTROY, which DESTROY should get called? Or more to the point, when should DESTROY be called? The way it is now (blessed referents), DESTROY is called when the last reference to the data item goes away, which makes a lot of sense. If references were blessed, then you would have to call DESTROY whenever any of the references themselves went away, which would result in multiple DESTROYs getting called for the same data item (each with a different package). And destroying references isn't really that interesting anyway (they don't really have any data to diddle with); destroying the data item is.

Ok, maybe I do understand the "why?", now.

  • Comment on Re^3: Inline::C-Cookbook's "Object Oriented Inline" (bless'd ref)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://637492]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2017-04-26 00:39 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (466 votes). Check out past polls.