在我之前的问题中Moose - Loading values from conf files... Jack Maney 非常友善地提供了如何使用 Moose 来实现此目的的示例。
为了使配置对象更加通用,我决定使用 Config::Auto .
问题是我对 Moose 的工作原理还很陌生。例如,Jack 的例子是:
package My::Module;
use Moose;
has 'config'=>(isa=>'HashRef[Str]',is=>'rw',required=>1);
around BUILDARGS=>sub
{
my $orig=shift;
my $class=shift;
my $args=shift; #other arguments passed in (if any).
my %config_hash=();
open(my $read,"<","config_file") or confess $!;
while(<$read>)
{
chomp;
my @array=split /:/;
$config_hash{$array[0]}=$array[1];
}
close($read);
$args->{config}=\%config_hash;
return $class->$orig($args);
};
no Moose;
1;
我已将其修改为:
#!/usr/local/bin/perl
package DART::Setup;
use namespace::autoclean;
use Moose;
use Config::Auto;
our $VERSION = '0.0.1';
has 'EMPTY' => ( isa => 'Str', is => 'ro', default => q{} );
has 'PPLTESTEXECUTIONID' => ( isa => 'Int', is => 'ro', default => 0 );
has 'SARQTESTEXECUTIONID' => ( isa => 'Int', is => 'ro', default => 0 );
has 'ISPROXY' => ( isa => 'Int', is => 'ro', default => 0 );
has 'LOCALHOST' => ( isa => 'Str', is => 'ro', default => '127.0.0.1' );
has 'config'=>(isa=>'HashRef[Str]',is=>'rw',required=>1);
has 'SSO' => ( isa => 'Str', is => 'rw', default => q{} );
has 'cookieFile' => ( isa => 'Str', is => 'rw', default => q{} );
around BUILDARGS=>sub
{
my $orig=shift;
my $class=shift;
my $args=shift;
my $cfg = Config::Auto::parse($args);
my %config_hash = %{$cfg};
$args->{config}=\%config_hash;
return $class->$orig($args);
};
return 1;
但说实话,我不确定我在这里做什么。首先,当我创建一个新的安装对象时,我需要提供多少个参数?我是否只需将其路径传递给我的配置文件,例如:
my $newConfig = DART::Setup->new('/home/y/conf/MyApp/MyApp.cfg');
或者我需要为 $orig 和 $class 提供参数吗?
最后,我现在如何访问新加载的配置?我可以做这样的事情:
my %configHash = %{$newConfig->config()};
foreach my $key (keys %configHash) {
print "the key is, $key, and the value is: $configHash{$key}\n";
}
我的理解正确吗?
最佳答案
好吧,在 BUILDARGS
内部,您想要读取配置文件并将键值对传递到 config
属性中。这是配置文件的另一个属性的修改版本。
package My::Module;
use Moose;
use Config::Auto;
has 'config'=>(isa=>'HashRef[Str]',is=>'rw',required=>1);
has 'config_file'=>(isa=>'Str',is=>'ro');
around BUILDARGS=>sub
{
my $orig=shift;
my $class=shift;
my $args=shift; #currently {config_file=>'/path/to/file/config_file.conf'} (or whatever)
#make sure we've been passed a config file
confess "No config file found in BUILDARGS" unless defined $args->{config_file};
#Now, we open the user-specified config file via Config::Any
my $ca=Config::Auto->new(source=>$args->{config_file},format=>"colon");
my $parsed=$ca->parse; #hash reference containing the parsed data.
#Now, we add this to our arguments that will become our attributes:
$args->{config}=$parsed;
return $class->$orig($args);
}
no Moose;
1;
关于BUILDARGS
,要认识到的主要一点是它接受以下参数:类的名称和原始构造函数(传递给Moose::Object
)然后传递给构造函数的任何其他参数。所以,如果你打电话
my $mm=My::Module->new({config_file=>"/path/to/file/file.conf"});
然后,在BUILDARGS
中,我们最初有
$args=={config_file=>"/path/to/file/file.conf"}
但是解析文件并添加 $parsed
哈希引用后,它变成了
$args=={config_file=>"/path/to/file/file.conf",config=>{server=>"mozilla.org",protocol=>"HTTP",...}}
等等等等
通过在 BUILDARGS
中写入 my $cfg = Config::Auto::parse($args);
,您试图传递 config_file
参数传递给 Config::Auto
中的解析器,它不知道如何处理它。
关于Perl Moose - 从配置文件加载值时的参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9779905/