Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
go ahead... be a heretic

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

Comment on Re: when is destroy function called

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 surveying the Monastery: (11)
As of 2014-04-18 00:25 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (460 votes), past polls