http://qs1969.pair.com?node_id=404783


in reply to use and require inside subs

You seem to be making a distinction between subroutines in .pl files and modules. There is no such distinction. Modules are typically just a collection of subroutines/functions/methods/widgets - call them what you will. See Simple Module Tutorial. Anyway besides suggesting that you put everyting into modules for consistency and simplicity's sake the main difference between use and require is that use does a require PLUS calls the modules import function. Never seen a module with an import() function? That is becaue it is inherited from Exporter Depending on what the module exports by default (in @EXPORT) and what you ask for (in @EXPORT_OK) zero or more functions/subroutines will become available via their unqualified name ie you can call somefunction(). You can *always* call Some::Module::somefunction() by its fully qualified name, regardless of whether or not you have imported it. The only pre-requisite is that you have used or required that module first.

The other significant difference between use and require is that use happens at compile time. require happens at runtime as Perl stumbles across it. require is almost the same as do (it uses it), except it will search @INC for you. For example you can do (not recommended) this to effectively get very similar results to useing a module:

do 'd:/perl/lib/Data/Dumper.pm'; Data::Dumper->import(); print Dumper(\@INC,\%INC);

So what do I personally do? Almost all code is in modules. In a given script/module I will use any other Module I know I will need, and import the functions that always get used. In any given function, if it is possible that a module has not been used and/or the required function imported I will have:

sub widget { # blah require Some::Module; my $result = Some::Module::somefunction(@args);

cheers

tachyon