Destroying an object

2005-10-18 Thread Mulander
--cut--
 sub destroy_self {

my $self = shift;
$self = undef;

 }
--cut--

The destroy method must be named DESTROY ( case-sensitive )
So you would have to do:
sub DESTROY {
my $self = shift;
undef $self;
}

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




Re: Destroying an object

2005-10-18 Thread Jeff 'japhy' Pinyan

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.  The reason this works and '$self = 
undef' doesn't is because the elements of @_ act as aliases; if you 
operate on elements of @_ DIRECTLY, you can affect changes in the things 
passed to the function.  Here's a more general example:


  sub trim {
for (@_) { s/^\s+//, s/\s+$// }
  }

This trim() function doesn't *return* trimmed string, it trims the strings 
you sent it:


  @words = (  this,   and  ,  that   );
  trim(@words);
  print [EMAIL PROTECTED];  # [this and that]

Of course, you can't send an ACTUAL string to trim(), because you can't 
modify a constant string:


  trim(  hello  );  # run-time fatal error

--
Jeff japhy Pinyan%  How can we ever be the sold short or
RPI Acacia Brother #734%  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %-- Meister Eckhart

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




Re: Destroying an object

2005-10-18 Thread Daniel Kasak

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




Destroying an object

2005-10-17 Thread Daniel Kasak

Greetings.

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

I'm trying:

sub destroy_self {

  my $self = shift;
  $self = undef;

}

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

--
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