加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

有没有办法“使用”一个单独的文件,反过来又使用Perl中的其他多

发布时间:2020-12-15 22:04:50 所属栏目:大数据 来源:网络整理
导读:我想创建几个将被用于我项目中几乎所有脚本和模块的模块.这些可以在我的每个脚本中使用,如: #!/usr/bin/perluse Foo::Bar;use Foo::Baz;use Foo::Qux;use Foo::Quux;# Potentially many more. 可以将所有这些使用语句移动到新的模块Foo :: Corge,然后只需在
我想创建几个将被用于我项目中几乎所有脚本和模块的模块.这些可以在我的每个脚本中使用,如:
#!/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';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读