PERL OOP - Inheritance

Discussion in 'Programming' started by pixel_boost, Oct 4, 2007.

  1. #1
    Hi,
    I'm new to perl. After reading perlboot, perltoot, perltooc, etc(find them here) I managed to understand a little bit about OOP in perl.
    But i have a problem, suppose that i have the main class A:
    
    package a;
    use strict; use warnings;
    my $var = "default_value";
    sub new {
    	my ( $class ) = shift;
    	my $self  = {};
    	bless ($self, $class);
    	return $self;
    }
    sub output{
            my ($self, $var) = @_;
            print "$var\n";
    }
    1;
    
    Code (markup):
    And class B inheriting from class A:
    
    package B;
    use base A;
    use strict;
    use warnings;
    
    sub output{
           my( $self, $var ) = @_;
           #i want to get access to $var from Parent Class
           # so this should print 'default_value'
           print $self->SUPER->{var} . "\n";
    }
    1;
    
    Code (markup):
    How can i manage to get access to $var from class B. The current code doesn't work.
    Thanks and sorry for the long post :)
     
    pixel_boost, Oct 4, 2007 IP
  2. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #2
    You need to use name resolution :: operator.
     
    it career, Oct 8, 2007 IP
  3. pixel_boost

    pixel_boost Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for suggestion, i'll try.
     
    pixel_boost, Oct 9, 2007 IP