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 );