Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^10: odd line in windows

by ikegami (Patriarch)
on Sep 08, 2011 at 22:22 UTC ( [id://924961]=note: print w/replies, xml ) Need Help??


in reply to Re^9: odd line in windows
in thread odd line in windows

I stand corrected. The code in the recipe is actually correct.

SV* obj_ref = newSViv(0); SV* obj = newSVrv(obj_ref, class);

newSVrv doesn't create a reference as a I had guessed, it returns a new SV and makes obj_ref a reference to it. WTF? How odd is that!!!

So obj_ref holds a reference to the blessed scalar in obj, which in turn contains an integer which corresponds to the memory address of the C object.

set_iv is not being used on the reference, so there are no issues wrt to recent changes in scalar types.

Replies are listed 'Best First'.
Re^11: odd line in windows
by BrowserUk (Patriarch) on Sep 08, 2011 at 22:29 UTC
    newSVrv doesn't create a reference as a I had guessed, it returns a new SV and makes obj_ref a reference to it. WTF? How odd is that!!!

    Very odd!

    There's nothing like consistency:

    1. newSViv: Creates a new SV and copies an integer into it
    2. newSVnv: Creates a new SV and copies a floating point value into it.
    3. newSVpv: Creates a new SV and copies a string into it.
    4. newSVrv: Creates a new SV for the RV, rv, to point to.

    And that's nothing like consistent! newRVsv() would make more sense (IMO).


    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.

      Here's an alternative way of writing the function that might be clearer.

      SV* new(const char * classname, const char * name, const char * rank, +long serial) { Soldier * soldier; SV * obj; SV * obj_ref; New(42, soldier, 1, Soldier); soldier->name = savepv(name); soldier->rank = savepv(rank); soldier->serial = serial; obj = newSViv((IV)soldier); obj_ref = newRV_noinc(obj); sv_bless(obj_ref, gv_stashpv(classname, GV_ADD)); SvREADONLY_on(obj); return obj_ref; // Inline will mortalize. }

        Indeed. That is much clearer.

        syphilis if you are following along, it would be worth updating the POD with that example


        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found