I will chime in and say I don't think deep copying is the best approach here. Use a global hash to store off-limits areas instead; you can localize individual keys of a hash as you descend, so Perl will do the backtracking for you naturally. This solution will most likely be much more efficient in terms of both time and space.