[Melbourne-pm] When your variables can't keep their types..
Toby Corkindale
toby.corkindale at strategicdata.com.au
Sun Jun 5 17:29:51 PDT 2016
Thanks for the replies all, but perhaps my point was missed.
It's not about JSON treating strings and integers differently.
Note that I did not assign anything to $port between the two calls to
encode_json(), yet $port changes.
Despite their name ("variables"), in normal programming languages, variables
only change when they're assigned to.
hence the exclamation at the end of the example -- "WTF Perl?!"
On Saturday, 4 June 2016 6:19:23 PM AEST Alex Balhatchet wrote:
> Hi Toby,
>
> If you convert 1234 from Perl to JSON you get 1234, if you convert "1234"
> from Perl to JSON you get "1234" - this is good, the conversion keeps its
> types perfectly.
>
> On Friday, 3 June 2016, Toby Corkindale
> <toby.corkindale at strategicdata.com.au> wrote:
> > Guess the output...
> >
> >
> > #!/usr/bin/env perl
> > use 5.12.0;
> > use warnings;
> > use JSON::XS qw(encode_json);
> >
> > my $port = 1234;
> >
> > my $j1 = encode_json({ port => $port });
> > my $foo = "$port";
> > my $j2 = encode_json({ port => $port });
> >
> > say $j1;
> > say $j2;
> >
> > if ($j1 ne $j2) {
> >
> > die "WTF Perl?!";
> >
> > }
More information about the Melbourne-pm
mailing list