Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to Re: inheritance: constructors by djantzen
in thread inheritance: constructors by Basilides

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (9)
    As of 2014-08-22 14:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (158 votes), past polls