http://www.perlmonks.org?node_id=669259


in reply to How to explicitly destroy an object so that all other references to it would become false?

1)
Many thanks!

2)
>do you have an example?

package XXX; my %already_created_objects; sub new { my ($class, $X) = @_; if (!$already_created_objects{$X}) { $already_created_objects{$X} = bless {somekey => $X} $class; } $already_created_objects{$X} }
somewhere inside the script
... $obj = new XXX($some_var_which_can_repeat); ... if ($some_conditions) { some_superundef $obj } ...

Scalar::Util::weaken can do what I would like, but, as I've understood, it is required to mark as "weak" every reference to the object? Indirection layer seems more handy, may be...

  • Comment on Re: How to explicitly destroy an object so that all other references to it would become false?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to explicitly destroy an object so that all other references to it would become false? (just 1)
by tye (Sage) on Feb 21, 2008 at 16:05 UTC
    but, as I've understood, it is required to mark as "weak" every reference to the object?

    No, the typical use would be to have only %already_created_objects contain weak references. Then normal object life-time control would take place and when an object is no longer referenced other than in %already_created_objects, that object would be destroyed and the value in %already_created_objects would transform into undef.

    If there is some reason you want to destroy the object while there are still live references to it besides %already_created_objects, then you should give more information to justify that unusual requirement.

    - tye