1

Am doing this on mac, perl version is

This is perl 5, version 16, subversion 3 (v5.16.3) built for darwin-thread-multi-2level

$ perl -e "$t=false;while(!($t)){print 1}"
$ perl -e "$t=true;while(!($t)){print 1}"
$ perl -e "$t=true;while(not($t)){print 1}"
$ perl -e "$t=false;while(not($t)){print 1}"
$

Why would this result? Anything I have missed?

Qiang Li
  • 10,593
  • 21
  • 77
  • 148
  • 3
    What is the result you're getting and what were you expecting? It's worth noting that `true` and `false` are not Perl keywords. `1` and `0` are commonly used. Also your code should almost certainly be in single rather than double quotes - the shell will try to replace `$t` before running your code. The Bash shell will also be confused by the `!` in a double-quoted string. – Grant McLean Apr 26 '16 at 21:12
  • When asking a question like this, it's usually helpful to explain what you think is strange in the output. Any Perl programmer looking at your examples would think the behaviour is completely expected. If you tell us what you find unexpected then we can debug your misunderstanding. – Dave Cross Apr 27 '16 at 09:19

1 Answers1

4

From perlsyn:

Truth and Falsehood

The number 0, the strings '0' and "" , the empty list (), and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value.

$t=false sets $t to the string false when strict 'subs' is disabled. The string false is truthy when used in a boolean context.

Therefore:

$t=false # truthy
!$t      # falsy
$t=true  # truthy
!$t      # falsy

Perl doesn't have special keywords for true and false; it's common to use 1 for true and 0 for false.

Community
  • 1
  • 1
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110