in reply to better way to cleanup a package?

As to your code, it seems to me you could do away with the %clean_types hashtable mapping as it doesn't do much, nor help with readability. <Update> Just use the value of $i and check for equality with 'scalars', etc. </Update>

But I take it that you need to clean these package variables because you're sharing them with the Safe via share or share_from, and between runs in the compartment the values from the preceding run need to be wiped away. Is this principally to conserve memory/force garbage collection, or to prevent data from being improperly preserved between uses of the compartment? If the former, then I don't believe there's much you can do, as perl is reluctant to release memory back to the system. If the latter, then rather than this convoluted approach, why not simply maintain a list of shared variables, iterating over them, undef'ing each?