<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>References on Notes</title>
    <link>https://www.simplicidade.org/tags/references/index.xml</link>
    <description>Recent content in References on Notes</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <managingEditor>melo@simplicidade.org (Pedro Melo)</managingEditor>
    <webMaster>melo@simplicidade.org (Pedro Melo)</webMaster>
    <copyright>(c) 2016 Pedro Melo.</copyright>
    <atom:link href="/tags/references/index.xml" rel="self" type="application/rss+xml" />
    <atom:link rel="hub" href="https://pubsuhhubbub.superfeedr.com/"/>
    
    <item>
      <title>Weaken references in Perl</title>
      <link>https://www.simplicidade.org/notes/2005/07/02/weaken-references-in-perl/</link>
      <pubDate>Sat, 02 Jul 2005 17:43:07 +0000</pubDate>
      <author>melo@simplicidade.org (Pedro Melo)</author>
      <guid>https://www.simplicidade.org/notes/2005/07/02/weaken-references-in-perl/</guid>
      <description>&lt;p&gt;In case you need circular structures in Perl, you should known about &lt;a href=&#34;http://search.cpan.org/~gbarr/Scalar-List-Utils-1.17/lib/Scalar/Util.pm&#34;&gt;Scalar::Util&lt;/a&gt; and it&amp;rsquo;s &lt;code&gt;weaken&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;See this example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package T;

sub DESTROY {
  my $self = shift;
  print STDERR &amp;quot;Bye bye: $self-&amp;gt;{name}\n&amp;quot;;
}

package main;
use Scalar::Util qw( weaken );

{
  my $a = bless { name =&amp;gt; &#39;a&#39; }, &#39;T&#39;;
  my $b = bless { name =&amp;gt; &#39;b&#39; }, &#39;T&#39;;

  $a-&amp;gt;{b} = $b;
  $b-&amp;gt;{a} = $a;
}
print &amp;quot;Should see a destroy &#39;a&#39; and &#39;b&#39;, but you wont...\n&amp;quot;;

{
  my $c = bless { name =&amp;gt; &#39;c&#39; }, &#39;T&#39;;
  my $d = bless { name =&amp;gt; &#39;d&#39; }, &#39;T&#39;;

  $c-&amp;gt;{d} = weaken($d);
  $d-&amp;gt;{c} = weaken($c);
}
print &amp;quot;Should see a destroy &#39;c&#39; and &#39;d&#39;!\n&amp;quot;;
print &amp;quot;Now you&#39;ll see a destroy &#39;a&#39; and &#39;b&#39;\n&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The output is this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Should see a destroy &#39;a&#39; and &#39;b&#39;, but you wont...
Bye bye: d
Bye bye: c
Should see a destroy &#39;c&#39; and &#39;d&#39;!
Now you&#39;ll see a destroy &#39;a&#39; and &#39;b&#39;
Bye bye: b
Bye bye: a
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The problem is that in the first block, although the &lt;code&gt;$a&lt;/code&gt; and &lt;code&gt;$b&lt;/code&gt; are no longer in scope, each one holds a reference to the other, and that prevents them both from being destroyed.&lt;/p&gt;

&lt;p&gt;In the second block, each one of &lt;code&gt;$c&lt;/code&gt; and &lt;code&gt;$d&lt;/code&gt; takes a weak reference to each other. Weak references don&amp;rsquo;t increment the reference count internal to all Perl variables, so at the end of the block, they are correctly destroyed.&lt;/p&gt;

&lt;p&gt;You&amp;rsquo;ve warned.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>