Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: (my?) problem with re-blessed references(?) by adrianh
in thread (my?) problem with re-blessed references(?) by BrowserUk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (10)
    As of 2020-09-21 19:11 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (126 votes). Check out past polls.