If the source file does not define new namespaces or classes and you just want to read the function definitions or data from a file, Perl provides the do
and require
functions.
do "scripting";
require "scripting";
The difference between them is that require
will look for the file to evaluate to a true value (it expects the last statement in the file to resolve to a non-zero, non-empty value), and will emit a fatal error if this does not happen. (You will often see naked 1;
statements at the end of modules to satisfy this requirement).
If scripting
really contains class code and you do need all the functionality that the use
function provides, remember that
use Foo::Bar qw(stuff);
is just syntactic sugar for
BEGIN {
$file = <find Foo/Bar.pm on @INC>;
require "$file";
Foo::Bar->import( qw(stuff) )
}
and suggests how you can workaround your inability to use use
:
BEGIN {
require "scripting";
scripting->import()
}
In theory, the file scripting
might define some other package and begin with a line like package Something::Else;
. Then you would load the package in this module with
BEGIN {
require "scripting";
Something::Else->import();
}