From fb6608f08e086de99d40b40534bc230142182adf Mon Sep 17 00:00:00 2001 From: Richard Leach Date: Sun, 2 Feb 2025 14:26:49 +0000 Subject: [PATCH] Replace sv_newmortal + sv_setsv_nomg with sv_mortalcopy_flags Creating a new mortal copy of an existing SV can also be done via: * `newsv = sv_newmortal();` * `sv_setsv_nomg(newsv, sv);` The same outcome can be achieved in a single function call: * `newsv = sv_mortalcopy_flags(sv, SV_DO_COW_SVSETSV);` --- pp.c | 3 +-- pp_ctl.c | 3 +-- pp_hot.c | 3 +-- pp_sys.c | 6 ++---- sv.c | 3 +-- 5 files changed, 6 insertions(+), 12 deletions(-) diff --git a/pp.c b/pp.c index 3704a5e07554..8354be956bc2 100644 --- a/pp.c +++ b/pp.c @@ -4063,8 +4063,7 @@ PP(pp_chr) { if (ckWARN(WARN_UTF8)) { if (SvGMAGICAL(top)) { - SV *top2 = sv_newmortal(); - sv_setsv_nomg(top2, top); + SV *top2 = sv_mortalcopy_flags(top, SV_DO_COW_SVSETSV); top = top2; } Perl_warner(aTHX_ packWARN(WARN_UTF8), diff --git a/pp_ctl.c b/pp_ctl.c index fa0e2478d07c..0e39d16209df 100644 --- a/pp_ctl.c +++ b/pp_ctl.c @@ -4738,8 +4738,7 @@ S_require_file(pTHX_ SV *sv) loader = *av_fetch(AV_FROM_REF(loader), 0, TRUE); if (SvGMAGICAL(loader)) { SvGETMAGIC(loader); - SV *l = sv_newmortal(); - sv_setsv_nomg(l, loader); + SV *l = sv_mortalcopy_flags(loader, SV_DO_COW_SVSETSV); loader = l; } } diff --git a/pp_hot.c b/pp_hot.c index 836abc4cfae9..45a1fe0d7c03 100644 --- a/pp_hot.c +++ b/pp_hot.c @@ -6745,8 +6745,7 @@ Perl_vivify_ref(pTHX_ SV *sv, U32 to_what) if (SvGMAGICAL(sv)) { /* copy the sv without magic to prevent magic from being executed twice */ - SV* msv = sv_newmortal(); - sv_setsv_nomg(msv, sv); + SV* msv = sv_mortalcopy_flags(sv, SV_DO_COW_SVSETSV); return msv; } return sv; diff --git a/pp_sys.c b/pp_sys.c index 5f8bb0d6eda3..addfa327d87c 100644 --- a/pp_sys.c +++ b/pp_sys.c @@ -563,14 +563,12 @@ PP_wrapped(pp_warn, 0, 1) SvGETMAGIC(errsv); if (SvROK(errsv)) { if (SvGMAGICAL(errsv)) { - exsv = sv_newmortal(); - sv_setsv_nomg(exsv, errsv); + exsv = sv_mortalcopy_flags(errsv, SV_DO_COW_SVSETSV); } else exsv = errsv; } else if (SvPOKp(errsv) ? SvCUR(errsv) : SvNIOKp(errsv)) { - exsv = sv_newmortal(); - sv_setsv_nomg(exsv, errsv); + exsv = sv_mortalcopy_flags(errsv, SV_DO_COW_SVSETSV); sv_catpvs(exsv, "\t...caught"); } else { diff --git a/sv.c b/sv.c index 3d3f61d4ce04..3b1488b411f9 100644 --- a/sv.c +++ b/sv.c @@ -10316,8 +10316,7 @@ Perl_sv_2io(pTHX_ SV *const sv) if (!io) { SV *newsv = sv; if (SvGMAGICAL(sv)) { - newsv = sv_newmortal(); - sv_setsv_nomg(newsv, sv); + newsv = sv_mortalcopy_flags(sv, SV_DO_COW_SVSETSV); } Perl_croak(aTHX_ "Bad filehandle: %" SVf, SVfARG(newsv)); }