Here is the sample. For some strange reason Perl thinks that 1 and 0
is a true value. Why?
$ perl -e '$x = 1 and 0; print $x;'
1
False Values: Empty string or string contains single digit 0 or undef value and zero are considered as the false values in perl.
Unlike many other languages which support object orientation, Perl does not provide any special syntax for constructing an object. Objects are merely Perl data structures (hashes, arrays, scalars, filehandles, etc.)
Perl does not have a special boolean type and yet, in the documentation of Perl you can often see that a function returns a "Boolean" value. Sometimes the documentation says the function returns true or returns false.
Because the precedence of and
and &&
differ:
$x = 1 and 0
is like ($x = 1) and 0
, whereas $x = 1 && 0
is like $x = (1 && 0)
.
See perlop(1).
Operator precedence in your example is
perl -e '($x = 1) and 0; print $x;'
while what you want is:
perl -e '$x = (1 and 0); print $x;'
or
perl -e '$x = 1 && 0; print $x;'
It doesn't:
$ perl -e '$x = (1 and 0); print $x;'
0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With