How does a Perl program know where to find the file containing Perl module it uses?

后端 未结 3 1161
悲&欢浪女
悲&欢浪女 2020-12-01 12:58

If my Perl program uses Perl modules, how will it determine where to find the file containing the module code?

For example, if the program contains:



        
3条回答
  •  [愿得一人]
    2020-12-01 13:25

    According to the perlfunc documentation on use:

    use Module LIST

    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.

    So require does the heavy lifting, and the require documentation provides

    If EXPR is a bareword, the require assumes a ".pm" extension and replaces "::" with "/" in the filename for you, to make it easy to load standard modules. This form of loading of modules does not risk altering your namespace.

    In other words, if you try this:

       require Foo::Bar;    # a splendid bareword
    

    The require function will actually look for the "Foo/Bar.pm" file in the directories specified in the @INC array.

提交回复
热议问题