develooper Front page | perl.perl5.porters | Postings from July 2021

Re: Missing sv_setrv()

Thread Previous | Thread Next
From:
Leon Timmermans
Date:
July 23, 2021 11:08
Subject:
Re: Missing sv_setrv()
Message ID:
CAHhgV8jTdBJaR1B5ji9oqRC=Y2g1Gn3aUdQC4kY3_Cp2q_5syA@mail.gmail.com
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

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About