将主脚本变量传递到Perl模块中 [英] Passing Main script variables into Perl Modules
问题描述
我正在编写一个由用户运行的Perl脚本,该脚本还将当前的Linux环境用作变量和其他变量.环境设置可能会更改,并且可能与原始设置有所不同.
I am writing a Perl script that is run by a user and makes use of the current Linux environment as variables and other variables as well. The environment settings may change and be different from what they were originally.
但是,我试图使用独立的Perl模块,并且需要能够访问这些变量.进行此操作的最佳实践是什么?使用Perl模块创建对象时,我只能传递10个变量,但这似乎过多...
However, I'm trying to use self-contained Perl Modules and need to be able to access these variables. What is the best practice to go about doing this? I can just pass along 10 variables when I create an object using the Perl Module, but that seems excessive...
谢谢
推荐答案
可以从全局%ENV
哈希中的任何位置访问环境变量:
The environment variables are accessible from anywhere in the global %ENV
hash:
print $ENV{HOME};
如果您要创建对象,则它们可能具有某些属性(例如,对象散列,数组,甚至是由内而外的对象……)只需将相关值存储在属性中即可,例如
If you are creating objects, they probably have some attributes (being the objects hashes, arrays or even inside out objects...) Just store the relevant values into the attributes, e.g.
my $obj = Some::Package->new( name => 'Homer',
surname => 'Simpson',
city => 'Springfield',
# ... 7 more
);
这篇关于将主脚本变量传递到Perl模块中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!