Some problems with the code shown:

But the main problem is the structural relationship of the child classes to the parent class. Here is one way to do what is wanted:

#! perl use strict; use warnings; #--------------------------------------------------------------------- +--------- { package SuperModule; sub new { my ($class, %params) = @_; my $self = { dbh => $params{DB} }; return bless $self, $class; } sub print_super { my ($self) = @_; print 'Super module: ', $self->{dbh}, "\n"; } } #--------------------------------------------------------------------- +--------- { package MyModule; our @ISA = qw( SuperModule ); sub new { my ($class, %params) = @_; my $self = SuperModule::new($class, %params); $self->{file_name} = $params{'file name'}; return bless $self, $class; } sub print_file { my ($self) = @_; print 'My file name from file obj ', $self->{file_name}, "\n"; } } #--------------------------------------------------------------------- +--------- { package YourModule; our @ISA = qw( SuperModule ); sub new { my ($class, %params) = @_; my $self = SuperModule::new($class, %params); $self->{block_name} = $params{'block name'}; return bless $self, $class; } sub print_block { my ($self) = @_; print 'My block name from block obj ', $self->{block_name}, "\ +n"; } } #--------------------------------------------------------------------- +--------- my %f_params = ( DB => 'File DB value', 'file name' => 'Lisa', ); my %b_params = ( DB => 'Block DB value', 'block name' => 'Pisa', ); my $file_obj = MyModule ->new(%f_params); my $block_obj = YourModule->new(%b_params); $file_obj ->print_super(); $file_obj ->print_file(); $block_obj->print_super(); $block_obj->print_block();

Output:

13:56 >perl 567_SoPW.pl Super module: File DB value My file name from file obj Lisa Super module: Block DB value My block name from block obj Pisa 13:56 >

Hope that helps,

Update: ++tobyink, below, for the improvements to the child class constructors.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Variables in Common parent class by Athanasius
in thread Variables in Common parent class by perlbaski

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.