1

I am learning scripting in perl. I have encountered an example which I don't understand:

my $num = 50;
if ($num) {
    print "True: $num\n";
} else {
    print "False: $num\n";
}

This is very simple indeed and it prints out the number 50 but what I don't understand is the condition. I expect something like if ($num) > 40 or any thing that tells computer to examine a condition. How does computer interpret if ($num) as a condition?

Thanks for any explanation.

serenesat
  • 4,611
  • 10
  • 37
  • 53
Homap
  • 2,142
  • 5
  • 24
  • 34
  • 1
    [What is true and false in Perl](http://www.perlmonks.org/?node=what%20is%20true%20and%20false%20in%20Perl%3F) and also [True or False? A Quick Reference Guide](http://www.perlmonks.org/?node_id=495975) – Robby Cornelissen Jul 24 '15 at 08:50

3 Answers3

1

Quoting the perlsyn manpage:

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.

Here $num is true as it is a non-zero integer, 50.

Note that something like $num > 40 isn't actually special syntax! It will evaluate to 1 (or "1", contextually) if the condition holds, and to 0 (or the empty string "", contextually) if it doesn't. Then Perl just sees if (1) or if (0) and handles accordingly.

Lynn
  • 10,425
  • 43
  • 75
1

In Perl 0 and ""(emtpy string) will be false. All others are true. Please note that if you are checking "0.0" as condition, it will return true as it is not and empty string.

Balu SKT
  • 549
  • 5
  • 22
  • Wrong. `undef` and `"0"`, for example, are also considered false. –  Jul 24 '15 at 09:06
  • True undef is also considered as false. But how come s string "0" will become false? Its not an empty string! – Balu SKT Jul 24 '15 at 09:31
  • `0` and `"0"` aren't very different in Perl! – Lynn Jul 24 '15 at 10:27
  • perl doesnt have a string and an integer data type. So when evaluated in boolean context 0 and "0" and '0' are all the same. there is no difference from perls view point. – Chris Doyle Jul 24 '15 at 10:30
-3

This is a way to check if a variable has a true value. The following evaluates to false because $num has no defined value.

my $num;
if (defined($num) && $num ne "" && $num ne "0")
{
    # do something
}

See What values are true and false in Perl.

serenesat
  • 4,611
  • 10
  • 37
  • 53
  • No. It checks that a variable has a **true** value, not that it has a defined value. The empty string and the number zero are two examples of values which are false, but defined. – Dave Cross Jul 24 '15 at 10:09
  • @DaveCross: Thank you. Corrected. – serenesat Jul 24 '15 at 10:19
  • No, this answer is still not correct. – mob Jul 25 '15 at 00:45
  • @mob: Can you please help me to correct this by editing the answer. – serenesat Jul 25 '15 at 05:46
  • `if ($num) ...` is equivalent to `if (defined($num) && $num ne "" && $num ne "0") ...` If it was a test for equality to 50l it would make the language a lot less useful. – mob Jul 25 '15 at 20:48