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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With