David Wright wrote:
Your $thingy could be a hashref, in which case $thingy->isa will die.

The point of the discussion is that you should be checking if $thingy is blessed() first, as UNIVERSAL::isa breaks for objects that masquerade as other objects (e.g. via an adaptor pattern).

I've been using it a lot recently to catch exceptions. What's so wrong with the below, almost identical to the example in perldoc -f die? I'd rather not die again immediately by assuming [EMAIL PROTECTED]>isa will work.

eval {
        # do some stuff
};

if ( $@ ) {
        if( UNIVERSAL::isa($@, 'My::Exception') ) {
           # known exception, handle appropriately
        }
        else {
           die "Ooops-a-daisy: $@";
        }
}

Exception::Class now offers the "caught" method so you don't have to use UNIVERSAL::isa that way. I also wrote Exception::Class::TryCatch for a little more helpful sugar:

  eval {
     # do stuff;
  };

  # catch() upgrades non-object $@ to Exception::Class::Base
  if ( catch my $err ) {
    if ( $err->isa('My::Exception') ) {
      # handle it
    }
    else {
      $err->rethrow;
    }
  }

Regards,
David Golden

Reply via email to