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.
.
| [reply] [d/l] [select] |
What gives bless $ref,'ARRAY'? Of course use ARRAY if necessary.
| [reply] [d/l] [select] |
Arturo mentioned I should give a context for this, so here it is. I'm allowing users of this module-set I'm writing to pass me stuff either as a blah or a ref to blah. So, I need to determine if it's a ref or not, and what kind of ref is it. Here's the code I've got: (Other comments on the code are welcome!)
sub coerce_args {
my $self = shift;
my $type = shift;
local $_;
# This is needed to guarantee that $type is a string that can be
# compared.
return @_ if !defined $type || $type eq '' || $type =~ /\d/;
if ($type eq 'ARRAY') {
# if ($_[0]->isa($type)) {
if (ref($_[0]) eq $type) {
return @{$_[0]};
} else {
return @_;
}
} elsif ($type eq 'HASH') {
# if ($_[0]->isa($type)) {
if (ref($_[0]) eq $type) {
return %{$_[0]};
} else {
return @_;
}
} elsif ($type eq 'SCALAR') {
# if ($_[0]->isa($type)) {
if (ref($_[0]) eq $type) {
return ${$_[0]};
} else {
return $_[0];
}
#GGG This one is a little confusing...
# } elsif ($type eq 'NONE') {
# return @_;
} else {
return @_;
# Unknown $type?? Maybe this is for objects?
}
}
| [reply] [d/l] |
| [reply] |
I'm writing to pass me stuff either as a blah or a ref to blah
My first question is: why such flexibility in the API.
My comment is: I don't like the way the user has to pass a type and tell you the type. Why don't you simply require that the user pass a reference to the datatype and then your job is easier. Further, there will be no chance of a user making a typographical error such as typing ARRY when he meant ARRAY.
| [reply] |
Oh, this is a private method. The actual public method would look something like:
sub my_public {
my $self = shift;
my @args = $self->coerce_args('ARRAY', @_);
....
}
Or, that could be a hash. The thing was that I couldn't imagine wanting to have anything other than a list(ref) or hash(ref), but I didn't want to limit myself to those, unless those were the only possibilities at all. Maybe I was being too generic? | [reply] [d/l] |