package Original;
sub new {
my $class = shift;
my $other = shift; # hash ref, optional
my $self = {
name => 'the original',
rating => 'supreme commander',
boots => 'laced to the knee',
};
if ($other) {
foreach (keys %$other) {
$self{$_} = $other->{$_};
}
}
bless($self, $class);
return $self;
}
sub copy {
my $self = shift;
return $self->new($self);
}
####
sub new {
my $class = shift;
my $self = _init(@_); # private method
bless($self, $class);
return $self;
}
sub _init {
my %data = (
name => 'supreme commander two',
rank => 'even better',
toothbrush => 'purple with sparklies',
);
if (@_) {
foreach (keys %{ $_[0] }) {
$data{$_} = ${$_[0]}->{$_}; # yuck, probably wrong
}
}
return \%data; # reference!
}
sub copy {
my $self = shift;
return $self->new($self); # pass this as a hash ref
}
####
sub copy {
my $self = shift;
my $new = __SUPER__->init($self->new($self));
return $new;
}