SPUG: Fwd: Question -- use vs require

Tye McQueen tyemq at cpan.org
Mon Aug 25 20:55:31 PDT 2008

gmail hiding "reply to all" often leads to me forgetting that I need to use

---------- Forwarded message ----------
From: Tye McQueen <tye.mcqueen at gmail.com>
Date: Mon, Aug 25, 2008 at 8:53 PM
Subject: Re: SPUG: Question -- use vs require
To: Trey Harris <trey+spug at lopsa.org <trey%2Bspug at lopsa.org>>

On Mon, Aug 25, 2008 at 12:47 PM, Trey Harris
<trey+spug at lopsa.org<trey%2Bspug at lopsa.org>
> wrote:

> "The advantage" makes it sound like you should be preferring it; you
> shouldn't.  99%+ of the time, you want C<use>.

Well, I think the "99%+" figure is overstated.  'require' is perfectly
appropriate in quite a few situations and is preferred in quite a few
more-advanced situations.  But, yes, if you are unsure which to use, 'use'
is the way to go.

It is rarely the case that replacing 'require' with 'use' will cause you
problems (it can cause problems, but it /usually/ doesn't, especially for
basic uses) and the reverse is not true (if you have code using 'use', it
will often be because 'use' is required and switching to 'require' is likely
to break things).

> You can use C<require> in a string eval so the load happens at runtime
> rather than compile time

No.  Simply using 'require' is enough to make the loading happen at
run-time.  You can also use 'use' in a /string/ eval in order to get the
loading and importing to happen at run-time but that also opens up lots of
potential problems so it is a technique that should not be used lightly.

> You can also use C<require> to prevent the C<import> method from being
> called (though ordinarily this isn't something you want to do, you should do
> a C<use Module qw();> instead to call C<import> with no arguments).

Actually, qw() like that prevents import() from being called at all.  To
call import() with no arguments, write simply 'use Module;'. And rather than
just stating that 'require' should be avoided, I'll provide reasons why one
might or might not want to avoid it. :)

>  3. What are the implications for object-oriented programming?
> That's a very broad question, but I don't think there are any implications
> of use vs. require for OOP versus any other type of programming.

I would say that it is unusual for an OO module to export things so it is
rare to have an OO module where 'require' is not sufficient.  So, for a
/typical/ OO module, there is very little difference between 'use'ing it vs.
'require'ing it.

Even loading at compile-time vs. run-time makes little difference in a
typical case with an OO module because the 'require' is usually
unconditionally executed near the very top of the requiring script/module.
"perl -c" is one of the rare cases where you would see a difference ("perl
-c" will check the syntax of any modules you 'use', but not of any modules
you 'require').

If you have compile-time code below that makes use of the module, than you
should certainly 'use' the module.

Oh, let's not forget the "obvious" case: If you import symbols/behaviors
from the module, then you need to 'use' the module.

There are certainly plenty of advanced situations where 'require' is a
better choice than 'use'.  For the basic situations, the main reason I would
use 'require' is to make it clear that none of the syntax that follows is
dependent on the module.

One only slightly advanced situations where 'require' can be beneficial is
when you have rarely-used part(s) of your code that are the only places
where some heavy-weight module is needed.  In those situations, it may be
worth using 'require' inside of the routine(s) that need the module so that
the many runs that don't exercise those features won't need to waste time
even loading the unutilized module.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20080825/b6ca36c8/attachment.html>

More information about the spug-list mailing list