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 11, 2022 02:37
Subject:
Re: Pre-RFC: builtin:: functions for detecting numbers vs strings
Message ID:
CANgJU+Xreu324M=4mR_s7R41eNqCbPwvos0POvNeu4w2rE8peQ@mail.gmail.com
On Fri, 11 Mar 2022, 09:42 Darren Duncan, <darren@darrenduncan.net> wrote:

> On 2022-03-10 5:19 p.m., Yuki Kimoto wrote:
> > I have a question.
> >
> > What means the scalar value is created as a number for JSON
> serialization?
> >
> >    # Created as a number
> >    my $num = 0;
> >
> >    # Replace 0 with "0"
> >    $num =~ s/0/0/;
> >    # Is this result 0 or "0"?
> >    my $json_num = to_json $num;
>
> The result is absolutely certainly a string, because it is the result of a
> regular expression, which is a string operation.
>
> Its logically the same as if you had said:
>
>    my $num = ''.$num;
>
> So "0" is the result in $json_num.
>

Agreed.

Fwiw The opposite outcome, forcing a value to be a number internally would
be:

$num = 0+$num;

Although saying that I wonder what we do these days with

$num += 0;

I haven't checked but I hope the two produce the same outcome.

Yuki you can check these things yourself by using Devel::Peek::Dump(). Just
inspect the flags.

Cheers,
Yves

>

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