Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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 $@;

gives...

> perl destroy.pl
obj is a: Dalek
Where's my 'obey' spanner?
destroy
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2014-09-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (145 votes), past polls