4 ms·
Perl avoids this problem by having contexts, a bit like casts but with easier syntax, and by having separate numeric and string equality operators.
by xorcist 6y ago
Perl avoids this problem by having contexts, a bit like casts but with easier syntax, and by having separate numeric and string equality operators.
- tyingq 6y agoIs there a reason returning undefined instead of true in those cases (string doesn't look like a number) would be worse?
- xorcist 6y agoVery few functions in Perl does that. It will emit a warning instead. As to why that is, perhaps someone with more Perl knowledge can chime in. I suspect it has to do with undef being inconvenient for signalling failure, since its evaluation will be context dependent (an array of undef is true but a numeric undef is false). Type conversion is built into the language at a fundamental level. Languages that borrows the syntax but not the type model can be confusing in cases, but Perl is pretty consistent once you get the basics.