In Perl, is it better to use a module than to require a file?

前端 未结 4 1339
抹茶落季
抹茶落季 2020-12-04 09:26

Another question got me thinking about different methods of code reuse: use vs. require vs. do

I see a lot of posts here where

4条回答
  •  Happy的楠姐
    2020-12-04 09:56

    The main difference is around import/export. use is preferred when you're making use of a module because it allows you to specify which routines you wish to import into your namespace :

    use MyModule qw(foo bar baz); # allows foo(), bar() and baz() to be used
    
    use MyModule qw(); # Requires explicit naming (e.g. MyModule::foo).
    

    use also gives runs the module's import() procedure which is often used to set the module up.

    See the perldoc for use for more detail.

提交回复
热议问题