No, adrianh is absolutely correct. In fact, this is very good style because it allows inheritance of behavior while allowing alterations to that behavior transparently based upon the concrete subclass you are actually instantiating.

Here's an example of bad, inflexible, child-snooping that you want to be concerned about:

package FamilyMember; # the parent class of Son, Daughter sub new { my ($class, $args) = @_; my $self = bless({}, $class); if ($class eq 'Son') { $self->askSantaFor('A Truck!'); } elsif ($class eq Daughter') { $self->askSantaFor("Subscription to Martha Stewart's 'Living'" +); } return $self; } sub askSantaFor { my ($self, $wish); push @{$self->{things_to_ask_for}}, $wish; }

What happens if you need to add a new subclass called StepDaughter? You have to go to the superclass and modify the constructor. The better way is just what you've done, and in this silly example the solution would be either to override askSantaFor in each subclass, or (even better) provide a class specific method, perhaps called wishList, that provides the appropriate parameters to the method called from the superclass constructor. Thus the above example code could be changed to:

sub new { my ($class, $args) = @_; my $self = bless({}, $class); $self->askSantaFor($class->wishList()); return $self; }

Hope that helps, DJ


In reply to Re: Superclass acessing sublass data by djantzen
in thread Superclass acessing sublass data by pauloschreiner

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.