Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: inheritance: constructors

by djantzen (Priest)
on Jun 24, 2002 at 23:22 UTC ( #176984=note: print w/replies, xml ) Need Help??

in reply to inheritance: constructors

Short answer: $classname->SUPER::new(@_)

Long answer:

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:

sub new { my ($class, @args) = @_; my $this; unless (defined @args) { $this = $class->SUPER::new(); # call zero-arg ctor return bless($this, $class); # rebless to our class } else { # do argument checking to figure out what ought to go # to the superclass ctor and what needs to be initialized here. $this = $class->SUPER::new(@some_args); # then initialize the parts of the object that are unique to this +class return bless($this, $class); # rebless to our class }

An easier solution is to use named lists to pass arguments and to stuff those into a hash.

sub new { my ($class, %args) = @_; my $this = $class->SUPER::new(%args); # Do additional processing on the args, e.g ... $$this{foo} = $args{foo}; return bless($this, $class); # rebless to our class }

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:

package SomeSuperclass; sub new { my $class = shift(); my $this = {}; bless($this, $class); $this->_init(@_); return $this; }

Any subclass can then define a "protected" method _init to 1) call superclass initialization methods, and 2) finish initialization for this class.

package SomeSubclass; use base SomeSuperclass; sub _init { my ($this, $foo, $bar, $baz) = @_; $this->SUPER::_init($bar, $baz); $$this{foo} = $foo; }

As always, mad props to TheDamian's book Object Oriented Perl

Replies are listed 'Best First'.
Re^2: inheritance: constructors
by Anonymous Monk on Dec 01, 2012 at 02:50 UTC
    Please, I beg you, do not tell people they should use an else/unless construct. Ever. I wish this wasn't even possible. People need to think in the proper order.

        Sorry, I'm not buying. This is not ok? --

        if ( $value != $expected_value ) { $errorcount++; die new Exception msg => "argh!"; } else { # continue with the happy path }

        But this is ok? --

        if ( $value != $expected_value ) { $errorcount++; die new Exception msg => "argh!"; } # continue with the happy path

        no, no, no.

        I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

        You assume the order of the code blocks does not matter, too most ppl it does.

        Either: if ($i != $j) or: unless($i == $j)

        Chose one, these are the same. Your example changes much more than just removing a double negative.

      See? I can't even think "unless/else" properly. Ugh. Backwards thinking is why so many of us Perlmongers get made fun of by the other programmers. And why our successors curse our names when they have to maintain our code. Stop the madness.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://176984]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-18 05:47 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (108 votes). Check out past polls.