On Thu, Jul 6, 2017 at 9:33 AM hw <h...@gc-24.de> wrote:

> False and true are genuinely numeric.  You can´t say for a string
> whether it is true or false; it is a string.
>

This is not a true statement in Perl.  All values in Perl can be true or
false.  And the prototypical true and false values, PL_sv_yes and PL_sv_no,
are dualvars that hold multiple values (string, int, and double).  These
values are returned by the various logical operators (and any other XS code
that wants to use them).

The following values in Perl are false: the empty list, undef, "", 0, 0.0,
and "0".

All other values are true.

These give you different results, and that is just wrong.  I did
> assign a /number/ to $i and never a string.


No, you assigned the result of the negation operator which returns
PL_sv_yes if the operand is false (see above for the list of false values)
or PL_sv_no if the operand is true.  Since 0 is false, !0 return PL_sv_yes
(which contains "1", 1, and 1.0).  Since since PL_sv_yes is true, !!0
returns PL_sv_no (which contains the values "", 0, and 0.0).  If you want
to ensure the result is an integer value 0 or 1, then you need to say:

$i = defined $i ? 0+!!$i : 0;

The venus secret operator (0+) forces the result to be an integer or double
(depending on what $i contained).  Another option is to use the int
operator:

$i = defined $i ? int !!0 : 0;

Reply via email to