Modules

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 BEGIN block runs as soon as the parser reaches the no statement, before any later code in the file is compiled. This is why no 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.

  • require still happens. no MODULE loads MODULE if it has not been loaded yet, then calls unimport on it. You do not need a matching use earlier in the file.

  • unimport is mandatory. If the package has no unimport method, the call dies at compile time with Can't locate object method "unimport" via package "MODULE". Most non-pragma modules do not define unimportno is 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 VERSION is a compile error. use VERSION exists as a compile-time version check against the running interpreter; there is no sensible inverse, so no 5.36; dies with No such class 5.36. The version check only goes one way.

  • no MODULE VERSION does parse. It requires MODULE to be at least VERSION (same check as use), then calls MODULE->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 want no to be a silent no-op, define an empty sub unimport {}.

  • Bareword MODULE only. Like use, 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 which unimport to call.

  • Does not undo use. no Foo calls Foo->unimport, which by convention reverses what Foo->import did to the current lexical scope — but that is a convention, not a language guarantee. A sloppy unimport can leave state behind, and side effects from require Foo (BEGIN blocks, @INC hooks, 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 }, eval boundary, or end of file. Code in a different file that requires or uses this one is unaffected.

  • import/unimport are just method calls. MODULE->unimport(LIST) runs exactly like any other class method — it sees MODULE as the first argument and LIST after. The method can inspect the caller via caller to find out which package to modify.

Differences from upstream#

Fully compatible with upstream Perl 5.42.

See also#

  • use — the positive form; no is literally use with unimport in place of import

  • require — the runtime load step that no performs before calling unimport

  • package — declares the current package, which is the scope a pragma’s unimport typically modifies

  • import — the conventional counterpart method; no calls unimport, use calls import

  • BEGIN — the compile-time phase in which no runs

  • caller — how an unimport method identifies the package it should affect