Thoughts on UNIVERSAL::isa

Ovid on 2007-12-07T17:31:19

Were it not for the latest release of UNIVERSAL::isa, we might have removed Test::MockObject from out test suite due the the huge numbers of warnings emitted. From our deps/ directory (these are all CPAN modules .. we ship our own dependencies):

$ ack -l 'UNIVERSAL::isa\(' perl5lib/|wc -l
     103

No, we don't have time to analyze 103 different packages and submit patches (with tests) which may or may not fix bugs but are really only sent to silence warnings in our test suite. We have the test suite to catch bugs and not one of those warnings appeared to be a bug. However, they could be. Here's another check:

ack  'sub isa ' perl5lib/
perl5lib/Exception/Class.pm
585: sub isa { shift->rethrow }

perl5lib/Contextual/Return.pm
898:sub isa {

Rethrowing an exception for Exception::Class is a bit surprising, but I can understand why it's done there. Contextual::Return also does the right thing. Unfortunately, we have over 100 CPAN dependencies which would handle these wrong if they encountered them. Sigh. Of course, we only use a small subset of the CPAN. Anyone happen to know offhand how many CPAN modules override isa and how many get it wrong? I can check when I get home, I suppose.

So with the new version of UNIVERSAL::isa, we can stay happy and get some safety against the insanity of calling this as a function without having unusable test results.

If you really must do that (usually you don't), you can do this (thanks to merlyn for this trick):

if ( eval { $thing->isa($class) } ) {


err, that's pod

autarch on 2007-12-07T20:40:03

That bit of code supposedly from Exception::Class is just an example of something you could do in the pod. It's not actually in the code itself. I'm not insane.

Re:err, that's pod

Ovid on 2007-12-07T20:48:23

Oops :)

Thanks for Testing!

chromatic on 2007-12-08T05:04:43

I'll take that as one vote to release the developer version as stable, then.

eval?

Alias on 2007-12-08T22:58:21

You surely mean of course (defined $it and blessed $it and $it->isa('Foo'))...

Re:eval?

Ovid on 2007-12-09T01:37:42

That's a lot longer to type and while it's theoretically clearer, I don't think it wins over an eval. Did I miss something obvious?

Re:eval?

Aristotle on 2007-12-09T03:09:24

In general, using eval willy nilly is not good because you might swallow an exception that should have bubbled up. But a properly working isa is not expected to throw exceptions (or so I hope), so this idiom seems just fine.

Re:eval?

chromatic on 2007-12-10T16:56:40

You're absolutely right. An overridden isa() that throws an would violate the Liskov substitution principle, so it would not in fact work properly.

Re:eval?

jjore on 2007-12-11T00:25:37

No, I think you meant

defined( blessed $it ) and $it->isa( '...' )
. blessed() is not a sufficient boolean on its own. There are two false classes in perl.