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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1020985]
help
Chatterbox?
[1nickt]: marto would like to hear that. Of course he speaks very deliberately and dramatically, allowing pauses between words, so if anybody could pull it off it would be him, or someone impersonating him ...
[Eily]: Discipulus that's a chess joke BTW right? Because bishops don't walk straight :D
[choroba]: stop making chessy jokes about bishops!
[choroba]: I mean cheesy
[LanX]: lol
[ambrus]: no, the chess bishop itself is a joke on real bi-shops
[ambrus]: or at least on the stereotype of bi-shops
[choroba]: we call chess bishops "archers"
[Eily]: choroba sorry, I had to get that off my chess
[ambrus]: chess certainly hasn't started those stereotypes, like how kings aren't that powerful but has convinced their whole country to work for them, etc. it's just poured them to a nice clean form that would easily get propagated.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2017-09-26 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (294 votes). Check out past polls.

    Notices?