What is true and false in Perl

In the Perl programming language no special values that mean true and false.

Any value that Perl can interpret as a Boolean value. For example, the string 'Hello' is true. The following code displays the string 'true':

▶ Run
#!/usr/bin/perl

if ('Hello') {
    print 'true';
} else {
    print 'false';
}

What values are false

In the Perl programming language there are 5 values that are false. This:

  • undef
  • 0 — 0
  • 0.0 — number of 0.0
  • '' — empty string
  • '0' — row 0

Here's an example that shows that all these values are interpreted by Perl as a lie. Code will display 5 times the word 'false':

▶ Run
#!/usr/bin/perl

foreach my $val (undef, 0, 0.0, '', '0') {
    if ($val) {
        print "true\n";
    } else {
        print "false\n";
    }
}

What values are true

Truth are all values, except those 5 that are false.

Rule

In order to remember what is false, you can use the rule: "All that 0 is false other than '0 but true' row".

All these 5 values are false if they are used as numbers become 0. Here's the code that confirms it, the code displays 5 zeros:

▶ Run
#!/usr/bin/perl

foreach my $val (undef, 0, 0.0, '', '0') {
    print $val + 0, "\n";
}

However, if you use use strict; and use warnings; (which almost always worth doing), in the case undef and the empty string Perl will show warning.

Here is the code and the result of his work in the case undef:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var = undef;

print $var + 0;
Use of uninitialized value $var in addition (+) at script.pl line 8.
0

And here is the code and the result in case of empty string:

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var = '';

print $var + 0;
Argument "" isn't numeric in addition (+) at script.pl line 8.
0

0 but true

In Perl there are such values in a Boolean context are true, but when used in as the number converted into the number zero. All such values are strings.

For example, the string '0ASDF' is an example of such value. When used as a Boolean value this string will be interpreted as true. Really, there are only 2 lines, which are lie — an empty string and a string which contains only a single character is the number zero, line '0ASDF' is not neither one of these two lines, so it is not a lie, and the truth.

Here's the code that shows what line '0ASDF' is true (code will display the string 'true'):

▶ Run
#!/usr/bin/perl

if ('0ASDF') {
    print 'true';
} else {
    print 'false';
}

If this line '0ASDF' to use as a number, it is interpreted as the number 0 (however, when included use strict;, use warnings; will be a warning):

▶ Run
#!/usr/bin/perl

use strict;
use warnings;

my $var = '0ASDF';

print $var + 0;
Argument "0ASDF" isn't numeric in addition (+) at script.pl line 8.
0

The most famous use of 0 but true values occurs in the Perl DBI library, it uses the string '0E0'.

Array, Hash, Reference

The array is true if there is at least one value. If in the array there is no value, then the array is false.

Similarly, if the hash has at least one pair of values, then the hash is true. If the hash is not no pair of values, it is a lie.

Reference is always true, no matter what it refers to. Even a reference to an empty array [] or an empty hash {} is the truth.

One and an empty string

Often to write in the code the truth is a number one, and to write lies used the number zero.

Interestingly, he believes that the canonical Perl value to denote the lie is not the number 0, and the empty string ''.

Here is an example that shows this. We took the line 'Hello', which is the truth, and with the unary operator ! turned it into a lie:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

print Dumper !'Hello';

The result appeared on the screen the text $VAR1 = '';

If you use the operator ! twice, we obtain the canonical value for truth from the point of view of Perl:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

print Dumper !!'Hello';

(on screen appears the text $VAR1 = 1;).

The problem of readability

Often to write in the code the truth is a number one, and to write lies used the number zero. But, unfortunately, from these numbers it is not clear what this means — the number (and there may be other numbers), or is a flag meaning true.

Here is an example. Here 1 for workers means that you need to use a single worker (can be replaced by 10). And the unit have log means that logging is enabled (and can be replaced only to 0 to disable):

my $config = {
    workers => 1,
    log => 1,
};

Other articles

Comments