Perl - Use all modules from specified subdirectory

2019-07-13 05:44发布

I have two modules:

./My/Module1
./My/Module2

Module1 is using subroutines from Module2. So in my script i typed following:

use My::Module1
use My::Module2

But this does not worked and perl complained that subroutines which are used from Module2 by Module1 does not exists. So I added following line to Module1:

use My::Module2

Finally this worked as expected.

I am wondering if there is some solution that will include all modules from specified sub-directory tree and solve dependencies automatically. I do not want to type use keyword in modules which depends on another modules. Following commands was tried but it did not worked (either by syntax errors or it used wrong modules):

use My::;
use My::*;
use My;

Also I would ask if this cross-using modules and calling it's subroutines is considered as a good practice in perl programming?

PS: @INC contains current directory so loading modules is working.

PPS: Modules used Exporter

1条回答
何必那么认真
2楼-- · 2019-07-13 06:24

I do not want to type use keyword in modules which depends on another modules.

Then type the BEGIN, require, and import keywords instead?

Seriously, there's no good way for this to work. Just use use in each module so that it can load the things it needs.

Also I would ask if this cross-using modules and calling it's subroutines is considered as a good practice in perl programming?

Yes. Modularization is considered good practice in all programming.

查看更多
登录 后发表回答