这就是我想要做的事情:
在我制作/开发的每个脚本中,我总是调用perl库和子例程,如:
#! /directory/bin/perl
system('source /directory/.cshrc&');
use Net::Domain qw(hostname hostfqdn hostdomain);
use Time::Local;
use Time::Piece;
use Switch;
use Exporter;
#use strict;
use Data::Dumper qw(Dumper);
use Time::Local;
use Time::Piece;
use Time::Seconds();
use Tk;
use Tk::BrowseEntry;
use Tk::Balloon;
use Tk::widgets qw(Checkbutton BrowseEntry);
use Tk::NoteBook;
use Tk::Pane;
use DBI;
use DBD::Oracle;
$ORACLE_HOME = "/lolDirectory/10.2.0/elinux";
$ENV{ORACLE_HOME}=$ORACLE_HOME;
###############
# SUBROUTINES #
###############
&ownerChecker;
&processChecker;
我希望我可以把所有这些文件放到一个文件中并加载到perl脚本中,同时运行它就好像它是perl脚本本身的一部分一样:
#! /directory/bin/perl
# load the content of the file and run it as a part of the script
这可能吗?如果有可能?如果可能的话,从调用libs到调用checker脚本可能是非常通用和标准的。
答案 0 :(得分:9)
创建一个加载其他“标准”模块的模块是perl5i和Modern::Perl之类的动机。
具有词汇效果的Pragma模块,例如strict,warnings和autodie,只需要在模块的导入例程中加载。需要告诉导出函数的模块将其模块导出到其他地方,这可以使用Import::Into来完成。最后,只需要加载类。
由于use
在编译时发生,您需要在运行时执行等效的require
模块并调用其import
方法。
这是打开严格和警告,加载Time :: Local并加载Time :: Piece,以及激活say和switch功能的示例。
package My::Perl;
use strict;
use warnings;
use Import::Into;
sub import {
# import is called as a class method
my $class = shift;
# The class which 'use'd this module
my $caller = caller;
# same as "use strict" but happens when import() is called.
require strict;
"strict"->import;
# use warnings;
require warnings;
"warnings"->import;
# use Time::Local;
# use Time::Piece;
Time::Local->import::into($caller);
Time::Piece->import::into($caller);
# use feature qw(say switch);
require feature;
feature->import(qw(say switch));
}
1;
现在你只需要加载那个模块。
use My::Perl;
say localtime->year;
不要太疯狂,你希望这些通常有用。如果你不打算使用它们,加载DBI和Tk是很愚蠢的。如果你想加载一堆Tk模块,可以单独创建一个My :: Tk模块。而且由于同样的原因,我不会让模块执行任何代码。