perlboolean

How do I use boolean variables in Perl?


I have tried:

$var = false;
$var = FALSE;
$var = False;

None of these work. I get the error message

Bareword "false" not allowed while "strict subs" is in use.

Solution

  • Truth and Falsehood in man perlsyn explains:

    The number 0, the strings '0' and "", the empty list "()", and "undef" are all false in a boolean context. All other values are true.

    In Perl, the following evaluate to false in conditionals:

    0
    '0'
    undef
    ''  # Empty scalar
    ()  # Empty list
    ('')
    

    The rest are true. There are no barewords for true or false. (Note: Perl v5.38 introduced true and false through the new builtin pragma).