这就是我想做的:
在我制作/开发的每个脚本中,我总是调用 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
这可能吗?如果可能的话?如果可能的话,从调用库到调用检查器脚本可能会非常通用和标准。
创建一个加载其他“标准”模块的模块是诸如此类的事情的动机perl5i http://metacpan.org/pod/perl5i and 现代::Perl http://metacpan.org/pod/Modern::Perl.
具有词法效果的 Pragma 模块(例如 strict、警告和 autodie)只需加载到模块的导入例程中即可。导出函数的模块需要被告知将其模块导出到其他地方,这可以通过导入::到 http://metacpan.org/pod/Import::Into。最后,只需加载类即可。
Since use http://perldoc.perl.org/functions/use.html发生在编译时,您需要在运行时执行等效操作,即require
ing模块并调用它的import
method.
下面是打开严格和警告、加载 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 模块来执行此操作。出于同样的原因,我不会让模块执行任何代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)