FAQ 8.46 What's the difference between require and use?

Discussion in 'Perl Misc' started by PerlFAQ Server, Mar 5, 2011.

  1. This is an excerpt from the latest version perlfaq8.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

    --------------------------------------------------------------------

    8.46: What's the difference between require and use?

    (contributed by brian d foy)

    Perl runs "require" statement at run-time. Once Perl loads, compiles,
    and runs the file, it doesn't do anything else. The "use" statement is
    the same as a "require" run at compile-time, but Perl also calls the
    "import" method for the loaded package. These two are the same:

    use MODULE qw(import list);

    BEGIN {
    require MODULE;
    MODULE->import(import list);
    }

    However, you can suppress the "import" by using an explicit, empty
    import list. Both of these still happen at compile-time:

    use MODULE ();

    BEGIN {
    require MODULE;
    }

    Since "use" will also call the "import" method, the actual value for
    "MODULE" must be a bareword. That is, "use" cannot load files by name,
    although "require" can:

    require "$ENV{HOME}/lib/Foo.pm"; # no @INC searching!

    See the entry for "use" in perlfunc for more details.



    --------------------------------------------------------------------

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
     
    PerlFAQ Server, Mar 5, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,279
  2. Dan Bikle
    Replies:
    2
    Views:
    99
    Michael Schoen
    Nov 26, 2005
  3. Lars Olsson
    Replies:
    2
    Views:
    108
    Jim Weirich
    Dec 2, 2005
  4. PerlFAQ Server
    Replies:
    0
    Views:
    367
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    272
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page