Perl has functions to create new SVs containing IV, PV, NV, etc.. and
it has functions to modify an existing SV to take a given IV, PV, etc..
value
SV *sv = newSViv(123) vs sv_setiv(sv, 123);
SV *sv = newSVpvn("hi", 2); vs sv_setpvn(sv, "hi", 2);
SV *sv = newSVnv(1.23) vs sv_setnv(sv, 1.23);
etc..
While we have functions to create new SVs containing an RV, we appear
not to have one to modify an existing SV:
SV *sv = newRV_noinc(some_av); vs nothing.
I don't know of any reason why this should be lacking. I've sometimes
found myself wanting this function, so I copypaste this thing around
the place:
#define sv_setrv_noinc(s, r) S_sv_setrv_noinc(aTHX_ s, r)
static void S_sv_setrv_noinc(pTHX_ SV *sv, SV *rv)
{
sv_setiv(sv, (IV)rv);
#if !HAVE_PERL_VERSION(5, 24, 0)
SvIOK_off(sv);
#endif
SvROK_on(sv);
}
I'll probably just add this to core (minus the unnecessary #ifdef),
unless anyone has a suggestion why not.
--
Paul "LeoNerd" Evans
leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
it has functions to modify an existing SV to take a given IV, PV, etc..
value
SV *sv = newSViv(123) vs sv_setiv(sv, 123);
SV *sv = newSVpvn("hi", 2); vs sv_setpvn(sv, "hi", 2);
SV *sv = newSVnv(1.23) vs sv_setnv(sv, 1.23);
etc..
While we have functions to create new SVs containing an RV, we appear
not to have one to modify an existing SV:
SV *sv = newRV_noinc(some_av); vs nothing.
I don't know of any reason why this should be lacking. I've sometimes
found myself wanting this function, so I copypaste this thing around
the place:
#define sv_setrv_noinc(s, r) S_sv_setrv_noinc(aTHX_ s, r)
static void S_sv_setrv_noinc(pTHX_ SV *sv, SV *rv)
{
sv_setiv(sv, (IV)rv);
#if !HAVE_PERL_VERSION(5, 24, 0)
SvIOK_off(sv);
#endif
SvROK_on(sv);
}
I'll probably just add this to core (minus the unnecessary #ifdef),
unless anyone has a suggestion why not.
--
Paul "LeoNerd" Evans
leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/