On Mon, 2026-05-04 at 11:28 +0200, [email protected] wrote:
> [email protected]:
> ...
> > Is there some kind of wierd evaluating of '!!1'
> > or '!!0' going on?
> 
>  No.

I'm not so sure about that.  Dumper shows the variable as numerical,
not as string.  The device sending the information uses 'true' and
'false'.

Why would the conversion of the JSON garbage turn it into '!!0'?

> >  Even '!!!1' should evaluate to 0 and not to nothing.
> ...
> 
>  
>  No, undef, "" and 0 are all perl legal false values.

If the value were undef, I would have gotten an error message about
printing an undefined value.

I've just made a test to verify it:


my $obj = ...;
my $rc = $obj->get_isoff;
say Dumper($rc);
say $rc;
printf("{%s}\n", $rc);


This prints:


"$VAR1 = !!0;


{}"


If there is a conversion from a numerical expression into a(n empty)
string taking place, then this is plain wrong and leads to errors.  The
caller doesn't expect a string being returned but a number, a boolean
actually, and the comparison will be


if(1 == $rc) ...


and not


if("" eq $rc) ...


The very usage of !! clearly indicates that this is a boolean value,
and in that context, no conversion must be performed:


> 
>  From `man perldata` under headng "Scalar values", 3rd paragraph:
> 
>  «A scalar value is interpreted as FALSE in the Boolean sense if it
> is
>  undefined, the null string or the number 0 (or its string
> equivalent, "0"),
>  and TRUE if it is anything else.  The Boolean context is just a
> special kind
>  of scalar context where no conversion to a string or a number is
> ever
>  performed.  Negation of a true value by "!" or "not" returns a
> special false
>  value.  When evaluated as a string it is treated as "", but as a
> number, it
>  is treated as 0.  Most Perl operators that return true or false
> behave this
>  way.»
> 
>  I.e. !1 depends on the context, if numeric it is 0, if string it is
> "":
> 
> $ perl -e 'print !1 + 0, "\n"'
> 0
> $ perl -e 'print !1 . "", "\n"'
> 
> $
> 
> Regards,
> /Karl Hammar
> 
> 

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to