Esteemed Monks,

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

my $self = shift; foreach my $key (keys(%{$self})) { delete $self->{$key}; }
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.

edit: small example to clarify what I mean

edit 2: put the example between readmore tags.

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; } }
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 :-)

Remember rule one...

Edit by castaway - added missing closing small tag


In reply to "cleaning out" object data by Forsaken

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.