X Dungeness wrote:
It's about what unary ! (bang operator) does to the operand
Here's the dissonance:
perl -E '$x=0; say "x=$x"; $x = !!$x; say "x=$x"'
x=0
x=
It behaves as you expect until you "bang" it twice.
I found a good explanation in the Camel:
"Unary ! performs logical negation, that is "not". The value
of a negated operand is true (1) if the operand is false
(numeric 0,string "0", the null string, or undefined) and
false ("") if the operand is true."
So double banging 0 yields "".
That doesn´t explain how and why a numerical value is converted into
a string. It could be something else than the operator doing this.
In any case, which sane programmer would expect a bug like this.
perl -e 'my $i = 1; $i = defined($i) ? (!$i) : 0; use Data::Dumper; print
Dumper($i);'
$VAR1 = '';
Logical negations do not involve turning numbers into strings. If they
do, they are either something else or buggy.
How do you enforce a numerical context?
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/