Perl | Boolean Values

In most of the programming language True and False are considered as the boolean values. But Perl does not provide the type boolean for True and False. In general, a programmer can use the term “boolean” when a function returns either True or False. Like conditional statements(if, while etc.) will return either true or false for the scalar values.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Perl Code to demonstrate the boolean values
  
# variable assigned value 0
$k = 0;
  
# checking whether k is true or false
if ($k)
{
    print "k is True\n";
}
else
{
    print "k is False\n";
  
# variable assigned value 2
$m = 2;
  
# checking whether m is true or false
if ($m)
{
    print "m is True\n";
}
else
{
    print "m is False\n";

chevron_right


Output:

k is False
m is True

True Values: Any non-zero number i.e. except zero are True values in the Perl language. String constants like ‘true’, ‘false’, ‘ ‘(string having space as the character), ’00’(2 or more 0 characters) and “0\n”(a zero followed by a newline charatcer in string) etc. also consider true values in Perl.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl Code to demonstrate the True values
      
    # variable assigned value 5
    $a = 5;
      
    # checking whether a is true or false
    if ($a)
    {
        print "a is True\n";
    }
    else
    {
        print "a is False\n";
      
    # string variable assigned white 
    # space character
    $b = ' ';
      
    # checking whether b is true or false
    if ($b)
    {
        print "b is True\n";
    }
    else
    {
        print "b is False\n";
      
    # string variable assigned 'false'
    # value to it
    $c = 'false';
      
    # checking whether c is true or false
    if ($c)
    {
        print "c is True\n";
    }
    else
    {
        print "c is False\n";
      
    # string variable assigned "0\n"
    # value to it
    $d = "0\n";
      
    # checking whether d is true or false
    if ($d)
    {
        print "d is True\n";
    }
    else
    {
        print "d is False\n";

    chevron_right

    
    

    Output:

    a is True
    b is True
    c is True
    d is True
    

False Values: Empty string or string contains single digit 0 or undef value and zero are considered as the false values in perl.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl Code to demonstrate the False values
      
    # variable assigned value 0
    $a = 0;
      
    # checking whether a is true or false
    if ($a)
    {
        print "a is True\n";
    }
    else
    {
        print "a is False\n";
      
    # string variable assigned empty string
    $b = '';
      
    # checking whether b is true or false
    if ($b)
    {
        print "b is True\n";
    }
    else
    {
        print "b is False\n";
      
    # string variable assigned undef
    $c = undef;
      
    # checking whether c is true or false
    if ($c)
    {
        print "c is True\n";
    }
    else
    {
        print "c is False\n";
      
    # string variable assigned ""
    # value to it
    $d = "";
      
    # checking whether d is true or false
    if ($d)
    {
        print "d is True\n";
    }
    else
    {
        print "d is False\n";

    chevron_right

    
    

    Output:

    a is False
    b is False
    c is False
    d is False
    

Note: For the conditional check where the user has to compare two different variables, if they are not equal it returns False otherwise True.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Perl Program demonstrate the conditional check
      
    # variable initialized with string
    $x = "GFG";
      
    # using if statement
    if ($x eq "GFG")
    {
        print "Return True\n";
    }
    else
    {
        print "Return False\n";
    }

    chevron_right

    
    

    Output:

    Return True
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Aman Khare 1



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.