Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: (my?) problem with re-blessed references(?)

by adrianh (Chancellor)
on Dec 13, 2002 at 09:32 UTC ( #219539=note: print w/ replies, xml ) Need Help??


in reply to (my?) problem with re-blessed references(?)

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.


Comment on Re: (my?) problem with re-blessed references(?)
Select or Download Code
Re: Re: (my?) problem with re-blessed references(?)
by BrowserUk (Pope) on Dec 13, 2002 at 11:40 UTC

    Thanks adrianh++.

    I should've spotted the shift thing myself. It was 4:00 am though. (Excuses, excuses &^) ... that's a crossed-eyed smiley).

    The $self = bless \$self, $class; bit was just an idea that I never got around to looking at the effects of as the other stuff was going wrong. The strange thing is that using that rather than [], meant that somethings appeared to be working, ie. the hashes in the base class were being populated, though 2 of the 3 hashes were getting two entries. 1 under the subclass reference and one under the base class reference? The 3rd hash (%approved) was only getting a single entry.

    I know that all sounds implausible given they are all initialised in the same way, but that is what I was seeing.

    I've made a backup of that version and will get back to investingating what was going on once I've finished experimenting with this.


    Examine what is said, not who speaks.

Re: Re: (my?) problem with re-blessed references(?)
by demerphq (Chancellor) on Dec 13, 2002 at 13:30 UTC
    $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'}.

    Just wanted to say that the standard way of doing this is not to parethesize the shift, but to put a + in front of it. This is perls way of ensuring that whatever follows the plus is construed as code and not something else. Note that this is NOT the same as 0+shift, which coerces numeric context.

    This is IME particularly useful with print and with hash keys.

    print +($.>10) ? "Skipped." : "Ok"; $foo{+shift}=10;

    --- demerphq
    my friends call me, usually because I'm late....

      Just wanted to say that the standard way of doing this...

      Perl has standards?

      :-) :-)

      It's also nice for constructing hashes with map: my %foo = map +( $_ => bar($_) ), @baz;
      which otherwise won't parse correctly.

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://219539]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-11-29 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (203 votes), past polls