use Test::More tests => 1; use Scalar::Util qw(weaken); { my $original = bless {},'My::Test::Class'; weaken $original; ok !defined($original), '$object garbage collected'; } #### use Test::More tests => 1; use Scalar::Util qw(weaken); { my $original = bless {},'My::Test::Class'; my $copy = $original; weaken $original; ok defined($original), '$object still accessible'; } #### { package Foo; sub new { my ($class, $name) = @_; bless {}, $class; }; my $In_global_destruction; END { $In_global_destruction++ }; my $Saved; sub DESTROY { my $self = shift; if ($In_global_destruction) { warn "really destroying $self\n"; } else { warn "not really destroying $self\n"; $Saved = $self; }; }; }; use strict; use warnings; use Test::More 'no_plan'; use Scalar::Util qw(weaken); my $copy; { my $original = Foo->new; $copy = $original; weaken $copy; destroyed_ok($original, '$original'); }; ok !defined($copy), '$original really destroyed'; __END__ # produces not really destroying Foo=HASH(0x512f0) ok 1 - $original was destroyed not ok 2 - $original really destroyed # Failed test (foo.pl at line 113) 1..2 # Looks like you failed 1 tests of 2. really destroying Foo=HASH(0x512f0)