perlpal has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im a bit hazy with regard to the differences between "use" and "require". On the outset , i understand that "require" specifies a particular version of perl and throws an error at compile time if the version is higher. "USE" includes the function of "require" as well as imports the modules listed and throws an error if any at compile time. Is there anything beyond this? Thanks in advance!

Replies are listed 'Best First'.
Re: "Use" vs "Require"
by JavaFan (Canon) on Jan 21, 2009 at 12:25 UTC
    A rule of thumb: use 'use' unless you have a good reason to use 'require'. If you're unsure what to use, use 'use'.

    For a short answer, what 'use' does is, it compiles the module (if not compiled already), then runs its code (this is not different from what require does). Afterwards, it calls the import routine (require doesn't do so). And all of it is done at compile time (require does it at runtime).

    For a longer answer, and a more detailed explaination of all the differences (I've left out some details) consult the manual pages.

      Getting a module via require, you can import. But you have to do so explicitly.

      Bad:

      require Cwd; print cwd();

      Good:

      require Cwd; Cwd->import; print cwd();

      Also fine, does not import:

      require Cwd; print Cwd::cwd();
Re: "Use" vs "Require"
by Anonymous Monk on Jan 21, 2009 at 11:44 UTC
Re: "Use" vs "Require"
by lakshmananindia (Chaplain) on Jan 21, 2009 at 15:16 UTC
Re: "Use" vs "Require"
by shmem (Chancellor) on Jan 21, 2009 at 17:43 UTC

    perldoc -q require turns up exactly what you are looking for. Apart from that, require returns the last evaluated expression of the required file, whereas use isn't allowed in an expression and therefore only works in void context.