Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: when is destroy function called

by ggoebel (Sexton)
on Feb 28, 2013 at 04:20 UTC ( #1020985=note: print w/replies, xml ) Need Help??

in reply to when is destroy function called

Perl is reference counted. So unless you're writing XS or creating circular references, you don't need to pay much attention. However, for those times when you do... the destructor is called when the last reference is undefined.

the following...

package Dalek;
sub new { return bless {}; }
sub exterminate { return q{'exterminate'}; }
sub obey { return q{'obey'}; }
sub DESTROY { print "destroy\n"; }

package main;
my $obj = Dalek->new();
print "obj is a: ", ref($obj), "\n";
print "Where's my ", eval('$obj->obey()'), undef($obj), eval('$obj->exterminate()'), " spanner?\n";
print $@ if $@;


> perl
obj is a: Dalek
Where's my 'obey' spanner?
Can't call method "exterminate" on an undefined value at (eval 2) line 1.

Note that the object is destroyed before the print statement in the destructor is flushed to STDOUT

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1020985]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2016-10-27 08:06 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (355 votes). Check out past polls.