解决方法
还有其他方法,但它们都有严重的问题.模块是要走的路,它们不一定非常复杂.这是一个基本模板:
package Mod; use strict; use warnings; use Exporter 'import'; #list of functions/package variables to automatically export our @EXPORT = qw( always_exported ); #list of functions/package variables to export on request our @EXPORT_OK = qw( exported_on_request also_exported_on_request ); sub always_exported { print "Hi\n" } sub exported_on_request { print "Hello\n" } sub also_exported_on_request { print "hello world\n" } 1; #this 1; is required,see perldoc perlmod for details
创建一个像/ home / user / perllib这样的目录.将该代码放在该目录中名为Mod.pm的文件中.您可以像这样使用模块:
#!/usr/bin/perl use strict; use warnings; #this line tells Perl where your custom modules are use lib '/home/user/perllib'; use Mod qw/exported_on_request/; always_exported(); exported_on_request();
当然,您可以根据需要为文件命名.将包命名为与文件相同是一种好的形式.如果你想在包的名称中使用::(如File :: Find),你需要在/ home / user / perllib中创建子目录.每个::相当于一个/,所以My :: Neat :: Module将进入文件/home/user/perllib/My/Neat/Module.pm.您可以在perldoc Exporter
中阅读有关perldoc perlmod
及更多有关Exporter的模块的更多信息