in reply to objects are references, right?

Data::Dumper is your friend for this issues:
#!/usr/bin/perl use strict; use warnings; package Node; sub new { my $class = shift; my $self = {}; $self->{parent} = undef; bless $self, $class; return $self; } sub parent { my $self = shift; if ( @_ ) { $self->{parent} = shift; } return $self; } package main; use Data::Dumper; my ( $child, $parent ) = ( new Node, new Node ); $child->parent($parent); print Dumper($parent); print Dumper($child); __END__ perl rvosa.pl $VAR1 = bless( { 'parent' => undef }, 'Node' ); $VAR1 = bless( { 'parent' => bless( { 'parent' => undef }, 'Node' ) }, 'Node' );
As you can see, both child and parent are blessed references (to hashes, in this case), and the same applies to $child->{parent}, which happens to be equal to $parent.

Note that I also activated strict and warnings, which spotted an error in your constructor:

sub new { my $class = shift; my $self = {}; my $self->{parent} = undef; # <<< you "my" $self twice! bless $self, $class; return $self; }

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

Replies are listed 'Best First'.
Re^2: objects are references, right?
by rvosa (Curate) on Jul 06, 2005 at 00:36 UTC
    Thanks! I've been using a more baroque version of the code I free-typed in here for a while now, and I've been checking with Data::Dumper already, but somehow I'm having difficulty with its coding style sometimes :)

    This clarifies things - it's the way I thought, but sometimes it's nice to have confirmation.
      I tend to use the x debugger command, which in some cases makes it more obvious:
      DB<2> x $child 0 Node=HASH(0x1c58f28) 'parent' => Node=HASH(0x1d9b770) 'parent' => undef DB<3> x $parent 0 Node=HASH(0x1d9b770) 'parent' => undef
      You can see by the hash addresses that $parent and $child{'parent'} point to the same hash. This is not immediately obvious with Data::Dumper.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of