I'd rewrite your code like this:
if( ! ref($r) ) {
#no reference at all
} elsif( ! eval { $r->can('can') } ) {
#unblessed ref
} else {
#blessed ref
}
because I think the eval{} seems likely to be more expensive than the ref(). But even more important, if $r is, for example, the simple string "My::Package", then
$r->can('can') will succeed (which is also a bug in
merlyn's
version), even if there is no package called My::Package. In fact, try
"This is a test"->can('can') for fun. (:
As for the difference between
eval { $r->can('can') } and
eval { $r->isa('UNIVERSAL') }, the only advantage I see to the first is fewer keystrokes. I find the latter clearer, personally. But that gave me the idea for
$r->can('isa'), which I find clear (as well as short). I guess isa() screams "object" to my brain faster than can() does, not sure why.
-
tye
(but my friends call me "Tye")