http://www.perlmonks.org?node_id=459179


in reply to "cleaning out" object data

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>