Perl の循環参照と GC

時間がないので走り書き…

package Object::B;

sub new {
  my $class = shift;
  bless { a => shift }, $class;
}

sub DESTROY {
}

package Object::A;

my @B;

sub createB {
  my $self = shift;
  push(@B, Object::B->new($self));
  $self->{b_index} = $#B;
}

sub destroyB {
  my $self = shift;
  undef $B[$self->{b_index}];
}

sub b {
  my $self = shift;
  $B[$self->{b_index}];
}

sub new {
  my $class = shift;
  my $self = bless {}, $class;
  $self->createB;
  self;
}

sub DESTROY {
  my $self = shift;
  $self->destroyB;
}

package main;

{
  my $a = Object::A->new;
}