In Perl, is it better to use a module than to require a file?
The main difference is around import/export. use
is preferred when you're making use of a module because it allows you to specify which routines you wish to import into your namespace :
use MyModule qw(foo bar baz); # allows foo(), bar() and baz() to be used
use MyModule qw(); # Requires explicit naming (e.g. MyModule::foo).
use
also gives runs the module's import()
procedure which is often used to set the module up.
See the perldoc for use for more detail.
Standard practice is to use use
most of the time, require
occasionally, and do
rarely.
do 'file'
will execute file
as a Perl script. It's almost like calling eval
on the contents of the file; if you do
the same file multiple times (e.g. in a loop) it will be parsed and evaluated each time which is unlikely to be what you want. The difference between do
and eval
is that do
can't see lexical variables in the enclosing scope, which makes it safer. do
is occasionally useful for simple tasks like processing a configuration file that's written in the form of Perl code.
require 'file'
is like do 'file'
except that it will only parse any particular file one time and will raise an exception if something goes wrong. (e.g. the file can't be found, it contains a syntax error, etc.) The automatic error checking makes it a good replacement for do 'file'
but it's still only suited for the same simple uses.
The do 'file'
and require 'file'
forms are carryovers from days long past when the *.pl file extension meant "Perl Library." The modern way of reusing code in Perl is to organize it into modules. Calling something a "module" instead of a "library" is just semantics, but the words mean distinctly different things in Perl culture. A library is just a collection of subroutines; a module provides a namespace, making it far more suitable for reuse.
use Module
is the normal way of using code from a module. Note that Module
is the package name as a bareword and not a quoted string containing a file name. Perl handles the translation from a package name to a file name for you. use
statements happen at compile time and throw an exception if they fail. This means that if a module your code depends on isn't available or fails to load the error will be apparent immediately. Additionally, use
automatically calls the import()
method of the module if it has one which can save you a little typing.
require Module
is like use Module
except that it happens at runtime and does not automatically call the module's import()
method. Normally you want to use use
to fail early and predictably, but sometimes require
is better. For example, require
can be used to delay the loading of large modules which are only occasionally required or to make a module optional. (i.e. use the module if it's available but fall back on something else or reduce functionality if it isn't.)
Strictly speaking, the only difference between require Module
and require 'file'
is that the first form triggers the automatic translation from a package name like Foo::Bar
to a file name like Foo/Bar.pm
while the latter form expects a filename to start with. By convention, though, the first form is used for loading modules while the second form is used for loading libraries.
There is a major preference for using use
, because it happens at an earlier state BEGIN {}
during compilation, and the errors tend to be propagated to the user at a more appropriate time. It also calls the sub import {}
function which gives the caller control over the import process. This is something that is heavily used. You can get the same effect, by calling the specific namespace's import
, but that requires you to know the name of the namespace, and the file, and to code the call to the subroutine... which is a lot more work. Conversely, use
, just requires you to know the namespace, and then it requires the file with the matching namespace -- thus making the link between namespaces and files less of an conscious to the user.
Read perldoc -f use
, and perldoc -f require
, for more information. Per perldoc -f use
:use
is the same as BEGIN { require Module; Module->import( LIST ); }
Which is just a lot more ugly.
Using use
to module will include module during compile time which increases speed but uses more memory, whereas using require
module will include during run time. Requiring a module without using import when needed uses less memory but reduces speed.