如何包含一个包含所有必需的Perl模块名称的文件? [英] How to include a file that contains all the required Perl module names?

查看:62
本文介绍了如何包含一个包含所有必需的Perl模块名称的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

perl脚本在脚本开头包含所有模块名称.

The perl scripts contain all the module names in the beginning of the script.

例如:

use strict;
use XML::Parser;
use XML::Simple;
use DBI;
use DBD::DB2::Constants;
use POSIX qw( strftime );
use Storable qw(dclone);
use Data::Dumper;
use Carp;

如何将所有模块名称保留在另一个文件中,并将该文件包含在主perl脚本中?

How to keep all the module names in another file and include the file in main perl script ?

谢谢.

推荐答案

创建自己的模块.但是,如果您在此模块中制作软件包,则这些strftime等将被导入到另一个命名空间中.您可以做点小技巧来做到这一点:

Create your own module. But, if if you make package in this module those strftime etc will be imported into another namespace. You can do litle hack to do this smiple:

MyModules.pm:

MyModules.pm:

use strict;
use XML::Parser;
use XML::Simple;
use DBI;
use DBD::DB2::Constants;
use POSIX qw( strftime );
use Storable qw(dclone);
use Data::Dumper;
use Carp;

请注意,此.pm中没有package关键字.您的脚本:

Note there was no package keyword in this .pm. Your script:

use MyModules;

将此.pm与脚本放置在同一目录中,或在运行时添加模块搜索路径:

Place this .pm into same dir with script or add modules search path at runtime:

use lib '/my_modules_dir/';
use MyModules;

不建议使用

使用 do require 命令,因为它们不会在脚本启动时检查语法.当然,您可以将它们放入BEGIN块中,但我认为它很棘手,通常BEGIN{ require ..}use

Use of do and require commands is not recommended, since they do not check syntax on script start. Sure you can place them into BEGIN block but i think its tricky way and generally BEGIN{ require ..} is the same as use

UPD: ikegami 指出了当您从许多模块中使用此功能时,该功能不起作用的问题.不管问题入门者的信息(他想从主脚本中使用它),还是从许多模块中添加如何使用它的信息,所以我的朋友会感觉更好.添加到MyModules.pm的末尾:

UPD: ikegami noted problem its not working when you use this from many modules. Regardless question starter's information (he want to use this from main script), ill add info how to use this from many modules, so my friend can feel better. Add to end of MyModules.pm:

delete $INC{'MyModules.pm'};
1;

这篇关于如何包含一个包含所有必需的Perl模块名称的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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