P
Peter Michaux
Hi,
I'm new to Perl (from C, JavaScript, Ruby) and going through the Camel
book.
Below is my an example of trying to write Ruby/Java style class-based
inheritance in Perl. (In JavaScript, Ruby and Java I would write like
this <URL: http://peter.michaux.ca/article/1>) The first eight lines
of each new subroutine below seems quite clunky. I'm trying to make it
so the new can be called either to instantiate a new object or be
called as super from a child class's constructor. I only want to name
the parent class once in the @ISA and use SUPER everywhere else so I
can change the inheritance chain easily.
package Person;
sub new {
my $invocant = shift;
my $this;
if (ref($invocant)) {
$this = $invocant;
} else {
$this = {};
bless($this, $invocant);
}
my ($name) = @_;
$this->{name} = $name;
return $this;
}
sub toString {
$this = shift;
return $this->{name};
}
# ---------------------------------------------
package Employee;
our @ISA = ('Person');
sub new {
my $invocant = shift;
my $this;
if (ref($invocant)) {
$this = $invocant;
} else {
$this = {};
bless($this, $invocant);
}
my ($name, $id) = @_;
$this->SUPER::new($name);
$this->{id} = $id;
return $this;
}
sub toString {
my $this = shift;
return $this->SUPER::toString() . ': ' . $this->{id};
}
# ---------------------------------------------
$ted = Employee->new('Ted', 10);
print ($ted->toString(), "\n");
Any suggestions to clean up the clunky parts of the new subroutines? I
expect that some will say "this is not how you do OOP Inheritance in
Perl." Other options?
Thank you,
Peter
http://peter.michaux.ca
http://forkjavascript.org
I'm new to Perl (from C, JavaScript, Ruby) and going through the Camel
book.
Below is my an example of trying to write Ruby/Java style class-based
inheritance in Perl. (In JavaScript, Ruby and Java I would write like
this <URL: http://peter.michaux.ca/article/1>) The first eight lines
of each new subroutine below seems quite clunky. I'm trying to make it
so the new can be called either to instantiate a new object or be
called as super from a child class's constructor. I only want to name
the parent class once in the @ISA and use SUPER everywhere else so I
can change the inheritance chain easily.
package Person;
sub new {
my $invocant = shift;
my $this;
if (ref($invocant)) {
$this = $invocant;
} else {
$this = {};
bless($this, $invocant);
}
my ($name) = @_;
$this->{name} = $name;
return $this;
}
sub toString {
$this = shift;
return $this->{name};
}
# ---------------------------------------------
package Employee;
our @ISA = ('Person');
sub new {
my $invocant = shift;
my $this;
if (ref($invocant)) {
$this = $invocant;
} else {
$this = {};
bless($this, $invocant);
}
my ($name, $id) = @_;
$this->SUPER::new($name);
$this->{id} = $id;
return $this;
}
sub toString {
my $this = shift;
return $this->SUPER::toString() . ': ' . $this->{id};
}
# ---------------------------------------------
$ted = Employee->new('Ted', 10);
print ($ted->toString(), "\n");
Any suggestions to clean up the clunky parts of the new subroutines? I
expect that some will say "this is not how you do OOP Inheritance in
Perl." Other options?
Thank you,
Peter
http://peter.michaux.ca
http://forkjavascript.org