Returning status codes and handling them makes your code very messy, especially if you want to prototype something. If we croak on error, then you can catch it in eval, but there is a problem - you don't want to match the error message, but the status code. For example if the exception on read is APR_TIMEUP you won't to be able to access that code. So here is what I propose that we use for exception handling. Let's say that apr_status_t is APR_TIMEUP, I'll hardcode it for this example:
status = APR_TIMEUP;
char *err = modperl_apr_strerror(status);
SV *errsv = get_sv("@", TRUE);
sv_setpv(errsv, err);
SvIVX(errsv) = status;
SvIOK_on(errsv);
Perl_croak(aTHX_ Nullch);I couldn't show it in pure perl, since die $err, where $err is a dual object doesn't make it through -- perl copies the IV slot only. But it works fine from C.
Basically it's somewhat similar to exception handling with objects, but here we use a dualvar $@ instead.
So you can write your code without any error handling:
$socket->recv($buff, $rlen);
And if something goes wrong, Perl will automatically croak for you, using the full message:
[Thu Apr 29 11:33:54 2004] [error] 70007:The timeout specified has expired
Though if you decide to catch it, you can do this:
eval { $socket->recv($buff, $rlen) };
if ($@) {
if ($@ == APR::TIMEUP) {
warn "timed out, giving up";
}
else {
die $@; # rethrow, unhandled error
}
}$@ in the numerical context gives us what we at the moment return, in the string context it's APR::strerror($rc);
So IMHO this proposal gives us the best of all aspects:
- You can code w/o any error handling, and no silent errors will ever occur. I like this more than current perl's core functions which won't croak unless you checked the error.
- You can still trap the errors and access the raw numerical return code for comparison against error constants
- You get the stringification of the error w/o needing to call APR::strerror($rc);
- We no longer need to return the status from each function and can make the APIs more perlish.
- We use a commonly known variable $@, which works exactly the same as with any other program, but has a special IV value.
- Finally the API is going to be consistent.
- One exception is for functions that may still need to return status codes, which aren't errors (but nothing comes to my mind at the moment, besides APR::EOF, which we can handle similar to perl's read, i.e. returning 0 when there is nothing to read.)
One issue I can see so far is that an uncaught croak:
[Thu Apr 29 11:33:54 2004] [error] 70007:The timeout specified has expired
doesn't report the context (file/line), so I'm going to figure out how to add it manually and it should be fine.
__________________________________________________________________ Stas Bekman JAm_pH ------> Just Another mod_perl Hacker http://stason.org/ mod_perl Guide ---> http://perl.apache.org mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com http://modperlbook.org http://apache.org http://ticketmaster.com
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
