How can I use a Perl function to return a boolean value and a string?
For example, I am using regular expression to find something in a string. If found, return true and assign value to $msg, otherwise return false and assign another value to $msg.
Now I want to return both $result (boolean) and $msg (String) by "return ($result, $msg); ", and it's verified working. I am wondering if it is good to do it like this or is there a better way? I don't want to make other people confused when they are looking at my code.
use constant { true => 1, false => 0 };
my $output = "hello world";
my $msg;
my $result;
sub is_string_found
{
if ($output =~ /hello/;)
{
$msg = "string is found";
$result = true;
}
else
{
$msg = "string is not found";
$result = false;
}
return ($result, $msg);
}
trueandfalsedon't really exist in perl as such.