In Perl, what is the difference between use and require for loading a module?

后端 未结 4 577
一生所求
一生所求 2021-02-03 22:24

What is the difference between doing use My::Module and require My::Module?

4条回答
  •  忘了有多久
    2021-02-03 22:35

    Perl comes with great documentation. Everyone would benefit from reading the entire documentation at least once every few months.

    C:\> perldoc -f require

    Otherwise require demands that a library file be included if it hasn't already been included. The file is included via the do-FILE mechanism, which is essentially just a variety of eval with the caveat that lexical variables in the invoking script will be invisible to the included code. Has semantics similar to the following subroutine:

    ... etc. Similarly,

    C:\> perldoc -f use

    Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to

    BEGIN { require Module; Module->import( LIST ); }
    

    except that Module must be a bareword.

    ... etc

    There is also the perlfaq entry although I think it is less informative than the above.

提交回复
热议问题