Two problem I can see:
- $foo{shift} is the same as $foo{'shift'} - you want $foo{(shift)}. This is what's causing your overwriting problem - everything is indexed under $whatever{'shift'}.
- bless \$self, ref $class || $class makes $self reference itself - hence it will never be garbage collected. Say hello to nasty memory leaks :-)
Fixing the above gives us...
package Quote;
use strict;
use warnings;
my (%phrase, %author, %approved);
sub new {
my ($class, $args) = @_;
my $self;
$self = bless [], ref $class || $class;
if (ref $args eq "HASH") {
$self->phrase() = $args->{phrase} if exists $args->{ph
+rase};
$self->author() = $args->{author} if exists $args->{au
+thor};
$self->is_approved() = $args->{approved} if exists $args->{ap
+proved};
}
$self;
}
sub phrase : lvalue { $phrase{(shift)}; }
sub author : lvalue { $author{(shift)}; }
sub is_approved : lvalue { $approved{(shift)}; }
sub DESTROY {
my $self = shift;
delete $phrase{$self};
delete $author{$self};
delete $approved{$self};
}
package QuotePlus;
use base qw(Quote);
use strict;
use warnings;
my(%date);
sub new {
my ($class, $args) = @_;
my $self = $class->SUPER::new($args);
if (ref $args eq "HASH") {
$self->date() = $args->{date} if exists $args->{date};
}
return $self;
}
sub date : lvalue { $date{(shift)}; }
sub DESTROY {
my $self = shift;
$self->SUPER::DESTROY();
delete $date{$self};
}
Which I think does what you want.