Skip to content

Instantly share code, notes, and snippets.

@DanielFrank
Last active January 31, 2019 04:09
Show Gist options
  • Save DanielFrank/9156618 to your computer and use it in GitHub Desktop.
Save DanielFrank/9156618 to your computer and use it in GitHub Desktop.
Test to show Garbage collection issues with MooX::LValue
package T;
use Moo;
use MooX::Types::MooseLike::Base qw( :all );
use MooX::LvalueAttribute;
has objName => (
'is' => 'rw',
'isa' => Str,
);
has num => (
'is' => 'rw',
'isa' => Int,
'default' => 0,
'lvalue' => 1,
);
sub DESTROY {
my $self = shift;
my $str = $self->objName . " demolishing\n";
print STDOUT $str;
}
__PACKAGE__->meta->make_immutable;
1;
#!/usr/bin/perl
use Devel::Refcount qw(refcount);
use T;
my $t = T->new(objName=>'item 1');
print "Refcount:" . refcount($t) . "\n";
$t = T->new(objName=>'item 2');
print "Refcount:" . refcount($t) . "\n";
$t = T->new(objName=>'item 3');
print "Refcount:" . refcount($t) . "\n";
$t->num++;
print "Refcount:" . refcount($t) . "\n";
$t = T->new(objName=>'item 4');
$t = T->new(objName=>'item 5');
print "Refcount:" . refcount($t) . "\n";
$t->num(7);
print "Refcount:" . refcount($t) . "\n";
$t = T->new(objName=>'item 6', num=>6);
print "Refcount:" . refcount($t) . "\n";
$t->num(12);
print "Refcount:" . refcount($t) . "\n";
$t = T->new(objName=>'item 7', num=>6);
print "Refcount:" . refcount($t) . "\n";
$t->num += 3;
print "Refcount:" . refcount($t) . "\n";
$t = T->new(objName=>'item 8');
undef $t;
print "Just undefed item 8; should have seen a destroy\n";
$t = T->new(objName=>'item 9');
print "Refcount:" . refcount($t) . "\n";
$t->num++;
undef $t;
print "Just undefed item 9; should have seen a destroy\n";
for (my $i=1;$i<=3;$i++) {
print "loop i=$i\n";
my $t1 = T->new(objName=>"A$i");
}
for (my $j=1;$j<=3;$j++) {
print "loop j=$j\n";
my $t2 = T->new(objName=>"B$j");
$t2->num++;
}
print"exiting now\n";
exit;
@DanielFrank
Copy link
Author

Test to show garbage collection issues with MooX::LValue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment