SPUG: Question -- use vs require

Trey Harris trey+spug at lopsa.org
Mon Aug 25 12:47:45 PDT 2008


In a message dated Mon, 25 Aug 2008, Christopher Howard writes:
> Hi.  I was hoping someone could clarify some small points for me.  I read 
> some on-line material, but I want to make sure I'm understanding things 
> correctly.
> 1. What is the difference between 'use' and 'require'?

% perldoc -f use
[...]
                It is exactly equivalent to

                    BEGIN { require Module; import Module LIST; }

                except that Module must be a bareword.


> 2. What is the advantage to using 'require' (if there is one)?

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

You can use C<require> in a string eval so the load happens at runtime 
rather than compile time (that's what the C<BEGIN> block is doing up 
there).  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).

> 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.

-- 
Trey Harris                                          http://www.lopsa.org/
President, LOPSA   --     The League of Professional System Administrators
Opinions expressed above are not necessarily those of LOPSA.


More information about the spug-list mailing list