J
Jason C
This is both a "can I" and "should I" question. Meaning, does it work, and if so, is there a reason to not do it?
I have a variables.lib that I use to hold most of my recurring variables and functions, then "require 'variables.lib' in the Perl scripts.
What I'm curious about is, can I "use" a module in a subroutine in the variables.lib, or does it have to be "use"d outside of the sub or in the main script?
Example:
# variables.lib
sub copyFile {
use File::Copy;
# Use: copyFile('file.txt', 'newfile.txt');
copy("$basepath/$_[0]", "$basepath/$_[1]")
or die "Copy failed: $!";
}
1;
That example is just typed up for this post, so please forgive any typos or logic errors. And if this is acceptable, then I would be doing it with several modules, not just File::Copy, this is isn't a module-specific question.
If "use"ing a module like this inside of a sub is acceptable, then am I correct that it wouldn't be loaded until (or unless) necessary? Or does Perl read ahead and load everything at once, regardless of whether the function or module is actually used?
I have a variables.lib that I use to hold most of my recurring variables and functions, then "require 'variables.lib' in the Perl scripts.
What I'm curious about is, can I "use" a module in a subroutine in the variables.lib, or does it have to be "use"d outside of the sub or in the main script?
Example:
# variables.lib
sub copyFile {
use File::Copy;
# Use: copyFile('file.txt', 'newfile.txt');
copy("$basepath/$_[0]", "$basepath/$_[1]")
or die "Copy failed: $!";
}
1;
That example is just typed up for this post, so please forgive any typos or logic errors. And if this is acceptable, then I would be doing it with several modules, not just File::Copy, this is isn't a module-specific question.
If "use"ing a module like this inside of a sub is acceptable, then am I correct that it wouldn't be loaded until (or unless) necessary? Or does Perl read ahead and load everything at once, regardless of whether the function or module is actually used?