T
Tore Aursand
Hmm. Don't know if I got the subject totally right. Just take a look at
the example instead.
I'm having problems with simple OOP inheritance. Seems like I've been
away from the OOP business too long. I've read through perlboot, perltoot
and perltooc.
Here's some sample code:
package Person;
use strict;
use warnings;
sub new {
my $class = shift;
my $self = {
'firstname' => '',
'lastname' => '',
'age' => 0,
};
bless( $self, $class );
$self->_init( @_ );
return $self;
}
sub _init {
my $self = shift;
my %ARGS = @_;
$self->{'firstname'} = $ARGS{'firstname'} || '';
$self->{'lastname'} = $ARGS{'lastname'} || '';
$self->{'age'} = $ARGS{'age'} || 0;
}
1;
package Employee;
use strict;
use warnings;
use Person;
our @ISA = qw( Person );
sub new {
my $class = shift;
my $self = $class->SUPER::new( @_ );
$self->{'salary'} = 0;
bless( $self, $class );
$self->_init( @_ );
return $self;
}
sub _init {
my $self = shift;
my %ARGS = @_;
$self->{'salary'} = $ARGS{'salary'} || 0;
}
1;
This code may very well work as intended, but when I create a new instance
of the Employee class, only Employee::_init() is run (and it is run twice);
#!/usr/bin/perl
#
use strict;
use warnings;
use Employee;
my $Employee = Employee->new();
I really thought this would spawn into this:
Employee::new()
Person::new()
Person::_init()
Employee::_init()
But it doesn't. Instead it is executed in this manner:
Employee::new()
Person::new()
Employee::_init()
Employee::_init()
Anyone care to explain what's going on and what I need to do in order to
make this work as I want it to?
Thanks!
the example instead.
I'm having problems with simple OOP inheritance. Seems like I've been
away from the OOP business too long. I've read through perlboot, perltoot
and perltooc.
Here's some sample code:
package Person;
use strict;
use warnings;
sub new {
my $class = shift;
my $self = {
'firstname' => '',
'lastname' => '',
'age' => 0,
};
bless( $self, $class );
$self->_init( @_ );
return $self;
}
sub _init {
my $self = shift;
my %ARGS = @_;
$self->{'firstname'} = $ARGS{'firstname'} || '';
$self->{'lastname'} = $ARGS{'lastname'} || '';
$self->{'age'} = $ARGS{'age'} || 0;
}
1;
package Employee;
use strict;
use warnings;
use Person;
our @ISA = qw( Person );
sub new {
my $class = shift;
my $self = $class->SUPER::new( @_ );
$self->{'salary'} = 0;
bless( $self, $class );
$self->_init( @_ );
return $self;
}
sub _init {
my $self = shift;
my %ARGS = @_;
$self->{'salary'} = $ARGS{'salary'} || 0;
}
1;
This code may very well work as intended, but when I create a new instance
of the Employee class, only Employee::_init() is run (and it is run twice);
#!/usr/bin/perl
#
use strict;
use warnings;
use Employee;
my $Employee = Employee->new();
I really thought this would spawn into this:
Employee::new()
Person::new()
Person::_init()
Employee::_init()
But it doesn't. Instead it is executed in this manner:
Employee::new()
Person::new()
Employee::_init()
Employee::_init()
Anyone care to explain what's going on and what I need to do in order to
make this work as I want it to?
Thanks!