in reply to What is the correct way to clear package?

use Symbol qw(delete_package); delete_package('Foo::Bar');
module::unload

Replies are listed 'Best First'.
Re^2: What is the correct way to clear package?
by vokbuz (Novice) on May 06, 2011 at 08:00 UTC

    Thanks! It works for me.

    Update:

    No, it doesn't.

    It clears nested packages. It's not required behavior.

    #!/usr/bin/env perl package Test1; package Test1::Nested; sub a { print "Test1::Nested::a\n"; } package main; use Symbol; # ok. eval "Test1::Nested::a()"; die $@ if $@; Symbol::delete_package( 'Test1' ); # fails. eval "Test1::Nested::a()"; die $@ if $@;
      > It's not required behavior.

      Well, it's not our fault if you don't clarify your requirements... It was a struggle to find out what you exactly want.

      > It clears nested packages.

      2 Thoughts:

      1. Symbol::delete_package is plain perlcode, you could just adapt it to your needs.
      2. If you want to rely on Symbol been kept adapted to any future changes, why not just safe away the globs of nested packages, apply delete_package() and then reassign the globs? You already have the necessary code in the OP.

      Cheers Rolf