I often set up objects with an init() or reset() methods. I use the new() simply to create the object, and then let it call init() where everything gets populated, assigned, and so on. In that case, I would just call init() again to get the object back to its pristine state. You might have to include some code to make sure it only has the stuff you want (and nothing more).

In your example, I would think you would want a new Guard object for each new guard on duty. You can still store the current Guard in the same variable in the main script, but once each Guard instance has its own object you can do things with the Guard coming off shift and the Guard going on shift. You can freeze the off-duty Guard object and thaw it out when it's time for him to work again. You could even keep an array of Guards around and rotate through them.

Instead of exposing a Guard object to the main script though, what you really want is a Security object. Like you said, the Celebrity does not care about the Guard, only the Security. The Security object can handle shift changes and so on. As you rotate through the Guards, you can ask the Security object things like "Who's on duty?", but also, "Who was last on duty?" and "What did he remember"? You can't do that sort of thing if you wipe out the Guard's memory at every shift change.

--
brian d foy <brian@stonehenge.com>

In reply to Re: "cleaning out" object data by brian_d_foy
in thread "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.