in reply to Surprising result when using undef
DB<1> p undef == 0 1 DB<2>
correct???
Perl does automatic type-casting² and emits warnings at best.
That's why you can do things like
my $count; $count++; print "Count is $count"
(undef->integer->string)
If you want to test against undef use operators like defined or //
> I would not have thought that "( $an_undefined_variable == 0 )" would evaluate to true because, golly, undef isn't a number and so the "==" operator is testing whether one number is the same as another number.
Perl is not C, types are dynamic. That's why Perl has so many more operators than other languages like + and . where others only have + °
BTW: JS has the worst of both worlds in this respect, types are dynamic but operators are spare. I.e. sometimes 1 + 1 means 11 there.
And in Python 3/2 results in 1 , because you divided two integers! (IIRC they adjusted something in Py3 but I think it's a new operator // )
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
°) Perl has also two equality operators for numeric == and string eq
That's why
DB<1> p undef eq "" 1 DB<2> p undef eq "0" DB<3>
²) i.e. internally. Externally do all scalars have a well defined numeric and string value.
|
|---|