有没有办法“使用"?一个文件又在 Perl 中使用多个其他文件? [英] Is there a way to "use" a single file that in turn uses multiple others in Perl?

查看:18
本文介绍了有没有办法“使用"?一个文件又在 Perl 中使用多个其他文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to create several modules that will be used in nearly all scripts and modules in my project. These could be used in each of my scripts like so:

#!/usr/bin/perl

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

# Potentially many more.

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

Is it possible to move all these use statements to a new module Foo::Corge and then only have to use Foo::Corge in each of my scripts and modules?

推荐答案

这样的事情应该可行:

http://mail.pm.org/pipermail/芝加哥谈话/2008-March/004829.html

基本上,创建包含大量模块的包:

Basically, create your package with lots of modules:

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;

Then use Lots::Of::Modules elsewhere;

use Lots::Of::Modules;
confess 'OH NOES';

这篇关于有没有办法“使用"?一个文件又在 Perl 中使用多个其他文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆