but that still make a circular reference because of $self
You could stick close to your original solution with a method call if you pass a weakened copy of $self to the subroutine reference:
That way, this_is_from_A has full access to the $self object (which isn't needed in your example so far).use strict; use warnings; use Devel::Cycle; package A; use Scalar::Util qw/weaken/; sub new { my ( $class, $href ) = @_; my $self = { dbi => $$href{dbi} }; my $weak_self = $self; weaken $weak_self; $self->{helper} = Helper->new( helper_function => sub { $weak_self->this_is_from +_A(@_) } ); bless $self, $class; } # ....the rest remains unchanged.
In reply to Re^5: Avoiding circular references
by haj
in thread Avoiding circular references
by frazap
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |