demonstrate self saving during DESTROY
Matt S Trout [Mon, 15 Jun 2009 19:47:22 +0000 (20:47 +0100)]
notes/save-my-life-safely.pl [new file with mode: 0644]
notes/save-my-life.pl [new file with mode: 0644]

diff --git a/notes/save-my-life-safely.pl b/notes/save-my-life-safely.pl
new file mode 100644 (file)
index 0000000..c164daf
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Devel::GlobalDestruction ();
+
+my %objs;
+
+BEGIN {
+  package Foo;
+
+  sub DESTROY {
+    warn "DESTROY\n";
+    return if Devel::GlobalDestruction::in_global_destruction();
+    $objs{$_[0]} = $_[0];
+  }
+}
+
+{
+  bless({}, 'Foo');
+}
+
+warn join(', ', %objs);
diff --git a/notes/save-my-life.pl b/notes/save-my-life.pl
new file mode 100644 (file)
index 0000000..2e1e748
--- /dev/null
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+my %objs;
+
+BEGIN {
+  package Foo;
+
+  sub DESTROY { warn "DESTROY\n"; $objs{$_[0]} = $_[0]; }
+}
+
+{
+  bless({}, 'Foo');
+}
+
+warn join(', ', %objs);