Mailing List Archive

Missing sv_setrv()
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/
Re: Missing sv_setrv() [ In reply to ]
On Fri, Jul 23, 2021 at 12:51 PM Paul "LeoNerd" Evans <
leonerd@leonerd.org.uk> wrote:

> 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.
>

That makes sense to me, I have needed it on at least one occasion.

Leon
Re: Missing sv_setrv() [ In reply to ]
yes, add this function please!
________________________________
From: Leon Timmermans <fawaka@gmail.com>
Sent: Friday, July 23, 2021 7:08 PM
To: Paul LeoNerd Evans <leonerd@leonerd.org.uk>
Cc: Perl5 Porters <perl5-porters@perl.org>
Subject: Re: Missing sv_setrv()

On Fri, Jul 23, 2021 at 12:51 PM Paul "LeoNerd" Evans <leonerd@leonerd.org.uk<mailto:leonerd@leonerd.org.uk>> wrote:
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.

That makes sense to me, I have needed it on at least one occasion.

Leon
Re: Missing sv_setrv() [ In reply to ]
On Fri, 23 Jul 2021 11:50:54 +0100
"Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:

> I'll probably just add this to core (minus the unnecessary #ifdef),
> unless anyone has a suggestion why not.

I'm just coming back to actually writing this now, and I've encountered
a few design questions, partly from comparing the function to similar
others in perl core, and partly from looking at simplifying other bits
of code to just call this new function instead of their own internal
logic involving SvRV_set and friends:

* Should it invoke SETMAGIC on the sv? I notice that sv_setiv() does
not (but there's a related sv_setiv_mg() which does); similar with
the other related ones. This feels the right shape.

* Should it increment the refcount of the referred SV? I suspect I
should in fact add two separate versions, sv_setrv_noinc() and
sv_setrv_inc() to be really clear on this.

That would lead to in total, four new macro names:
sv_setrv_noinc()
sv_setrv_noinc_mg()
sv_setrv_inc()
sv_setrv_inc_mg()
Is this OK?

* Should it accept ref=NULL to clear the reference? I currently think
it shouldn't (especially because that makes sv_setrv_inc more
awkward). But, if it did accept NULL then I could replace a few
more bits of custom code in core with more calls to this function.

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: Missing sv_setrv() [ In reply to ]
On Sun, 22 Aug 2021 18:40:58 +0100
"Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:

> I'm just coming back to actually writing this now

And now a PR:

https://github.com/Perl/perl5/pull/19074

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: Missing sv_setrv() [ In reply to ]
On Sun, 22 Aug 2021 23:13:06 +0100
"Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:

> On Sun, 22 Aug 2021 18:40:58 +0100
> "Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:
>
> > I'm just coming back to actually writing this now
>
> And now a PR:
>
> https://github.com/Perl/perl5/pull/19074

And now merged :)

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/