Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

(tye)Re: can in Re: ref, no, maybe?

by tye (Sage)
on Jan 11, 2001 at 19:35 UTC ( [id://51127] : note . print w/replies, xml ) Need Help??


in reply to can in Re: ref, no, maybe?
in thread ref, no, maybe?

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")