in reply to "cleaning out" object data

I would use your own solution, but put this in an '_init', so I could set some initial values (if needed). This could then be called from your 'new' or when you want to 'reset' the object:
sub new { my $class = shift; my $self = {}; bless($self, $class); $self->_init; return $self; } sub _init { my $self = shift; foreach my $key (keys(%{$self})) { delete $self->{$key}; } $self->{hours_on_the_job} = 0; $self->{people_shot} = 0; etc... }
Then you can just call $self->_init; whenever you wish to reset the data.

.:| If it can't be fixed .. Don't break it |:.

Replies are listed 'Best First'.
Re^2: "cleaning out" object data
by jhourcle (Prior) on May 20, 2005 at 13:35 UTC

    I like your thinking, but I'd suggest not calling it init. 'reset' or something similar might be a better idea.

    It's common practice, when you're dealing with inheritance, to make sure you call all of the init-type functions in the inherited object. (the easiest way is to just call $self->NEXT::_init(), if you're using NEXT

    Of course, if you were to use an _init() function as you've described, you'll end up trashing whatever other people have set up, as well.

    Ideally, you would just clear out the keys that you were making use of, so you don't end up treading on someone else's toes... but I'd still move the destructive part of the code out to a seperate function that is not called by the _init() or new() routines.

      Thanks for pointing out $self->NEXT::... to me... I have not been doing too much OO, but I have encountered problems for which the NEXT module is a good option! I will be busy making some code changes now :)

      .:| If it can't be fixed .. Don't break it |:.