no#
The compile-time inverse of use — call a module’s unimport
method to turn off what use turned on.
no MODULE LIST is handled by the parser exactly like
use MODULE LIST, except it calls MODULE->unimport(LIST) instead of
MODULE->import(LIST). Both run at compile time, both are lexically
scoped, and both require the module to already be loaded. The common
case is switching a pragma off inside a block — no strict 'refs',
no warnings 'uninitialized', no feature 'switch' — without
touching the enclosing file.
Synopsis#
no MODULE LIST
no MODULE
no MODULE VERSION LIST
no MODULE VERSION
What you get back#
no is a declaration, not an expression. It has no useful return
value and cannot appear where a value is expected. The effect is a
side effect on the compile-time state of the enclosing lexical scope.
How it expands#
The parser rewrites
no MODULE LIST;
into the rough equivalent of
BEGIN {
require MODULE;
MODULE->unimport(LIST);
}
— the same shape as use, with unimport swapped in for
import. Consequences that follow from this:
Compile time. The
BEGINblock runs as soon as the parser reaches thenostatement, before any later code in the file is compiled. This is whyno strict 'refs'takes effect on the statements that follow it in the same file, not at runtime.Lexical scope. Pragmas that honour lexical scope (
strict,warnings,feature,integer,utf8, and the other core pragmas) see their “off” effect confined to the innermost enclosing block,eval, or file. Leaving the block restores the previous state.requirestill happens.no MODULEloadsMODULEif it has not been loaded yet, then callsunimporton it. You do not need a matchinguseearlier in the file.unimportis mandatory. If the package has nounimportmethod, the call dies at compile time withCan't locate object method "unimport" via package "MODULE". Most non-pragma modules do not defineunimport—nois really only useful for modules that advertise it.
Examples#
Turn off a single warning category inside a block:
use warnings;
for my $row (@rows) {
no warnings 'uninitialized';
print join(",", @$row), "\n"; # empty fields render as ""
}
# 'uninitialized' warnings are back on here
Relax strict 'refs' for one statement that needs symbolic
dereferencing:
use strict;
sub install {
my ($name, $code) = @_;
no strict 'refs';
*{"main::$name"} = $code; # symbolic typeglob assignment
}
Disable an experimental feature warning while using the feature:
use feature 'signatures';
no warnings 'experimental::signatures';
sub greet ($who) { "hello, $who\n" }
Roll back a default feature bundle to plain Perl semantics:
use v5.36; # enables strict, warnings, say, signatures…
no feature 'switch'; # …but not given/when
Turn off a user pragma that provides its own unimport:
use My::Pragma qw(loud);
{
no My::Pragma; # My::Pragma->unimport called here
quiet_code();
}
Edge cases#
no VERSIONis a compile error.use VERSIONexists as a compile-time version check against the running interpreter; there is no sensible inverse, sono 5.36;dies withNo such class 5.36. The version check only goes one way.no MODULE VERSIONdoes parse. It requiresMODULEto be at leastVERSION(same check asuse), then callsMODULE->unimport(LIST). The version check is about the loaded module, not the interpreter.Module without
unimport. Dies at compile time. There is no fallback to a no-op; if you are writing a module and wantnoto be a silent no-op, define an emptysub unimport {}.Bareword
MODULEonly. Likeuse, the module name must be a bareword known at compile time.no $name;is a syntax error — the parser needs the literal token to decide whichunimportto call.Does not undo
use.no FoocallsFoo->unimport, which by convention reverses whatFoo->importdid to the current lexical scope — but that is a convention, not a language guarantee. A sloppyunimportcan leave state behind, and side effects fromrequire Foo(BEGIN blocks,@INChooks, installed subs in other packages) are not rolled back.Effect ends at the enclosing block. For lexical pragmas, the “off” state stops at the next
},evalboundary, or end of file. Code in a different file thatrequires oruses this one is unaffected.import/unimportare just method calls.MODULE->unimport(LIST)runs exactly like any other class method — it seesMODULEas the first argument andLISTafter. The method can inspect the caller viacallerto find out which package to modify.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
use— the positive form;nois literallyusewithunimportin place ofimportrequire— the runtime load step thatnoperforms before callingunimportpackage— declares the current package, which is the scope a pragma’sunimporttypically modifiesimport— the conventional counterpart method;nocallsunimport,usecallsimportBEGIN— the compile-time phase in whichnorunscaller— how anunimportmethod identifies the package it should affect