1

I heard from multiple tutorials that there is no Boolean variable. Instead I can use 1 for true or 0 for false. However, I have 2 methods to get the boolean values. The output is the same.. But I do not know which method is correct for collecting the return boolean values. Let me give you an example I made a script,call.pl to call the function from another script,script.pl and the script.pl will return the 1 or 0. I perform the if conditional to evaluate. If it is true, it will says that it is even otherwise it is odd.

Method 1 script.pl

sub checkevenodd {
     my ($num) = @_;
     chomp($num);
     my $remainder = $num % 2;
     if ($remainder == 0)
     {
       return 1;
     }
     else
     {
        return 0
     }
}
1;

call.pl

require "script.pl";
my $no = 123;
if (checkevenodd($no) == 1)
{
    print "it is even";
}
else
{
   print "it is odd";
}

method 2 script.pl

sub checkevenodd {
     my ($num) = @_;
     chomp($num);
     my $remainder = $num % 2;
     if ($remainder == 0)
     {
       return 1;
     }
     else
     {
        return 0
     }
}
1;

call.pl

require "script.pl";
my $no = 123;
if (checkevenodd($no))
{
    print "it is even";
}
else
{
   print "it is odd";
}

I use the function to check whether is it 1 or 0... Then if it is 1, it is even or else odd. So which method is best for receiving the boolean value from the function?? I do not want to create a variable. Instead I want to return 1 or 0.. How to get the 1 or 0.. Is it correct method??

10
  • 1
    Zero and one are considered false and true respectively. So you can simply use the short form: if (checkevenodd($no)) {...}. I think this implicit form is also more correct, since you are actually checking if the condition is true or false, and not comparing integers. Commented Sep 21, 2017 at 7:32
  • 0 is false, but 1 is not exactly true, it is just one possible value for true. True is everything not being false. For example 2 is also true. Commented Sep 21, 2017 at 7:38
  • @ceving, 0 is just one of many false values as well. Commented Sep 21, 2017 at 7:59
  • Having the subroutine in a different file is a complete red herring here. It has nothing to do with the problem at all. Commented Sep 21, 2017 at 8:02
  • i dont understand on why is there no boolean variable in perl but other programming have that Commented Sep 21, 2017 at 8:11

2 Answers 2

2

When you write:

if (checkevenodd($no) == 1)

You are not checking for a Boolean value. You are checking for the value 1. In this case it will work (because checkevenodd() only ever returns 0 or 1) but, in general, you should only ever check the truth of a Boolean expression, not its value. It's much better to write:

if (checkevenodd($no))

A couple of other points.

  • checkevenodd is not a good name for this subroutine. When I have subroutines that return Boolean values, I always try to give them a name that starts with is_. Your subroutine returns true if the number is even, so I would call this subroutine is_even().
  • Your subroutine is far more complex than it needs to be. I would write it as:

    sub is_even {
      my ($num) = @_;
    
      # For an even number, $num % 2 is zero - which is false.
      # Therefore use ! to make it true.
      return ! $num % 2;
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1

How about something like this?

sub even_odd {
     my ($num) = @_;
     my $remainder = $num % 2;
     return $remainder ? 0 : 1;

}

and using it as a complete script:

my $no = 123;
if (even_odd($no))
{
    print "it is even";
}
else
 {
   print "it is odd";
}

sub even_odd {
     my ($num) = @_;
     my $remainder = $num % 2;
     return $remainder ? 0 : 1;

}

returns: it is odd

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.