I have a method in a class that needs to make sure that it is called only for an instance of the object, and not as a class method.
I will probably do something like this:
sub foo
{
my ($self) = @_;
if (ref($self) ne __PACKAGE__) { return; }
...do stuff
}
But I think it will be more efficient:
sub foo
{
my ($self) = @_;
if (not ref($self)) { return; }
...do stuff
}
Questions:
Is it possible to assume that if ref () does not return undef, that it will return the current package?
Ideally, I would like to go back and do something similar in all of my health testing methods. It is a bad idea?
Is there an easier way to do what I want?
" " . , , , , . .
!
EDITED, , ref undef, .
2 . - :
$self->isa(__PACKAGE__)
? , , - , :
MyClass::MyMethod($ref_to_some_other_object)