In Perl, is it better to use a module than to require a file?
perl
Solution
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.
Problem
Another question got me thinking about different methods of code reuse: `use` vs. `require` vs. `do` I see a lot of posts here where the question centers around the use of `require` to load and execute code. This seems to me to be an obvious bad practice, but I haven't found any good resources on the issue that I can point people to. perlfaq8 covers the difference between `use` and `require`, but it doesn't offer any advice in regard to preference (as of 5.10--in 5.8.8 there is a quick bit of advice in favor of use). This topic seems to suffer from a lack of discussion. I have a few questions I'd like to see discussed: - What is the preferred method of code reuse in Perl? - `use ModuleName;` - `require ModuleName;` - `require 'file.pl';` - `do 'file.pl';` - What is the difference between `require ModuleName` and `require "file.pl"`? - Is it ever a good idea to use `require "file.pl"`? Why or why not?