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??
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.