[Melbourne-pm] When your variables can't keep their types..

Simon Taylor simon at unisolve.com.au
Sun Jun 5 17:35:12 PDT 2016



On 6/06/2016 10:29 AM, Toby Corkindale wrote:
> 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?!"

I agree.

I didn't run your sample to see if it behaved the same way here, but it 
is very counter-intuitive behavior.

Cheers,

Simon


>
> 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?!";
>>>
>>> }
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm



More information about the Melbourne-pm mailing list