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.#!/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' );
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
In reply to Re: objects are references, right?
by polettix
in thread objects are references, right?
by rvosa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |