Re: !!!1 is nothing?
hw via beginners <[email protected]> Mon, 04 May 2026 23:34:39 +0200
| Newsgroups | gmane.comp.lang.perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
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? >=20 > =C2=A0No. 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'? > > =C2=A0Even '!!!1' should evaluate to 0 and not to nothing. > ... >=20 > =C2=A0 > =C2=A0No, 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 =3D ...; my $rc =3D $obj->get_isoff; say Dumper($rc); say $rc; printf("{%s}\n", $rc); This prints: "$VAR1 =3D !!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 =3D=3D $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: >=20 > =C2=A0From `man perldata` under headng "Scalar values", 3rd paragraph: >=20 > =C2=A0=C2=ABA scalar value is interpreted as FALSE in the Boolean sense i= f it > is > =C2=A0undefined, the null string or the number 0 (or its string > equivalent, "0"), > =C2=A0and TRUE if it is anything else.=C2=A0 The Boolean context is just = a > special kind > =C2=A0of scalar context where no conversion to a string or a number is > ever > =C2=A0performed.=C2=A0 Negation of a true value by "!" or "not" returns a > special false > =C2=A0value.=C2=A0 When evaluated as a string it is treated as "", but as= a > number, it > =C2=A0is treated as 0.=C2=A0 Most Perl operators that return true or fals= e > behave this > =C2=A0way.=C2=BB >=20 > =C2=A0I.e. !1 depends on the context, if numeric it is 0, if string it is > "": >=20 > $ perl -e 'print !1 + 0, "\n"' > 0 > $ perl -e 'print !1 . "", "\n"' >=20 > $ >=20 > Regards, > /Karl Hammar >=20 >=20 -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/