Passing Main script variables into Perl Modules

旧街凉风 提交于 2019-12-20 07:56:43

问题


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.

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...

Thanks


回答1:


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
                            );


来源:https://stackoverflow.com/questions/12117120/passing-main-script-variables-into-perl-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!