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

Re: Missing sv_setrv()

Thread Previous
From:
Andy Xiao
Date:
July 27, 2021 01:21
Subject:
Re: Missing sv_setrv()
Message ID:
TYAP286MB02503A2F0C9B42270EFE3BD38AE99@TYAP286MB0250.JPNP286.PROD.OUTLOOK.COM
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

Thread Previous


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