|Perl: the Markov chain saw|
Re: inheritance: constructorsby djantzen (Priest)
|on Jun 24, 2002 at 23:22 UTC||Need Help??|
Short answer: $classname->SUPER::new(@_)
Mimicking Java's inheritance behavior exactly is difficult in Perl without adding a bunch of overhead that the Java compilers normally handle; namely, inserting calls to super() in cases where your subclass constructor does not explicitly call the superclass constructor, and also generating zero-argument constructors to be used by subclasses of your subclass. In Java, every time you create an object you instantiate the class's ancestors all the way up to java.lang.Object, and in order to facilitate this there must be generic, default constructors available. Update/Correction: The compiler will insert a constructor for you in the total absence of a constructor. If you've already got one that takes arguments, it's up to you to provide a zero-arg version unless you wish to force subclasses to specify arguments at all times.
You can get this behavior in Perl with code like:
An easier solution is to use named lists to pass arguments and to stuff those into a hash.
My current favorite way to handle inheritance is to separate creation from initialization. Write a constructor in your top-level class to be inherited, like:
Any subclass can then define a "protected" method _init to 1) call superclass initialization methods, and 2) finish initialization for this class.
As always, mad props to TheDamian's book Object Oriented Perl