in reply to Where in a package should 'use' be called?
Both are useful. Typically people place the "use" statements at the top, and if necessary, document why. You can place the use almost anywhere though. However, keep in mind that since use has a compile-time component to it, its effects are not constrained to the scope of whatever sub calls use:
use feature qw/say/; say "Outside of sub: ", sum(1..10); say "Inside of sub: ", foo(1..10); sub foo { use List::Util qw(sum); return sum(@_); }
As you can see, both calls have access to sum
For this reason, you will sometimes see code that needs to conditionally load a module use require. This won't automatically import the names that the target module exports, you would then have to call that module's import. Or if you want to keep your namespace clean, just use fully-qualified names.
Another way to conditionally load a module is with the if module.
Dave
|
|---|