From: Matt S Trout Date: Mon, 15 Jun 2009 20:12:05 +0000 (+0100) Subject: making a weak ref strong X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dd944db57e1969dc93f5916c7f343d47ed9cba56;p=p5sagit%2FMutually-Assured-Destruction.git making a weak ref strong --- diff --git a/notes/saved-by-strength.pl b/notes/saved-by-strength.pl new file mode 100644 index 0000000..cdad638 --- /dev/null +++ b/notes/saved-by-strength.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Devel::GlobalDestruction (); +use Scalar::Util (); + +BEGIN { + package Foo; + + sub DESTROY { + return if Devel::GlobalDestruction::in_global_destruction(); + warn "DESTROY\n"; + + my $self = shift; + + if (ref $self->{other_side}) { + if ( + Scalar::Util::refaddr($self->{other_side}{reference}) + eq Scalar::Util::refaddr($self) + ) { + warn "Enlivening"; + $self->{other_side}{reference} = $self; + delete $self->{other_side}; + } + } + } +} + +my $near = {}; +my $far = $near->{reference} = bless({ other_side => $near }, 'Foo'); +warn $near->{reference}; +warn $far; +Scalar::Util::weaken($near->{reference}); +warn "Setup done\n"; +undef($far); +warn $near->{reference}; +Scalar::Util::weaken($near->{reference}); +warn $near->{reference};