Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by tye (Sage)
on Sep 06, 2007 at 02:45 UTC ( [id://637310]=note: print w/replies, xml ) Need Help??


in reply to Re: Inline::C-Cookbook's "Object Oriented Inline"
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).

In Perl, it is useless to bless something unless you have a reference to it; so blessing is done through the reference. In XS, the blessing API doesn't have the extra derefence cycle built into it, understandably.

- tye        

Replies are listed 'Best First'.
Re^3: Inline::C-Cookbook's "Object Oriented Inline" (bless'd ref)
by sfink (Deacon) on Sep 06, 2007 at 18:18 UTC
    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-28 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found