use
- use Module VERSION
- use Module LIST
- use Module
- use VERSION
Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to
except that Module must be a bareword. The importation can be made conditional by using the if module.
In the peculiar
use VERSION
form, VERSION may be either a positive decimal fraction such as 5.006, which will be compared to$]
, or a v-string of the form v5.6.1, which will be compared to$^V
(aka $PERL_VERSION). An exception is raised if VERSION is greater than the version of the current Perl interpreter; Perl will not attempt to parse the rest of the file. Compare with require, which can do a similar check at run time. Symmetrically,no VERSION
allows you to specify that you want a version of Perl older than the specified one.Specifying VERSION as a literal of the form v5.6.1 should generally be avoided, because it leads to misleading error messages under earlier versions of Perl (that is, prior to 5.6.0) that do not support this syntax. The equivalent numeric version should be used instead.
This is often useful if you need to check the current Perl version before
use
ing library modules that won't work with older versions of Perl. (We try not to do this more than we have to.)use VERSION
also lexically enables all features available in the requested version as defined by thefeature
pragma, disabling any features not in the requested version's feature bundle. See feature. Similarly, if the specified Perl version is greater than or equal to 5.12.0, strictures are enabled lexically as withuse strict
. Any explicit use ofuse strict
orno strict
overridesuse VERSION
, even if it comes before it. Later use ofuse VERSION
will override all behavior of a previoususe VERSION
, possibly removing thestrict
andfeature
added byuse VERSION
.use VERSION
does not load the feature.pm or strict.pm files.The
BEGIN
forces therequire
andimport
to happen at compile time. Therequire
makes sure the module is loaded into memory if it hasn't been yet. Theimport
is not a builtin; it's just an ordinary static method call into theModule
package to tell the module to import the list of features back into the current package. The module can implement itsimport
method any way it likes, though most modules just choose to derive theirimport
method via inheritance from theExporter
class that is defined in theExporter
module. See Exporter. If noimport
method can be found then the call is skipped, even if there is an AUTOLOAD method.If you do not want to call the package's
import
method (for instance, to stop your namespace from being altered), explicitly supply the empty list:- use Module ();
That is exactly equivalent to
If the VERSION argument is present between Module and LIST, then the
use
will call the VERSION method in class Module with the given version as an argument. The default VERSION method, inherited from the UNIVERSAL class, croaks if the given version is larger than the value of the variable$Module::VERSION
.Again, there is a distinction between omitting LIST (
import
called with no arguments) and an explicit empty LIST()
(import
not called). Note that there is no comma after VERSION!Because this is a wide-open interface, pragmas (compiler directives) are also implemented this way. Currently implemented pragmas are:
Some of these pseudo-modules import semantics into the current block scope (like
strict
orinteger
, unlike ordinary modules, which import symbols into the current package (which are effective through the end of the file).Because
use
takes effect at compile time, it doesn't respect the ordinary flow control of the code being compiled. In particular, putting ause
inside the false branch of a conditional doesn't prevent it from being processed. If a module or pragma only needs to be loaded conditionally, this can be done using the if pragma:There's a corresponding
no
declaration that unimports meanings imported byuse
, i.e., it callsunimport Module LIST
instead ofimport
. It behaves just asimport
does with VERSION, an omitted or empty LIST, or no unimport method being found.Care should be taken when using the
no VERSION
form ofno
. It is only meant to be used to assert that the running Perl is of a earlier version than its argument and not to undo the feature-enabling side effects ofuse VERSION
.See perlmodlib for a list of standard modules and pragmas. See perlrun for the
-M
and-m
command-line options to Perl that giveuse
functionality from the command-line.