Perl B::Hooks::EndOfScope module allows you to execute code when perl finished compiling the surrounding scope.