Home > Archive > PERL Beginners > August 2007 > I feel like destroying something!
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
I feel like destroying something!
|
|
| Inventor 2007-08-05, 7:00 pm |
| I think I need to use a destructor in my program. It works just fine,
producing repeatable results, but now I put in a parameter sweep loop
that runs the whole complicated thing several times and I get
unexpected noisy junk instead of smoothly varying hills and valleys
and slopes. To be certain of the problem, I varied a parameter that
should output a linear result and it was quite noisy. So I thought I
would erase the object the same way I initialized it in the
constructer and then make a new one like this:
# destroy old games database and create new ones
if ($sweep > 0) {
$games = []; # delete entire
Games_Database object
$games = Games_Database->new(); # make a new
Games_Database object
}
I also tried writing a DESTROY() method and calling it, to no avail.
I read perltoot and the article "Better Living Through
Destruction" (IIRC) on perl.com and didn't really understand the parts
on destructors. What foolish beginner mistake am I making, and how do
I accomplish my act of destruction? Thanks a bunch.
| |
| Randal L. Schwartz 2007-08-05, 7:00 pm |
| >>>>> "Inventor" == Inventor <Inventor-66@comcast.net> writes:
Inventor> I think I need to use a destructor in my program.
You need a destructor when your object holds things that need more care to
delete than simply letting the data structure go away. For example: the
object changes need to be saved to disk, or some temporary files are related
to the object.
If that's not the case, you don't need a DESTROY.
And you never *call* the DESTROY. Perl calls it when the last reference
has been lost.
Perhaps your program design suffers by having too many globals, and not enough
scope reduction. Do you have any package variables, or a large number of "my"
variables that live outside all subroutines?
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
| |
| Mr. Shawn H. Corey 2007-08-05, 7:00 pm |
| Randal L. Schwartz wrote:
> You need a destructor when your object holds things that need more care to
> delete than simply letting the data structure go away. For example: the
> object changes need to be saved to disk, or some temporary files are related
> to the object.
Or you created a cyclic reference and need to break it. (If you don't know what this is, chances are good you never created one, so don't worry about it.)
>
> If that's not the case, you don't need a DESTROY.
>
> And you never *call* the DESTROY. Perl calls it when the last reference
> has been lost.
>
> Perhaps your program design suffers by having too many globals, and not enough
> scope reduction. Do you have any package variables, or a large number of "my"
> variables that live outside all subroutines?
>
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
| |
| Inventor 2007-08-05, 7:00 pm |
| On Aug 5, 3:00 pm, shawnhco...@magma.ca (Mr. Shawn H. Corey) wrote:
> Randal L. Schwartz wrote:
>
> Or you created a cyclic reference and need to break it. (If you don't know what this is, chances are good you never created one, so don't worry about it.)
>
>
>
>
>
>
> --
> Just my 0.00000002 million dollars worth,
> Shawn
>
> "For the things we have to learn before we can do them, we learn by doing them."
> Aristotle
I fixed it! I see now that I didn't need a destructor, just a cleanup
routine. There were variables holding information like team ratings,
home team advantages, and things like that which I needed to clear
with a method i called restart(). The program was doing its
calculations starting with the ending state information from the
previous run. There was one "my" variable that needed to be cleared
also, so thanks for the hint. No cyclic references (happen to have
just read a little about those). Thanks again for being so helpful, I
would be stuck on things for w s without all your answers to my
questions.
|
|
|
|
|