Unloading Modules
The opposite of use is no. Instead of calling import, it calls unimport. That method can do whatever it likes. The syntax is the
same:
noMODULE; noMODULELIST; noMODULEVERSION; noMODULEVERSIONLIST;
You may only want some symbols available for a short time. For
instance, the Moose module, an object system built on top of Perl’s built-in
features, imports many convenience methods. The has method declares attributes, but once you are
done with those names, they don’t need to stick around. At the end of the
section that needs them, you can unimport them with no:
package Person;
use Moose;
has "first_name" => (is => "rw", isa => "Str");
has "last_name" => (is => "rw", isa => "Str");
sub full_name {
my $self = shift;
$self–>first_name . " " . $self–>last_name
}
no Moose; # keywords are removed from the Person packageTo temporarily turn off a strict feature, unimport the feature that’s in the way. Use the smallest scope possible so you don’t miss other problems:
my $value = do {
no strict "refs";
${ "${class}::name}" }; # symbolic reference
};Similarly, you might need to temporarily turn off a type of warning, so you unimport that type of warning:
use warnings;
{
no warnings 'redefine';
local *badger = sub { ... };
...;
}Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access