Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When writing a Perl module, do I have to specifically check for an object instance in all my methods?

Tags:

oop

perl

I'm writing a module that has several methods. Let's consider this :

package MyPackage;
sub new {
 ...
}

sub do_your_job {
 ...
}
1;

What is to stop someone calling do_your_job like MyPackage->do_your_job instead of $obj->do_your_job ? Do I need to check in every method that I'm receiving a reference as the first argument?

like image 214
Geo Avatar asked Nov 29 '22 04:11

Geo


1 Answers

No. If your module features only the most minimal documentation, it should be clear from that that do_your_job is a method that needs to be called on an object. If someone wants to call that in some other way, it's his fault.

Of course, you could check whether the first parameter to your method is a blessed object. But this has at least two drawbacks: You lose a little bit of performance and you clutter your methods with code that really doesn't do anything promised by the method's name.

like image 182
innaM Avatar answered Dec 18 '22 21:12

innaM