Geoffrey Young wrote:
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
      }
  }

I wrote a bunch of code and it works perfectly well, when there is an error. However if you do:


eval {
  some_other_call();
  $socket->recv($buff, $rlen) };
}
die "$@" if $@ && $@ == APR::TIMEUP;

and some_other_call() throws a plain perl exception (die "foo"), we have a problem, as the above code will generate:

Argument "foo ..." isn't numeric ...

since normally $@ doesn't have the IV slot populated. While we could still proceed with it and say that it works only for one call eval {}, it's not too nice for the users.

So, next I'm trying to use exception objects with overloaded methods. Here is a demo, this time in perl:

package Err;

use overload
    'bool' => \&num,
    '=='   => \&num,
    '0+'   => \&num,
    '""'   => \&str;

sub str { my $self = shift; $self->{str};}
sub num { my $self = shift; $self->{num};}

sub new {
    my ($class, $num, $str) = @_;
    bless {
        num => $num,
        str => $str,
    }, __PACKAGE__;

}

package main;

sub foo {
    my $name = shift;
    my $err = Err->new(1234, "$name, stay cool!");
    die $err;
}

eval { foo('gozer'); };
if ($@ && ref $@ && $@ == 1234) {
    warn "The error was: $@";
}

foo('geoff');

so, running it produces:

% perl-5.8.4-ithread proto2.pl
The error was: gozer, stay cool! at proto2.pl line 31.
geoff, stay cool!

As you can see, what happens is, if you don't trap the exception the reloaded stringification operator dumps the full error message (I'm planning to generate it on demand, rather then populate it when the error occurs). And yes, I still miss the line/file info, but it'll be there.

If you trap the exception, you can now certainly know whether this is a mod_perl $@ exception (by calling ref()), before doing $@ == 1234, which in turn invokes the overloaded numerical methods and gives us the return status as a number.

Moreover by using the object we may do even more things in the future.

As by now I've found that all my attempts have stumbled into some kind of wall, I'm not 100% sure that this is the perfect solution, but I'm going to give it a try next.

As always, your comments and ideas are welcome.

__________________________________________________________________
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]



Reply via email to