Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:44:37+00:00 2026-06-11T10:44:37+00:00

I read the Programming Perl book which is rather complicated in some places. And

  • 0

I read the “Programming Perl” book which is rather complicated in some places. And one of those places is the section “Instance Destructors” in the 12th chapter “Objects”. This section says:

  1. Objects in Perl are destroyed when there is no more references to them.
  2. There is an opportunity to capture control just before object is going to be recycled by defining DESTROY method in its class.
  3. Although destructors are rarely needed in Perl, some objects may have, for instance, filehandles or database connections, which are outside of the memory system. So it is necessary to attend them specially.
  4. Perl does not do hierarchical destruction.

Then there is a paragraph which I failed to understand:

This only applies to inherited classes; an object that is simply contained within
the current object—as, for example, one value in a larger hash—will be freed
and destroyed automatically. This is one reason why containership via mere ag-
gregation (sometimes called a “has-a” relationship) is often cleaner and clearer
than inheritance (an “is-a” relationship).

I can’t understand what it means. Does it mean that an object that IS NOT simply contained within the current object, WILL NOT be freed and destroyed automatically?

I do know that a DESTROY that is called on the garbage collection is the nearest one and only one. No other overridden DESTROY methods are called when there is no refs to an instance. But, as I understand, the same behavior is expected when a ref to an instance is placed inside another object.

Would someone be so pleasant to construe and to provide a code-example ?


UPDATE:

Actually what I was looking for, was the explanation of the This only applies to inherited classes; words which turned out to be that:

If you have an instance of a class and it has a DESTROY method than that method will override DESTROY methods of a parent class(es), but that does not apply to an object, that is in a has-a relationship with the object in question. Its DESTROY won’t be overridden

Sorry for not clear question, it would better fit to the English Language and Usage. Thanks to everyone.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:44:38+00:00Added an answer on June 11, 2026 at 10:44 am

    Rule 1: Objects in Perl are destroyed when there is no more references to them.

    {
      my $object = Class->new()
    }
    # there are no more references to $object
    # (it is out of scope and can't be accessed by any means)
    # Perl is free to garbage-collect it.
    

    Rule 2: There is an opportunity to capture control just before object is going to be recycled by defining DESTROY method in its class.

    package Class;
    sub new     { return bless({}, shift) }
    sub DESTROY { print STDERR "The object is destroyed" }
    
    ####
    
    {
      my $object = Class->new();
    }
    # before the object is garbage-collected, cleanup-operations should be manually performed
    # like closing down connections, solving circular references
    # any time, the object might print it's message.
    

    Rule 4: Perl does not do hierarchical destruction.

    package AnotherClass;
    use Class;
    use parent 'Class';
    sub DESTROY { print STDERR "subclass reporting dead" }
    

    Now if AnotherClass will be instantiated, only the DESTROY method of AnotherClass will be called, not the DESTROY method of Class. This is meant with the absence of a hierarchical destruction. This is obvious, as the DESTROY method overwrites the previous entry. The original DESTROY can be called manually

    Using a parent class and a child class is a IS-A relationship: AnotherClass is a Class.

    If we have YetAnotherClass:

    package YetAnotherClass;
    use Class;
    sub new {return bless({member => Class->new()}, shift) }
    
    {
       my $object = YetAnotherClass->new();
    }
    # $object goes out of scope (zero reference count) and will be destroyed.
    # Therefore, the reference count of the "member" drops to zero
    # The member will therefore be destroyed and print it's message.
    

    This is a case of aggregation (Class is a data member of YetAnotherClass), and a HAS-A relationship.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the book Linux System Programming I have read some like this: fgetc returns
In Programming Perl (the book) I read that I can create a dictionary where
I've read that good programming practice claims that one should use a function if
Read (skimmed enough to get coding) through Erlang Programming and Programming Erlang . One
I am trying to learn perl programming and am using it to read a
It was suggested that Effective Java is a great book to read before programming
Recently,I read Java Network Programming,in section 5.5,part 5.5.2.6 ,it has the following code: public
I have developed a program which makes use of serial programming to read and
I read in Programming in Scala section 23.5 that map, flatMap and filter operations
When I read through Programming Perl , 2nd Edition, Page 51, something confuses me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.