in reply to UNIVERSAL::isa() vs. ref()
ref($obj) will only give you the one class $obj was blessed into most recently (since a single object cannot be blessed into more than one class, it only has one class at any time). isa takes an argument, and tells you if $obj is one of those. For example, $obj->isa('thing') will tell you if $obj is blessed as a 'Thing' or any class that inherits from 'Thing'. (as you can see, everything isa->('UNIVERSAL').)
so it depends what you want to know; if you only care what exact object this was blessed as, use ref. If, on the other hand, you want to know if $obj is descended from a particular class, use isa. note that if your code may grow in the future, it may turn out you would prefer to use isa() because it makes your class more subclassable by making the question more general. if i am blessed as a Geek::Perl, and you buy everyone who is a Geek::Programmer a beer, then you should buy me a beer (because Geek::Perl inherits from Geek::Programmer; therefore every Geek::Perl isa Geek::Programmer).
note, too, that often people use isa when what they want to know is can.
for more on isa and can, see Damian Conway's Object Oriented Perl
.update: if all you're trying to find out is if it is a ref at all, ref is the right thing to use, IMO. Also, if all you're checking for is the builtins, there's no need to deal with subclassing (though tied variables may give you headaches). And, of course, you can't use the $obj->isa('ARRAY') syntax i show above, since HASH, ARRAY, and SCALAR refs aren't objects (until perl6...). In short, having seen your situation, i'd use ref.
.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: UNIVERSAL::isa() vs. ref()
by Anonymous Monk on Jun 14, 2001 at 21:31 UTC |