The difference between use and requirement (I listed the differences, you need to know what else is there)

I read the explanation even with perldoc and StackOverflow . But there is a bit of confusion.

  • use usually loads the module at compile time, while require works at runtime
  • use calls the built-in import function, whereas require must call the import module separately, for example

     BEGIN { require ModuleName; ModuleName->import; } 
  • require used if we sometimes want to load larger modules.

  • use throws an exception in earlier states whereas require does when I encounter a problem
  • With use we can selectively load procedures not all but a few, like

     use Module qw(foo bar) # it will load foo and bar only 

is it possible in require ?

Beisdes, are there other differences between use and require ?

There are a lot of discussions on google, but I only understood these points above.
Please help me in other points.

+6
source share
3 answers

I think the code you wrote at the second point itself explains the difference between the two ...

In practice, “use” fulfills the “requirement” of the module, and after that it automatically imports the module with the “demand”, instead the module is only required for presence, but you have the freedom to import it when you need it ...

Considering the above, it is obvious that the question at point 5 does not make sense, since “require” does not import anything, there is no need to specify the part of the module to load, you can selectively load the part that you need when you perform the import operation ...

Also, keep in mind that although the “use” action is at compile time (Perl compilation phase) , the “require” action is at run time , for this reason with the “require” requirement, you can import a package only when and / or when it is really necessary.

+4
source

This is similar to the difference between my , our and local . The differences are important, but you should use my 99% of the time.

Perl is a pretty old and cruel language. Over the years, it has evolved from a combination of the awk / shell / kitchen sink language into a stronger typed and more powerful language.

Back to Perl 3. A few days before the concept of modules and packages solidified, there was no concept of modules having their own namespace for functions and variables. Everything was available everywhere. There is nothing to import. The use keyword does not exist. You always used require .

By the time Perl 5 was released, modules had their own repository for variable names and routines. Thus, I could use $total in my program, and my Foo::Bar module could also use $total , because my $total was really $main::total , and their $total was really $Foo::Bar::total .

Export is a way to make variables and routines from a module available to your main program. So you can say copy( $file, $tofile); instead of File::Copy::copy( $file, $tofile ); .

The use keyword is just automated for you. Also, use executed at compile time before your program was executed. This allows you to use modules, so you can say foo( @array ) instead of foo( \@array ) or munge $file; instead of munge( $file );

As the use perldoc page says:

It [uses] is equivalent exactly :
BEGIN { require Module; Module->import( LIST ); }

Basically, you should use use more require 99% of the time.

I can only think of one case where you need to use require over use , but this is only for emulating use . There are times when a module is optional. If Foo::Bar is available, I can use it, but if it is not, I will not. It would be nice if I could check if Foo::Bar .

Let's try this:

 eval { use Foo::Bar; }; my $foo_bar_is_available = 1 unless ( $@ ); 

If Foo::Bar not available, I get the following:

 Can't locate Foo/Bar.pm in @INC (@INC contains:....) 

This is because use happens before I can run eval on it. However, I know how to emulate use with require :

 BEGIN { eval { require Foo::Bar; Foo::Bar->import( qw(foo bar barfu) ); }; our foo_bar_module_available = 1 unless ( $@ ); } 

It works. Now I can check this in my code:

 our $foo_bar_module_available; if ( $foo_bar_module_available ) { fubar( $var, $var2 ); #I can use it } else { ... #Do something else } 
+7
source

The difference between use and requirement:

  • If we use "use", you do not need to specify the file extension. Example: using server_update_file.
  • If we use "require", you must provide the file extension. Example: requires "Server_update_file.pm";
  • The "use" method is used only for modules.
  • The "require" method is used for both libraries and modules.

Refer to the link for more information: http://www.perlmonks.org/?node_id=412860

+1
source

Source: https://habr.com/ru/post/951715/


All Articles