[Israel.pm] use and require

Gabor Szabo szabgab at gmail.com
Sat Mar 19 23:48:19 PDT 2011

On Sun, Mar 20, 2011 at 5:15 AM, Shmuel Fomberg <owner at semuel.co.il> wrote:
> On 2011/03/13 19:45, sawyer x wrote:
>>     Whenever I see 'require' used instead of 'use', I expect problems
>>     in the
>>     module-dependency tree.
>> I think there are benefits to both. It depends, as always, on your needs.
>> If you have a healthy build process, you can use "require" instead of
>> "use" when you want to allow some features to be lazy, and that's very
>> useful. You can also allow loading two different versions of the same
>> mechanism, depending on some logic. If you have this, load that one,
>> if not, load this one. Imagine Mouse only uses Moose if it's already
>> loaded, saving you a ton on compile-time.
> While you are right, I think that these 'require' statements should be
> in very specific places. plugins finding and loading code, dynamic
> feature loading, and so on.
> but not littered through the code.
> Especially the main perl file, should 'use' all the modules that are
> known to be needed for running it. no point leaving it to later point of
> time.

"littered through the code." sound very harsh. I'd say
"strategically placed at the location where the package has to be loaded"

I don't know what other usage of "require" you have seen. I saw quite
a lot of code where people mixed "require" and "use" statements
in an arbitrary manner. This is probably written by people who did not
understand what the meaning of these two statements are and just
added them based on cargo-cult.

I think it totally depends on the usage pattern. For a persistent web
application loading everything up front won't hurt performance.
For an editor where people want to start typing ASAP and don't
want to wait for all the subsystems to load you will serve them better
by delaying everything as much as possible.


More information about the Perl mailing list