in reply to objects are references, right?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: objects are references, right?
by rvosa (Curate) on Jul 06, 2005 at 00:36 UTC | |
by QM (Parson) on Jul 06, 2005 at 17:38 UTC |