In case anyone else is interested, my idea about dumping the contents of a container when you opened it worked, but it didn't make sense from a user's point of view. If you open a container while you're carrying it, then it makes sense for the contents to fall out. But if you open a container when you're not carrying it, it doesn't make sense for the contents to fall out. I've now handled it differently. When you open a container, it tells you the contents. You can then take the contents using :pocket "object". Once taken, you can't return it to the container or put anything else in the container. You can examine the object before or after taking it out of the container. You can open or close the container at will. If you open it when it's empty, it tells you it's empty. When you examine the container, it tells you it's closed OR it contains an object OR it's empty. Whew!
As I have three different containers, none of which are quite as simple as what's described above, it was quite a lot of hard work getting all the tests right for all the different conditions. Anyway, it's done now, so I'll know how to do it in future.