I made this script to check how scalars change when accidently using 'eq' instead of '==' and vice versa. Using '==' on strings changes nothing but using 'eq' on numbers makes the scalar change somehow. Here comes the code:
#!/usr/bin/perl
use strict;
use JSON;
my $str = "123";
my $num = 123;
print "BEFORE:\n";
print "str: ", \$str, " num: ", \$num, "\n";
print to_json({str => $str, num => $num}, {pretty => 1});
if ($str == 23) { }
if ($num eq "123") { }
print "AFTER:\n";
print "str: ", \$str, " num: ", \$num, "\n";
print to_json({str => $str, num => $num}, {pretty => 1});
print "\n";
Output:
BEFORE:
str: SCALAR(0x8010f8) num: SCALAR(0x801050)
{
"num" : 123,
"str" : "123"
}
AFTER:
str: SCALAR(0x8010f8) num: SCALAR(0x801050)
{
"num" : "123",
"str" : "123"
}
With words, $num is changed from being a number to being a string. By commenting the line
if ($num eq "123") { }
$num is not changed anymore. Is this a bug or feature? Why does this happen? Also, how can I see this without using to_json?
perl --version
This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi