Should instance methods in Perl call each other like this:
package BlaBla;
sub foo {
my($self) = @_;
#do something awesome;
}
sub bar {
my($self) = @_;
foo();
}
Or like this:
package BlaBla;
sub foo {
my($self) = @_;
#do something awesome
}
sub bar {
my($self) = @_;
$self->foo();
}
Thank you all for your time.
Yes, you should include
$self– if you don’t, polymorphism won’t work because each method will look for all calls in its own package, even if they’re overridden in a subclass. Worse, if the referenced method is inherited from a parent and not overridden in the current subclass, the program willdiebecause it doesn’t exist in that package. And that’s even if you do remember to try to fake out Perl’s OO mechanism by passing$selfas the first parameter.