Home: Perl Programming Help: Beginner:
use Something;



SirAnvil
Deleted

Dec 31, 2000, 8:17 PM


Views: 326
use Something;

Question!

I've seen scripts use a file that it came with such as

use somefile.pm;

and in the somefile.pm it has a bunch of pre-defined variables like
$variable1="apples";
$variable2="oranges";
and so on. So when I say use somefile.pm does that load the contents of that file into a cache?

SirAnvil


japhy
Enthusiast

Jan 1, 2001, 7:58 AM


Views: 322
Re: use Something;

The use compile-time directive is like calling require(), but: a) it happens at compile-time, b) it also calls the import() method of the module, if it exists, and c) it only works with modules (files ending in .pm).

It's used like so:


Code
use Some::Module;  # the file is Some/Module.pm 
use Foobar 'blat'; # the file is Foobar.pm

# are like

BEGIN {
require Some::Module;
Some::Module->import;
}
BEGIN {
require Foobar;
Foobar->import('blah');
}

The import() method allows you to access specific parts of the module to be copied into your program, instead of having to access them from the module's namespace. Some modules import things automatically.

To learn more about modules, please read perldoc perlmod.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author