我想创建几个将被用于我项目中几乎所有脚本和模块的模块.这些可以在我的每个脚本中使用,如:
#!/usr/bin/perl use Foo::Bar; use Foo::Baz; use Foo::Qux; use Foo::Quux; # Potentially many more.
可以将所有这些使用语句移动到新的模块Foo :: Corge,然后只需在我的每个脚本和模块中使用Foo :: Corge?
解决方法
这样的事情应该有效:
http://mail.pm.org/pipermail/chicago-talk/2008-March/004829.html
基本上,创建包含许多模块的包:
package Lots::Of::Modules; use strict; # strictly optional,really # These are the modules we want everywhere we say "use Lots::Of::Modules". # Any exports are re-imported to the module that says "use Lots::Of::Modules" use Carp qw/confess cluck/; use Path::Class qw/file dir/; ... sub import { my $caller = caller; my $class = shift; no strict; *{ $caller. '::'. $_ } = \*{ $class. '::'. $_ } for grep { !/(?:BEGIN|import)/ } keys %{ $class. '::' }; }
然后在其他地方使用Lots :: Of :: Modules
use Lots::Of::Modules; confess 'OH NOES';