s***@optusnet.com.au
2007-08-29 02:23:02 UTC
Hi,
The Inline::C cookbook presents an example that contains the following new() function which creates the Soldier object:
----------------------------------
SV* new(char* class, char* name, char* rank, long serial) {
Soldier* soldier;
New(42, soldier, 1, Soldier);
SV* obj_ref = newSViv(0);
SV* obj = newSVrv(obj_ref, class);
soldier->name = savepv(name);
soldier->rank = savepv(rank);
soldier->serial = serial;
sv_setiv(obj, (IV)soldier);
SvREADONLY_on(obj);
return obj_ref;
}
---------------------------------
A while back, at http://www.mail-archive.com/***@perl.org/msg01370.html Ken Williams suggested the following (more succinct) rewrite:
---------------------------------
SV* new(char* class, char* name, char* rank, long serial) {
Soldier* soldier;
New(42, soldier, 1, Soldier);
soldier->name = savepv(name);
soldier->rank = savepv(rank);
soldier->serial = serial;
return sv_setref_pv(newSViv(0), class, (void *)soldier);
}
----------------------------------
But the 2 versions of new() return slightly different objects. The original (cookbook) version returns this:
----------------------------------
SV = PVIV(0x2f5b8c) at 0x2f4940
REFCNT = 1
FLAGS = (PADBUSY,PADMY,ROK)
IV = 0
RV = 0x2f3ca4
SV = PVMG(0x2febbc) at 0x2f3ca4
REFCNT = 1
FLAGS = (OBJECT,IOK,READONLY,pIOK)
IV = 3143860
NV = 0
PV = 0
STASH = 0x45fca8 "Soldier"
PV = 0x2f3ca4 ""
CUR = 0
LEN = 0
----------------------------------
But, in Ken's rewrite, that READONLY flag is absent in the returned object.
1) What are the likely/possible ramifications of that READONLY flag being unset ? (I note that the cookbook version explicitly sets that READONLY flag ... presumably for a good reason.)
2) How does one modify Ken's rewrite so that it sets that READONLY flag ?
I recently asked about this at perlmonks ( http://www.perlmonks.org/index.pl?node_id=635113 ) but didn't receive any responses on the matter.
It's doubtful that I really *need* to know the answers (as I'm quite happy to stick with the cookbook approach) but I am, nevertheless, curious about it.
Note: I've also changed a couple of things in the versions of new() that I've presented above. The original versions used strdup(), which I've replaced with savepv() - and they used malloc(), which I've replaced with new().
Cheers,
Rob
The Inline::C cookbook presents an example that contains the following new() function which creates the Soldier object:
----------------------------------
SV* new(char* class, char* name, char* rank, long serial) {
Soldier* soldier;
New(42, soldier, 1, Soldier);
SV* obj_ref = newSViv(0);
SV* obj = newSVrv(obj_ref, class);
soldier->name = savepv(name);
soldier->rank = savepv(rank);
soldier->serial = serial;
sv_setiv(obj, (IV)soldier);
SvREADONLY_on(obj);
return obj_ref;
}
---------------------------------
A while back, at http://www.mail-archive.com/***@perl.org/msg01370.html Ken Williams suggested the following (more succinct) rewrite:
---------------------------------
SV* new(char* class, char* name, char* rank, long serial) {
Soldier* soldier;
New(42, soldier, 1, Soldier);
soldier->name = savepv(name);
soldier->rank = savepv(rank);
soldier->serial = serial;
return sv_setref_pv(newSViv(0), class, (void *)soldier);
}
----------------------------------
But the 2 versions of new() return slightly different objects. The original (cookbook) version returns this:
----------------------------------
SV = PVIV(0x2f5b8c) at 0x2f4940
REFCNT = 1
FLAGS = (PADBUSY,PADMY,ROK)
IV = 0
RV = 0x2f3ca4
SV = PVMG(0x2febbc) at 0x2f3ca4
REFCNT = 1
FLAGS = (OBJECT,IOK,READONLY,pIOK)
IV = 3143860
NV = 0
PV = 0
STASH = 0x45fca8 "Soldier"
PV = 0x2f3ca4 ""
CUR = 0
LEN = 0
----------------------------------
But, in Ken's rewrite, that READONLY flag is absent in the returned object.
1) What are the likely/possible ramifications of that READONLY flag being unset ? (I note that the cookbook version explicitly sets that READONLY flag ... presumably for a good reason.)
2) How does one modify Ken's rewrite so that it sets that READONLY flag ?
I recently asked about this at perlmonks ( http://www.perlmonks.org/index.pl?node_id=635113 ) but didn't receive any responses on the matter.
It's doubtful that I really *need* to know the answers (as I'm quite happy to stick with the cookbook approach) but I am, nevertheless, curious about it.
Note: I've also changed a couple of things in the versions of new() that I've presented above. The original versions used strdup(), which I've replaced with savepv() - and they used malloc(), which I've replaced with new().
Cheers,
Rob