I am writing a Perl module, and I'd like to return an error code of sorts if there is a problem with the fuction.
Normally, you simply return a zero on an error and a non-zero on a normal return. That way you can do this:
if (foo()) {
print "Everything is copacetic!\n";
} else {
print "Whoops! Something went terribly wrong!\n";
}
So, if
foo() returns a zero, I know there's an error. However, I don't know what type of error I got. What I'd like to do is something like this:
if (foo()) {
print "Everything is copacetic!\n";
} else {
print "Error: $!\n";
}
Now, I
know this won't work because
$! is for system errors, and I'm returning my own personal error.
However, it would be nice to be able to test the return value of my function, and if it fails, give you a reason why I'm returning an error.
So, is there anyway of doing this?
(Yes, I know I could return an array, so the first value is my return value and the second is my error message, but then the "if" statement doesn't test the function directly:
($value, $errorMsg) = foo();
if ($value) {
print "Everything is copacetic\n";
} else {
print "Error: $errorMsg\n";
}