Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: SV creation inside XS

by llancet (Friar)
on Nov 09, 2011 at 01:53 UTC ( [id://936922]=note: print w/replies, xml ) Need Help??


in reply to Re^3: SV creation inside XS
in thread SV creation inside XS

Is it harmful if I bless a blessed object? If it is, I need some way to know whether my referent is blessed. However, "sv_isobject" works on a reference. Is there any similar thing works on a referent?

Replies are listed 'Best First'.
Re^5: SV creation inside XS
by BrowserUk (Patriarch) on Nov 09, 2011 at 03:41 UTC
    If it is, I need some way to know whether my referent is blessed. However, "sv_isobject" works on a reference. Is there any similar thing works on a referent?

    You'll kind of have to clarify what you mean here by "reference" and "referent".

    Warning: Blessing involves magic, and I get very antsy around anything to do with (XS) magic.

    When you "bless a reference", the magic is attached to the thing referenced, not the reference itself, despite that you need a reference to perform the blessing.

    My evidence for this:

    #! perl -slw use strict; my( $scalar, @array, %hash ); print for \( $scalar, @array, %hash ); =comment Prints SCALAR(0x2a7c20) ARRAY(0x2a7c80) HASH(0x2a7d28) =cut bless $_, 'fred' for \( $scalar, @array, %hash ); print for \( $scalar, @array, %hash ); =comment Prints fred=SCALAR(0x2a7c20) fred=ARRAY(0x2a7c80) fred=HASH(0x2a7d28) =cut my $newref = \$scalar; print $newref; =comment Prints fred=SCALAR(0x2a7c20) =cut my $ref2ref = \\$scalar; print $ref2ref; print $$ref2ref; =comment Prints REF(0x6e148) fred=SCALAR(0x2a7c20) =cut

    Notice how $newRef, and the referent of $ref2ref know that it is the thing they are pointed at that is blessed.

    Take that one step further and assign a reference to a new, unblessed thing to a previously blessed reference, and it now knows it refers to a unblessed thing:

    $newref = \ 'fred'; print $newref;; =comment Prints SCALAR(0x2c7d70) =cut

    So, the common speech pattern of "a blessed reference" is really a misnomer. What we actually have is "a reference to a blessed thing".

    Turning this back to your statement above, sv_isobject() is defined as:

    Returns a boolean indicating whether the SV is an RV pointing to a blessed object. If the SV is not an RV, or if the object is not blessed, then this will return false.

    Only the referent is blessed, but you have to pass an RV that references it to test whether it is blessed or not.

    (Doesn't make a whole bunch of sense to me, but I told you I get antsy around magic. :)

    And finally:

    Is it harmful if I bless a blessed object?

    Re-blessing an object just replaces the package it is blessed into with the new package (or the same). But the old blessing is forgotten. Whether this is a mistake depends upon what you are trying to achieve.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I think this is very inconvenient: in perlapi, all blessing tools work with the scalar containing the referent, but not the thing being referenced, like the "sv_isobject" you mentioned above.
      I'm asking about: whether there are tools work on the referent, other than the wrapping RV. Otherwise I have to create a temporary wrapper each time I need such operation, which seemingly not quite necessary:
      HV* my_thing = something->get_from_somewhere(); // test if my_thing is blessed SV* tmp = newRV_noinc((SV*)my_thing); if (sv_isobject(tmp)) { // blah blah blah } SvREFCNT_dec(tmp); // I hate that!!!
        Otherwise I have to create a temporary wrapper each time I need such operation, which seemingly not quite necessary:

        As I said: "(Doesn't make a whole bunch of sense to me,".

        In theory, you should be able to test the appropriate flag bit directly:

        0x00100000 SVs_OBJECT

        This flag is set when the object is "blessed". It can only be set for value type SvPVMG or subtypes of it. This flag also indicates that the STASH pointer is valid and points to a namespace HV.

        But I cannot find any macro for accessing it, and direct access is frowned upon. That said, it seems unlikely that the flags word is going to change much, so it is a possibility.

        But that said, the way you will normally gain access to your objects is through your functions having been passed an argument. Which will usually, if not always, be an RV that references it. In that case, you wouldn't have to create another Rv as you shoudl still have access to the one passed to you.

        The only other case I can think of is when the object has just been created (ie. in new() or equivalent.), at which point you will (should) know whether you have already blessed it. Also, you would have to create an RV before returning it to the caller, so there would be no need for a temporary one.

        I think that to make headway, you would have to show the circumstances (ie.code) where have the object but no RV and need to determine if it has already been blessed?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: SV creation inside XS
by ikegami (Patriarch) on Nov 09, 2011 at 06:09 UTC

    You could create a reference, then destroy it.

    SV * rv = SvRV_noinc(sv); int blessed = sv_isobject(rv); SvREFCNT_dec(rv);

    or you could use the equivalent

    int blessed = SvOBJECT(sv);

    I don't know if that's kosher, but I don't see it breaking.

Log In?
Username:
Password:

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

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

    No recent polls found