使用Config :: Any和Config :: General Driver插入环境变量

时间:2013-03-20 13:44:04

标签: perl configuration

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

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 个答案:

答案 0 :(得分: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
相关问题