Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Why is the base class's destructor not called?

by lifang11 (Initiate)
on Jan 22, 2014 at 11:27 UTC ( #1071593=note: print w/ replies, xml ) Need Help??


in reply to Re: Why is the base class's destructor not called?
in thread Why is the base class's destructor not called?

If that is true, the son class is destroyed later than father class? The sequence of destruction is diffrent from C++?


Comment on Re^2: Why is the base class's destructor not called?
Re^3: Why is the base class's destructor not called?
by choroba (Abbot) on Jan 22, 2014 at 11:31 UTC
    The destructor is called when an object is destroyed, not the class.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Why is the base class's destructor not called?
by Athanasius (Monsignor) on Jan 22, 2014 at 13:17 UTC

    In C++, if class Son inherits from class Father, then class Son inherits class Father’s methods and data. So it makes sense that when an object of class Son goes out of scope, the Son destructor is called to clean up the data which Son did not inherit, and then the Father destructor is called to clean up the data Son inherited from Father.

    But Perl* is different:

    Perl only provides method inheritance as a built-in feature. Attribute inheritance is left up [to] the class to implement.
    perlobj

    So, from the point of view of the data, there needn’t be any of the Father class in the Son class. So there may be nothing of the Father class to destroy, and no purpose in calling Father’s destructor.

    *Meaning the core Perl language, not the various OO frameworks mentioned by tobyink, below.

    Hope that helps,

    Update (23 Jan 2014): Added [to] to clarify the quote, and reduced the font size of the footnote.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^3: Why is the base class's destructor not called?
by BrowserUk (Pope) on Jan 22, 2014 at 18:43 UTC

    Objects (class instances) are represented by blessed references.

    When blessed reference goes out of scope (or otherwise becomes eligible for destruction), the cleanup code (only) calls a DESTROY method within the class that that reference is blessed into.

    Since your instance is of class B; only B's destructor is called automatically. It is up to the writer of class B to call any base-class or delegate destructors that need to be called from B's DESTROY method.

    As for the 'apparent order' or destructor calls, it all depends upon how you choose to trace them. If you output your trace messages on entrance to the destructors:

    package A; ... sub DESTROY { warn 'A::DESTROY' ... return; } package B; ... sub DESTROY { warn 'B::DESTROY'; my $self = shift; $self->SUPER::DESTROY; return; }

    Then class Bs destructor appears to be called before class A's. But do it this way:

    package A; ... sub DESTROY { ... warn 'A::DESTROY' return; } package B; ... sub DESTROY { my $self = shift; $self->SUPER::DESTROY; warn 'B::DESTROY'; return; }

    And it will appear to happen the other way around. So, appearances aren't everything.

    The main difference between Perl and C++ in this regard is that in Perl, the destructors are called recursively, and the programmer gets to decide which order things happen in (or if at all).

    In C++; destructors are called iteratively in a fixed (youngest to oldest) order.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Why is the base class's destructor not called?
by ikegami (Pope) on Jan 24, 2014 at 14:55 UTC
    No, it happens in whatever order you want it to. The order is fixed in C++, completely but up to you in Perl. Like you found out, you can even completely skip calling a destructor.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-12-29 07:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls