2013-03-20 37 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