assuming I have a classic object, ie a blessed anonymous hash, which contains a chunk of data, and I want to clean out the data without actually deleting the object itself, what would be the best way to go about it? So far all I've been able to come up with would to be a
kind of solution but there has to be a better approach to this. Is there one? I searched the docs extensively but was unable to come up with a better idea.my $self = shift; foreach my $key (keys(%{$self})) { delete $self->{$key}; }
edit: small example to clarify what I mean
edit 2: put the example between readmore tags.
Now our Celebrity doesn't care who the actual guard is, he/she just wants to sleep() safely. So the object itself needs to take care of cleaning out the data without having to bother the calling script. Hope that clears it up a bit :-)package Celebrity; use strict; use warnings; my $security = Guard->new; while(1) { sleep(3600); $security->check; } package Guard; use strict; use warnings; sub new { my $class = shift; my $self = {}; bless($self, $class); $self->{'Hours_on_the_job'} = 0; return $self; } sub check { #crawl through the bushes, meet people, shoot them #collect all sorts of data $self->{'Data'} = "blahdiblah"; $self->{'Moredata'} = "dumdidum"; $self->{'Yetmoredata'} = "whatever dude"; $self->{'Hours_on_the_job'} += 1; if($self->{'Hours_on_the_job'} == 6) { #long live the union! #file an extensive report, go home #new guard takes over * cleaning out the object would happen here * $self->{'Hours_on_the_job') = 0; } }
Remember rule one...
Edit by castaway - added missing closing small tag
In reply to "cleaning out" object data by Forsaken
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |