in reply to How to return an appropriate error from module?
Funny, I'm coding a schema validator too at the moment and returning hash (more precisely, hashref) like {success=>1, data=>..., ...} is also my preferred way.
Compared to the ($success, $res) tuple, hash gives you more flexibility as you can add more keys laters should you need one.
I'm too lazy creating "result objects" and I think it's an overkill anyway. Oftentimes you just want to give the user an error code and the output without much behaviour at all, so object is not particularly necessary here.
Exception is actually very handy, but I often deal with cross-language, cross-computer communication and convention about remote exception is not that well defined (at least in Perl?). Also I've always had this feeling that Perl5 is not really "exception-based". I'd wait for Perl6 for that, but that's just me of course.
A simple data structure like hash, on the other hand, is supported by virtually any decent programming language (even bash nowadays, I've heard).
There's another style which you could try:
$success = validate($data, $output);
$output is an optional argument which if given will be filled with the output by validate(). Sometimes you don't need the output at all and just need the fail/success boolean status.
|
|---|