qazwart has asked for the wisdom of the Perl Monks concerning the following question:
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 "Whoops! Something went terribly wrong!\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:if (foo()) { print "Everything is copacetic!\n"; } else { print "Error: $!\n"; }
($value, $errorMsg) = foo(); if ($value) { print "Everything is copacetic\n"; } else { print "Error: $errorMsg\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Returning Error Codes
by gellyfish (Monsignor) on May 25, 2006 at 20:31 UTC | |
|
Re: Returning Error Codes
by philcrow (Priest) on May 25, 2006 at 20:34 UTC | |
|
Re: Returning Error Codes
by ioannis (Abbot) on May 26, 2006 at 02:07 UTC | |
|
Re: Returning Error Codes
by Sandy (Curate) on May 25, 2006 at 20:48 UTC | |
|
Re: Returning Error Codes
by duckyd (Hermit) on May 26, 2006 at 04:48 UTC | |
|
Re: Returning Error Codes
by qazwart (Scribe) on May 30, 2006 at 15:05 UTC |