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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|