在 Perl 中,使用 Appconfig 我希望能够在另一个配置文件中包含 cfg 文件

发布于 2024-12-15 01:15:16 字数 600 浏览 0 评论 0原文

我做了很多研究,但没有运气......

我想要一个配置文件,其中包含一些默认值或通用值。然后让其他配置文件包含默认配置,并根据需要覆盖变量。

示例:

Default.cfg :

[${name}]
   bin      = /bin/1   
   tmp_dir  = /tmp
   tmp_file = /tmp/${name}_tmp_file

------------------------------

myProc.cfg :

name  = Test_proc

[${name}]    
   bin   = /bin/TEST

这样,myProc.cfg 使用 default.cfg 中定义的相同 tmp_dir,但定义“名称”并重新定义 bin 目录。

好吧,这个例子非常简单,但我有无数的配置,它们共享相同的 cfg 行,但略有不同,我正在尝试合并配置。

我的应用程序采用 Perl 语言,并且已经使用 AppConfig

预先感谢您的帮助。

I've done a lot of research but no luck...

I want to have a config file with some default or generic values in it. Then have other config files INCLUDE that default config, and overwrite variables if needed.

example:

Default.cfg :

[${name}]
   bin      = /bin/1   
   tmp_dir  = /tmp
   tmp_file = /tmp/${name}_tmp_file

------------------------------

myProc.cfg :

name  = Test_proc

[${name}]    
   bin   = /bin/TEST

This way, myProc.cfg uses the same tmp_dir defined in the default.cfg but defines the "name" and redefines bin dir.

Well, this example is very simple but I have gizillion configs that share the same lines of cfg but differ a little and I'm trying to consolidate configs.

My app is in Perl and already using AppConfig.

Thank you in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-12-22 01:15:16

Config::General 应该能够处理这个问题,但是如果您想坚持使用 AppConfig,只需创建一个子类,通过手动处理包含来预处理配置文件,说s/_my_special_include_syntax = (.+)$/IncludeThis($1)/ge;
据我所知,这应该可以在没有任何其他修改的情况下工作,因为最后一个分配获胜,因此,最后一个分配会覆盖默认值

Config::General ought to be able to handle this, but if you wish to stick with AppConfig, simply create a subclass, that preprocesses the config file, by manually processing includes , say s/_my_special_include_syntax = (.+)$/IncludeThis($1)/ge;
afaik, this should work without any other modifications, since last assignment wins, thus, the last assignment overrides defaults

你没皮卡萌 2024-12-22 01:15:16

YAML 可以处理一个文件中的多个“文档”。因此可以使用 YAML 来分​​隔多个 AppConfig 文档。

我还没有真正深入研究AppConfig,但是像下面这样的东西在调整后应该可以工作。

use strict;
use warnings;

use English qw<$RS>;
use YAML qw<LoadFile>;

sub open_strings {
    my @results = map { 
        open( my $h, '<', \$_ );
        $h;
    } @_;
    return wantarray ? @results 
         : @_ == 1   ? $results[0] 
         :             \@results
         ;
}

my @cfgs 
    = map { my $c = AppConfig->new(); $c->file( $_ ); $c } 
      open_strings( LoadFile( $fh_or_fname ))
    ;

下面的代码展示了如何将它们分成文档,您甚至可能不需要这样做。第三个文档是文件到文本的映射。

--- | 

  [${name}]
     bin      = /bin/1   
     tmp_dir  = /tmp
     tmp_file = /tmp/${name}_tmp_file

--- > 

  name  = Test_proc

  [${name}]    
     bin   = /bin/TEST

---
Default.cfg : |
  [${name}]
     bin      = /bin/1   
     tmp_dir  = /tmp
     tmp_file = /tmp/${name}_tmp_file

Myproc.cfg : | 

  name  = Test_proc

  [${name}]    
     bin   = /bin/TEST

YAML can handle multiple "documents" in a file. So it's possible to use YAML to separate several AppConfig documents.

I haven't done real diving into AppConfig, but something like the following should--when tuned--work.

use strict;
use warnings;

use English qw<$RS>;
use YAML qw<LoadFile>;

sub open_strings {
    my @results = map { 
        open( my $h, '<', \$_ );
        $h;
    } @_;
    return wantarray ? @results 
         : @_ == 1   ? $results[0] 
         :             \@results
         ;
}

my @cfgs 
    = map { my $c = AppConfig->new(); $c->file( $_ ); $c } 
      open_strings( LoadFile( $fh_or_fname ))
    ;

The code below shows how you can separate them up into documents, and that you might not even have to. The third document is a mapping of file to text.

--- | 

  [${name}]
     bin      = /bin/1   
     tmp_dir  = /tmp
     tmp_file = /tmp/${name}_tmp_file

--- > 

  name  = Test_proc

  [${name}]    
     bin   = /bin/TEST

---
Default.cfg : |
  [${name}]
     bin      = /bin/1   
     tmp_dir  = /tmp
     tmp_file = /tmp/${name}_tmp_file

Myproc.cfg : | 

  name  = Test_proc

  [${name}]    
     bin   = /bin/TEST
兲鉂ぱ嘚淚 2024-12-22 01:15:16

答案很晚,但我发现 AppConfig 可以运行多次。

为了发布我的文件,我将所有登录名/密码移至 configfile.ini.secret 并将示例登录名/密码留在 configfile.ini 中

$config->define("configFile=s", {DEFAULT=>$dirname."/eversolar.ini"});
$config->args();

// Configfile must exist   
-e $config->configFile or die "Configfile '", $config->configFile, "' not found\n";

// read configfile
$config->file($config->configFile);
pmu_log("Severity 3, Configfile is: " . $config->configFile . "\n");

// Look for secret file
$config->configFile($config->configFile.".secret");
if (-e $config->configFile) {
    $config->file($config->configFile);
    pmu_log("Severity 3, Configfile is: " . $config->configFile . "\n");
}

,您可以通过下一个文件的名称对文件进行菊花链,而不是仅仅对 .secret 进行硬编码在上一篇中。

类似的东西(perl 语法可能是错误的)

$configFile = $config->configFile;

while (-e $configfile) {
  $config->configFile(""); //do not read any more files
  $config->file($configFile); //after this one.
  $configFile = $config->configFile; // unless the next one is set in this
} 

或者你可以将文件放在 config.d 目录中并按字母顺序处理所有文件。

A late answer, but I found that AppConfig can be run multiple times.

To publish my files I moved all my login/passwords to configfile.ini.secret and left example login/passwords in configfile.ini

$config->define("configFile=s", {DEFAULT=>$dirname."/eversolar.ini"});
$config->args();

// Configfile must exist   
-e $config->configFile or die "Configfile '", $config->configFile, "' not found\n";

// read configfile
$config->file($config->configFile);
pmu_log("Severity 3, Configfile is: " . $config->configFile . "\n");

// Look for secret file
$config->configFile($config->configFile.".secret");
if (-e $config->configFile) {
    $config->file($config->configFile);
    pmu_log("Severity 3, Configfile is: " . $config->configFile . "\n");
}

Instead of just hardcoding the .secret, you could daisy-chain the files by having the name of the next file in the previous one.

Something like (and perl syntax is probably wrong)

$configFile = $config->configFile;

while (-e $configfile) {
  $config->configFile(""); //do not read any more files
  $config->file($configFile); //after this one.
  $configFile = $config->configFile; // unless the next one is set in this
} 

Or you could put files in a config.d directory and process all in alfabetical order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文