Jeff 'japhy' Pinyan wrote:

On Oct 18, Daniel Kasak said:

I've got an OO object that I want to destroy from inside the object itself.

sub destroy_self {
 my $self = shift;
 $self = undef;
}

But this doesn't work. What is the correct way of doing it?


That's because you're not getting rid of the CONTENTS of the object, you're merely undefining a variable that holds a reference to the contents.

Ordinarily, you can just wait for the object to go out of scope and it will be destroyed by Perl. However, if you want to do it when you want to, here's a sneaky way:

  sub kill_object {
    undef $_[0];
  }

Call it as

  $obj->kill_object;

and the object will be destroyed.

While I understand the logic behind this, it unfortunately doesn't work; the object persists! Time for some example code.
The code in my OO module ( forms::countries ) that should destroy itself:

---

sub destroy_self {
  undef $_[0];
}

---

The code in my app that should take advantage of the existance of the above object:

---

sub on_Countries_clicked {
if ( $countries ) {
       $countries->{form}->get_widget("Countries")->present;
   } else {
       $countries = forms::countries->new( $form_options );
   }
}

---

I've stepped the code through a debugger, and the destroy_self() sub is being run at the right time ... but the object still persists, and when I click on a button and on_Countries_clicked is called, the 1st option:

$countries->{form}->get_widget("Countries")->present;

is executed instead of the 2nd:

$countries = forms::countries->new( $form_options );

I am working around this currently by simply destroying the {form} bit, eg:

---

sub destroy_self {
  my $self = shift;
  $self->{form} = undef;
}

---

and then testing for the existance of $countries->{form} instead of just $countries. But this isn't ideal.

What am I doing wrong?

--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: [EMAIL PROTECTED]
website: http://www.nusconsulting.com.au

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to