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

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

Thread Previous | Thread Next
From:
Dan Book
Date:
February 28, 2022 08:56
Subject:
Re: Pre-RFC: builtin:: functions for detecting numbers vs strings
Message ID:
CABMkAVXh-Xbi=4yEAj8fdpDMB1YCWbFtcGvxyO1PWv4eK=RgYw@mail.gmail.com
On Mon, Feb 28, 2022 at 3:27 AM Philippe Bruhat (BooK) <book@cpan.org>
wrote:

> On Wed, Feb 23, 2022 at 11:03:14PM +0100, Graham Knop wrote:
> > For serializers, being able to detect the original type of a value is
> > essential, so it would help to provide functions to provide this
> > information. For example, providing builtin::isnumber and
> > builtin::isstring, to go along with builtin::isbool.
>
> One issue that comes to mind is that of round-tripping.
>
> JSON, when read by Perl, is just text. Actually, all input that we don't
> parse from Perl source code is always text, right?
>
> When parsing `{ "a" : 2, "b": "c" }`, all that Perl reads is text. How
> would a pure Perl JSON parser decode that `2` from the input source and
> keep it in memory as if it was always the number 2?
>

That's not the case - JSON parsers are aware whether they are parsing a
number or a string because the JSON grammar is distinct, and can create the
scalar differently depending. Not like e.g. certain databases where you
can't always tell the type of the response values. Cpanel::JSON::XS::Type
uses this information to return a type schema which you can already use to
round-trip the output - these sort of changes enable more opportunities.

-Dan

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