Regarding Inheritance

Discussion in 'Perl Misc' started by Mahesh, Dec 17, 2004.

  1. Mahesh

    Mahesh Guest

    I have two packages package1 and package2.

    Suppose this the code of package1
    package package1;

    my $file_scope_var = "var in package1"

    sub new
    {
    my $proto = shift;
    my $self = { };
    my $class = ref ($proto) || $proto;
    bless ($self, $class);
    $self->{NAME} = undef;
    }

    sub print_package1
    {
    my $self = shift;
    my $name = $self->NAME();
    my $file_scope_var = $self->file_scope_var();
    print"\n\n Name is $name and file scope var is $file_scope_var";
    }


    suppose the code of package2 is
    package package2
    @ISA=("package1");

    sub new
    {
    my $proto = shift;
    my $self = { };
    my $class = ref ($proto) || $proto;
    }

    sub print_package2
    {
    my $self = shift;
    $self->print_package1();
    }

    now my problem is that since Iam inheriting package1 into package2.
    and Iam calling print_package1 in package2.

    Will that call to print_package2 will print file_scope_var value or
    not. Since it is a file scope variable will it get printed when we call
    print_package2 or not.

    Please help me in this soon as I was stuck in the middle of the work.
    Thanks and Regards,
    Mahesh
    Mahesh, Dec 17, 2004
    #1
    1. Advertising

  2. Mahesh wrote:

    > Will that call to print_package2 will print file_scope_var value or
    > not. Since it is a file scope variable will it get printed when we call
    > print_package2 or not.


    Have a look at "perldoc perltooc", in the section "Inheritance Concerns"
    for a discussion of class variables and inheritance mix.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Dec 17, 2004
    #2
    1. Advertising

  3. Sherm Pendley, Dec 17, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darkstorm
    Replies:
    3
    Views:
    350
    Malte Starostik
    Mar 29, 2005
  2. He Shiming
    Replies:
    2
    Views:
    343
    Janusz Szpilewski
    Jun 10, 2005
  3. Anarki
    Replies:
    5
    Views:
    465
    James Kanze
    Jul 7, 2007
  4. Replies:
    1
    Views:
    425
    Alf P. Steinbach
    Feb 10, 2008
  5. Angus
    Replies:
    3
    Views:
    337
    H. S. Lahman
    Jan 4, 2009
Loading...

Share This Page