Is Perl better to use a module than require a file?

Another question made me think of various methods to reuse code: use versus require versus do

I see many posts here where the question is about using require to load and execute code. This seems like obvious bad practice to me, but I have not found any good resources on this subject that I could point out to people.

Perlfaq8 covers the difference between use and require , but does not give any advice on preferences (from 5.10 - there is a little tip in favor of using in 5.8.8 ).

This topic seems to suffer from a lack of discussion. I have a few questions that I would like to discuss:

  1. What is the preferred method for reusing code in Perl?
    • use ModuleName;
    • require ModuleName;
    • require 'file.pl';
    • do 'file.pl';
  2. What is the difference between require ModuleName and require "file.pl" ?
  3. Is it ever a good idea to use require "file.pl" ? Why or why not?
+49
perl
Feb 01 '10 at 22:30
source share
5 answers

The standard practice is to use use most of the time, sometimes require and do rarely.

do 'file' will execute file as a Perl script. It is almost like calling eval on the contents of a file; if you do the same file several times (for example, in a loop), it will be analyzed and evaluated each time, which is unlikely to be what you want. The difference between do and eval is that do cannot see lexical variables in the scope, which makes it more secure. do sometimes useful for simple tasks, such as processing a configuration file written as Perl code.

require 'file' is similar to do 'file' , except that it will parse only one particular file once and will throw an exception if something goes wrong. (for example, the file cannot be found, it contains a syntax error, etc.). Automatic error checking makes it a good substitute for do 'file' , but it is still only suitable for the same simple purposes.

The forms do 'file' and require 'file' are hyphenations from the last days when the * .pl file extension meant "Perl Library". The modern way to reuse code in Perl is to organize it into modules. Calling something “module” instead of “library” is just semantics, but words mean completely different things in Perl culture. A library is just a collection of routines; the module provides a namespace, which makes it much more suitable for reuse.

use Module is the normal way to use code from a module. Note that Module is the name of the package as an annual word, and not a quoted string containing the file name. Perl handles the translation from package name to file name for you. The use statements are executed at compile time and throw an exception if they fail. This means that if a module depending on your code is unavailable or cannot load the error, this will be obvious right away. In addition, use automatically calls the import() method of the module, if it has one that can get you a little bit.

require Module is similar to use Module , except that it happens at runtime and does not automatically call the import() module method. Usually you want to use use for failure earlier and predictably, but sometimes require better. For example, require can be used to delay loading large modules, which are only occasionally required, or to make the module optional. (i.e. use the module if it is available, but return to something else or reduce functionality if it is not.)

Strictly speaking, the only difference between require Module and require 'file' is that the first form triggers automatic translation from the name of the package, such as Foo::Bar , to the file name, for example Foo/Bar.pm , while the last the form expects the file name to start with. By convention, however, the first form is used to load modules, and the second form is used to load libraries.

+67
Feb 02 '10 at 3:52
source share

There is a big prerequisite for using use , as it happens in the early state of BEGIN {} at compile time, and errors usually propagate to the user at a more appropriate time. It also calls the sub import {} function, which gives caller control of the import process. This is what is heavily used. You can get the same effect by invoking a specific import namespace, but for this you need to know the namespace name and file, and also encode the subroutine call ... which works a lot more. Conversely, use simply requires you to know the namespace, and then it requires a file with a suitable namespace, which makes the relationship between namespaces and files less conscious for 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 ); } BEGIN { require Module; Module->import( LIST ); } BEGIN { require Module; Module->import( LIST ); } This is much more ugly.

+6
Feb 01 '10 at 22:38
source share

The main difference is import / export. use preferred when you use a module because it allows you to specify which routines you want 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 (eg MyModule::foo). 

use also triggers the import() procedure of the module, which is often used to configure the module.

See perldoc for more details.

+5
Feb 01 '10 at 22:39
source share

Using use for the module will include the module at compile time, which increases speed but uses more memory, while using the require module will include at run time. Requiring a module without using imports uses less memory if necessary, but slows down.

+2
Mar 24 '12 at 19:19
source share

I have a question about the hash, about the hash, the hash of the array, the array of the array and the hash array, I need a full explanation with the programs

0
Feb 04 '19 at 2:59
source share