Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Matts (1087)

  (email not shown publicly)

I work for MessageLabs [] in Toronto, ON, Canada. I write spam filters, MTA software, high performance network software, string matching algorithms, and other cool stuff mostly in Perl and C.

Journal of Matts (1087)

Monday July 08, 2002
04:19 PM

Exception objects

[ #6224 ]

While writing my talk on Exceptions for OSCon, of course I'm talking about exception objects. Now I like to check that what I'm talking about is accurate, so I write little perl scripts for the example code I'm running.

I started to talk about how when you're throwing exception objects, you have to check ref($@) first (or blessed($@) if you have Scalar::Util available), before you check $@->isa() [*].

use strict;
eval {
die "failed\n";
if ($@) {
  if ($@->isa('Foo')) {
    warn("got foo\n");
  else {
    warn("got $@\n");

Of course I tested this, and discovered it's not true! When you test $string->isa() it just tries to convert $string to a package name, and checks the isa() sub in that package, which inherits from UNIVERSAL. So it doesn't actually fail. Even under -w and use strict, since calling methods on a string isn't a strict operation (and so it shouldn't be).

So does anyone know where I got this idiom from? I know I've been doing it for years, at least since I was working on both WebBoard (with O'Reilly) and on AxKit.

[*] Yes, I'm aware of UNIVERSAL::isa - I just consider it a pain to type ;-)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I think you may have gotten that from me when we worked on WebBoard.

    One thing that could have caused it is if we were doing this:

      eval {  ... };

      if ( $@->isa(...) )

    That'll fail if $@ is undef, which may have led us (or me) to start using UNIVERSAL::isa or something like that.