4 ms·
> Perl doesn't have a way to say "this value is a number" or "this value is a string". This isn't really any different than JavaScript or similar languages tha
by wahern 5y ago
> Perl doesn't have a way to say "this value is a number" or "this value is a string".
This isn't really any different than JavaScript or similar languages that perform automatic type coercion. Internally scalars are differentiated. When writing Perl XS (the C extension interface), scalars are clearly differentiated into integers, doubles, strings, references (Perl's equivalent of pointers), and several other internal types. In a sense, a Perl scalar is the same as a "scalar" in many other languages (whether strongly or weakly, statically or dynamically typed), where you have two basic types--scalar and compound types. In C an integer, floating point, and pointer are all scalar values, as distinguished from an array, struct, or union.
So when you do `my $a = 3;`, `$a` at that point is definitely an integer type. However, it can be coerced (both conceptually and literally--in Perl objects are mutable and can have both their value and type changed in-place) into a string according to Perl's extremely complex DWIM type coercion rules.
- eurleif 5y ago>This isn't really any different than JavaScript or similar languages that perform automatic type coercion. "1" + "1" is "11" in JavaScript. In Perl, it's 2. There's also no JS-like `typeof` operator in Perl. The types you mention in Perl XS are just an implementation detail from the perspective of the language itself. "1" and 1 are exactly the same value, not different values that can be coerced into each other.