#!/usr/bin/perl
use strict;
use warnings;
{
package DemoA;
sub new
{
my $class=shift;
my $self=bless {},$class;
return $self;
}
sub hello
{
my $self=shift;
print $self->message(),"\n";
}
sub message
{
my $self=shift;
return 'Hello World';
}
};
{
package DemoB;
use parent -norequire => 'DemoA';
sub message
{
my $self=shift;
return 'Shalom';
}
};
my $obja=DemoA->new();
$obja->hello(); # writes "Hello World"
my $objb=DemoB->new();
$objb->hello(); # writes "Shalom"
It is possible to use a modified constructor, using the SUPER notation:
sub new
{
my $class=shift;
my $self=$class->SUPER::new(@_);
$self->{'x'}='y';
return $self;
}
Still, no reblessing needed.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
|