Broad Network


Truth and Falsehood in Perl

Perl Syntax Part 2

Perl Course

Foreword: In this part of the series I talk about what is equivalent to Boolean true and what is equivalent to Boolean false.

By: Chrysanthus Date Published: 13 Jun 2015

Introduction

This is part 2 of my series, Perl Syntax. In this part of the series I talk about what is equivalent to Boolean true and what is equivalent to Boolean false. You should have read the previous part of the series before reaching here, as this is a continuation. In Perl, true is the integer 1 and false is the integer zero.

False
The number 0, the string '0', the string "", the empty list (), and undef are all false in a Boolean context. This means, in a condition such as the if or the while condition, any of these string items will be equivalent to zero; that is, false. Here, 0 is zero and not the letter, O. In the following code, the condition for each code segment is false:

use strict;

    if (!0)
        {
            print "condition is equivalent to false\n";
        }
    if (!'0')
        {
            print "condition is equivalent to false\n";
        }
    if (!"")
        {
            print "condition is equivalent to false\n";
        }
    if (!())
        {
            print "condition is equivalent to false\n";
        }
    if (!undef)
        {
            print "condition is equivalent to false\n";
        }

Try the code. In practice, the conditions will not be that straightforward. The following code, where the variable is equivalent to "", is a bit more practical:

use strict;

    my $var = "";

    if (!$var)
        {
            print "condition is equivalent to false\n";
        }

True
If a value is not false, it is true. So, if a value is not equivalent to 0, or '0', or "", or (), or undef, then it is true. For example, 1 is true, 1 is true, and 2 is true. In fact, all numbers are true except 0. The string, "something" is true. In fact any non-empty string is true, including " ", which has just a space character. In the following code, the condition for each code segment is true:

use strict;

    if (-1)
        {
            print "condition is equivalent to true\n";
        }
    if (1)
        {
            print "condition is equivalent to true\n";
        }
    if ("something")
        {
            print "condition is equivalent to true\n";
        }
    if (" ")
        {
            print "condition is equivalent to true\n";
        }

In practice, the conditions will not be that straightforward. The following code, where the variable is equivalent to "some text", is a bit more practical:

use strict;

    my $var = "I love you";

    if ($var)
        {
            print "it is true\n";
        }

undef
undef meaning undefined, is false. However, when used as a number, undef is treated as 0 ; when used as a string, it is treated as the empty string, "". When you declare a variable without assignment, the value of the variable is undef.

That is it for this part of the series. Rendezvous in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message