有没有办法"使用"单个文件,而这个文件又在Perl中使用多个其他文件?

cow*_*god 9 perl module

我想创建几个模块,这些模块将用于我项目中的几乎所有脚本和模块.这些可以在我的每个脚本中使用 d,如下所示:

#!/usr/bin/perl

use Foo::Bar;
use Foo::Baz;
use Foo::Qux;
use Foo::Quux;

# Potentially many more.
Run Code Online (Sandbox Code Playgroud)

是否可以将所有这些use语句移动到一个新模块Foo::Corge,然后只需要use Foo::Corge在我的每个脚本和模块中移动?

jro*_*way 7

这样的事情应该有效:

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. '::' };
}
Run Code Online (Sandbox Code Playgroud)

然后在其他地方使用Lots :: Of :: Modules;

use Lots::Of::Modules;
confess 'OH NOES';
Run Code Online (Sandbox Code Playgroud)


inn*_*naM 7

是的,有可能,但不,你不应该这样做.

我花了两个星期的时间来摆脱一个除了使用其他模块之外什么也没做的模块.我想这个模块开始简单而且无辜.但是多年来它变成了一个拥有大量使用语句的庞大的野兽,其中大多数都不需要我们的webapp任何特定的运行.最后,只需要20秒就可以"使用"该模块.它支持延迟复制和粘贴模块创建.

再说一遍:你可能会在几个月或几年内感到后悔.你有什么好处?您在几个模块中保存了几行输入.很重要.