2013-03-20 39 views
0

如何插入环境变量Config::General设置为Config::Any。这是我从我的OX应用程序中提取的配置片段,它返回我的.conf中指定的数据,除了我无法识别环境变量外。使用Config :: Any和Config :: General驱动程序插入环境变量

has config => (
    isa   => 'HashRef', 
    is   => 'ro', 
    lifecycle => 'Singleton', 
    dependencies => ['config_file'], 
    block  => sub { 
    my $s = shift; 

    my $cfg 
     = load_class('Config::Any')->load_files({ 
      flatten_to_hash => 1, 
      use_ext   => 1, 
      files   => [ $s->param('config_file') ], 
      General   => { 
       -InterPolateEnv => 1, 
      }, 
     }); 

    return $cfg->{ $s->param('config_file') }; 
    }, 
); 

这里的一对夫妇尝试对我的配置

<db> 
    dsn $PERL_FEEDER_DSN 
</db> 

<db> 
    dsn $ENV{PERL_FEEDER_DSN} 
</db> 

的这些都只是包含文字$... DSN结束。

我以前做过这件事,但我无法弄清楚我是怎么做到的,或者我可能做错了什么。

回答

1

您需要指定-InterPolateEnv => 1-InterPolateVars => 1一起,以便加载Config::General::Interpolated。据我了解,-InterPolateEnv => 1本身不会触发加载该模块。这是该模块可以理解的选项。

#!/usr/bin/env perl 

use strict; 
use warnings; 
use Config::General; 

my $conf = new Config::General(
    -ConfigFile  => 'test.conf', 
    -InterPolateEnv => 1, 
    -InterPolateVars => 1, 
); 

use YAML; 
print Dump { $conf->getall }; 

配置文件:

<db> 
    dsn $PERL_FEEDER_DSN 
</db>

输出:

--- 
db: 
    dsn: testing
+0

不谓是什么做的? 'General => { -InterPolateEnv => 1, },' – xenoterracide 2013-03-20 22:21:25

+0

对不起,我的意思是它需要与-InterPolateVars => 1一起指定,以便Config :: General :: Interpolated被加载。 – 2013-03-20 23:28:21

+0

啊,看我可以发誓,文件说,InterPolateEnv暗示InperPolateVars – xenoterracide 2013-03-21 00:13:53