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

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

Thread Previous | Thread Next
February 28, 2022 08:27
Re: Pre-RFC: builtin:: functions for detecting numbers vs strings
Message ID:
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?

I guess something like the following would work:

    # from parsing the JSON text, we end up with:
    # $key = "a"
    # $num = "2"
    $json->{$key} = 0+ $num;

    # and when parsing the next element:
    # $key = "b"
    # $str = "c"
    $json->{$key} = "$str";

Is this how we're expected to *set* the initial type of a scalar when
it's obtained from outside input (not source code)?

 Philippe Bruhat (BooK)

 Where there are hearts of gold, there is no need for bars of steel.
                                   (Moral from Groo The Wanderer #103 (Epic))

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About