develooper Front page | perl.perl5.porters | Postings from March 2022

Re: Pre-RFC: builtin:: functions for detecting numbers vs strings

Thread Previous | Thread Next
From:
demerphq
Date:
March 1, 2022 04:53
Subject:
Re: Pre-RFC: builtin:: functions for detecting numbers vs strings
Message ID:
CANgJU+XKBXgYVC89cxBFK1FaOnkgbv9MGmmphcpQr9YLZ81wxg@mail.gmail.com
On Mon, 28 Feb 2022 at 14:15, Philippe Bruhat (BooK) <book@cpan.org> wrote:
>
> On Mon, Feb 28, 2022 at 02:15:42AM -0800, Darren Duncan wrote:
> > On 2022-02-28 1:41 a.m., Dan Book wrote:
> >
> > Likewise, "0+$w" produces a number so its result is "originally" a
> > number, so do that, or some other explicit cast operator like "int $w"
> > etc, with values pulled from JSON to make them canonically numbers. --
> > Darren Duncan
>
> I think it all depends if a new SV was created. We've seen that some
> string operations operate on the original string. I doubt this is the
> case with assignment or numbers, so I expect the result of 0+ is indeed
> a new original SV.
>
> Actually, let me follow Yves' suggestion and test my assumptions:
>
>     $ perl -MDevel::Peek -e '$a="1";Dump($a); $b=0+$a; Dump($b)'
>     SV = PV(0x564576205ec0) at 0x564576230d80
>       REFCNT = 1
>       FLAGS = (POK,IsCOW,pPOK)
>       PV = 0x564576231b90 "1"\0
>       CUR = 1
>       LEN = 10
>       COW_REFCNT = 1
>     SV = IV(0x564576230e18) at 0x564576230e28
>       REFCNT = 1
>       FLAGS = (IOK,pIOK)
>       IV = 1

Right, so the action of:

$new= 0+$old;

converts it to a "number". It is the perl equivalent of:

SV *new_sv= newSViv(SvIV(old_sv));

No function needed in builtin for this IMO.

Note that the impact of the flag change would show if you added an
additional Dump($a) at the end of your original invocation.

cheers,
Yves



-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

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