Tuesday, May 21, 2013

Weak refs

Look at this Perl code:
use Scalar::Util qw( weaken );

my ( $weak_a, $weak_b, $weak_c );

{
        my $a = {
                name => 'a',
        };

        my $b = {
                a    => $a,
                name => 'b',
        };

        my $c = {
                a    => $a,
                b    => $b,
                name => 'c',
        };

        $a->{b} = $b;

        $weak_a = $a;
        $weak_b = $b;
        $weak_c = $c;

        weaken( $weak_a );
        weaken( $weak_b );
        weaken( $weak_c );
}

print $weak_a->{name};
print $weak_b->{name};
print $weak_c->{name};

What and why will be printed?

No comments:

Post a Comment